I have the following code in VS2008:
int i,j;
bool pr = false;
#pragma omp parallel for private(pr) num_threads(2)
for(i=0;i<PIC_X;i++)
{
int rank = omp_get_thread_num();
int count = omp_get_num_threads();
if ( !pr )
{
printf_s("Hello from thread %d of %d\n", rank, count);
pr = true;
}
for(j=0;j<PIC_Y;j++)
{
// do stuff
}
}
(Not trying to make a nested OpenMP loop, in case you’re wondering). The problem is, the num_threads clause has no effect whatsoever: I only ever get “Hello from thread 0 of 1” on the output. I tried using omp_set_num_threads(2) as well, to no avail. What gives?
You have set pr outside of the parallel region and then made pr private by putting it in a private clause. That means that each thread has a pr, but the private pr variables are not initialized. Use firstprivate rather than private for pr, so that the private variables are initialized.
However, you are incorrect about loop counters being private by default. The loop counter for the worksharing (or canonical) for (i.e., the variable i) is private (section 2.4.1 for Construct of the OMP V2.0 spec). But “j” is not. See the OpenMP V2.0 spec (which is what Microsoft supports in VS2008), section 2.7.2 Data-Sharing Attribute Clauses:
As for omp_get_num_threads() returning a 1, all I can think of is that you didn’t compile this wih the OpenMP flag enabled.