9

I've added OpenMP to an existing code base in order to parallelize a for loop. Several variables are created inside the scope of the parallel for region, including a pointer:

#pragma omp parallel for
for (int i = 0; i < n; i++){
    [....]
    Model *lm;
    lm->myfunc();
    lm->anotherfunc();
    [....]
}

In the resulting output files I noticed inconsistencies, presumably caused by a race condition. I ultimately resolved the race condition by using an omp critical. My question remains, though: is lm private to each thread, or is it shared?

1 Answer 1

11

Yes, all variables declared inside the OpenMP region are private. This includes pointers.

Each thread will have its own copy of the pointer.

It lets you do stuff like this:

int threads = 8;
int size_per_thread = 10000000;

int *ptr = new int[size_per_thread * threads];

#pragma omp parallel num_threads(threads)
    {
        int id = omp_get_thread_num();
        int *my_ptr = ptr + size_per_thread * id;

        //  Do work on "my_ptr".
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. From reading the spec, it seemed as if that were the case, but it was a bit unclear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.