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 6619399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:57:44+00:00 2026-05-25T20:57:44+00:00

I have the following code which works on the compilers I have available (xlC

  • 0

I have the following code which works on the compilers I have available (xlC and gcc) but I don’t know if it is fully compliant (I didn’t find anything in the OpenMP 3.0 spec that explicitly disallows it):

#include <iostream>
#include <vector>
#include <omp.h>

struct A {
  int tid;
  A() : tid(-1) { }
  A(const A&) { tid = omp_get_thread_num(); }
};

int main() {
  A a;

  std::vector<int> v(10);
  std::vector<int>::iterator it;
#pragma omp parallel for firstprivate(a)
  for (it=v.begin(); it<v.end(); ++it)
    *it += a.tid;

  for (it=v.begin(); it<v.end(); ++it)
    std::cout << *it << ' ';
  std::cout << std::endl;
  return 0;
}

My motivation is to figure out how many threads and each thread’s id in the omp parallel for
section (I do not wish to call it for each element that is being processed though). Is there any chance that I’m causing undefined behavior?

  • 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-25T20:57:45+00:00Added an answer on May 25, 2026 at 8:57 pm

    I would just decouple (start of) the parallel region from the loop, and use private variable to keep tid:

    std::vector<int>::iterator it;
    int tid;
    #pragma omp parallel private(tid)
    {
        tid = omp_get_thread_num();
        #pragma omp for 
        for (it=v.begin(); it<v.end(); ++it)
            *it += tid; 
    }
    

    Added: below are the quotes from the OpenMP specification (Section 2.9.3.4) that make me think your code is conformant and so does not produce UB (however see another addition below):

    … the new list item is initialized from the original list item existing before the construct. The initialization of the new list item is done once for each task that references the list item in any statement in the construct. The initialization is done prior to the execution of the construct.

    For a firstprivate clause on a parallel or task construct, the initial value of the new list item is the value of the original list item that exists immediately prior to the construct in the task region where the construct is encountered.

    C/C++: … For variables of class type, a copy constructor is invoked to perform the initialization. The order in which copy constructors for different variables of class type are called is unspecified.

    C/C++: A variable of class type (or array thereof) that appears in a firstprivate clause
    requires an accessible, unambiguous copy constructor for the class type.

    Added-2: However, it is not specified which thread executes the copy constructor for a firstprivate variable. So in theory, it can be done by the master thread of the region for all copies of the variable. In this case, the value of omp_get_thread_num() will be equal in all copies, either 0 or, in case of nested parallel regions, the thread number in the outer region. So, being a defined behavior from OpenMP standpoint, it may result in a data race in your program.

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

Sidebar

Related Questions

I have the following code which works perfectly, but I want to allow access
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I have the following code which works when i put it in any blank
At the moment I have the following code which works fine. label = new
I have the following PHP code which works out the possible combinations from a
I have the following code for Android which works fine to play a sound
I have the following code, which works as I expect. What I would like
I have code that looks like the following, which works fine for displaying the
I have the following code, which works fine , however when I change the

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.