Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:05:47+00:00 2026-05-30T03:05:47+00:00

I’m a newbie using OpenMP with C++. I was doing a simple function, loading

  • 0

I’m a newbie using OpenMP with C++. I was doing a simple function, loading two arrays using a for loop. These arrays are defined as complex.

#include <omp.h>
#include <iostream>
#include <stdlib.h>
#include <complex>
#define CHUNKSIZE   10
#define N       100

using namespace std;

int main (int argc, char *argv[]) 
{
int nthreads, tid, i, chunk;
complex<double> a[N], b[N], c[N];

/* Some initializations */
for (i=0; i < N; i++)
    a[i].real() = b[i].real() = i * 1.0;
chunk = CHUNKSIZE;

#pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)
{
    tid = omp_get_thread_num();
    if (tid == 0)
    {
        nthreads = omp_get_num_threads();
        printf("Number of threads = %d\n", nthreads);
    }
    printf("Thread %d starting...\n",tid);

#pragma omp for schedule(dynamic,chunk)
    for (i=0; i<N; i++)
    {
        c[i] = a[i] + b[i];
        printf("Thread %d: c[%d]= %e\n",tid,i,c[i]);
    }

}  /* end of parallel section */

}

When I compile, I get this warning:

omp_complex.cpp:43: warning: cannot pass objects of non-POD type ‘struct std::complex’ through ‘…’; call will abort at runtime

and If I run a.out, I get an “Illegal instruction” message on the screen. I was trying to find out what’s going on, but I didn’t find any good reference. Do anyone know if complex types are allowed in OpenMP directives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:05:47+00:00Added an answer on May 30, 2026 at 3:05 am

    The error is here:

    printf("Thread %d: c[%d]= %e\n",tid,i,c[i]);
    

    printf doesn’t know (and has no way of knowing) how to handle a std::complex. Use C++ streaming operations to output complex types.

    Furthermore, in order to avoid concurrency issues, you need to stream into a thread-local buffer before you can write to stdout, otherwise the C++ streaming syntax creates race conditions.

    I usually use a macro for that (but C++11 makes this easier with variadic templates):

    #define THREAD_SAFE_OUT(out, message) \
        do { \
            std::ostringstream buffer; \
            buffer << message; \
            out << buffer.str(); \
        while (false)
    
    …
    
    THREAD_SAFE_OUT(std::cout, "Thread " << tid << ": c[" << i << "] = " << c[i]);
    

    A word on style when using OpenMP:

    Don’t use the private directive. This is just a workaround for languages who require all variables to be declared at the beginning of the method. Since C++ doesn’t mandate this, it’s better (always) to declare the variables on first use, i.e. inside the parallel section. That way, they are also thread-private.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.