I have two threads with a parallel for but this one does not break up into threads:
#pragma omp parallel sections
{
#pragma omp section
{
for(int i=0;i<4;++i)
printf("Loop A %d %d\n", omp_get_thread_num(),2);
}
#pragma omp section
{
for(int i=0;i<4;++i)
printf("Loop B %d %d\n", omp_get_thread_num(),3);
}
}
Output:
Running…
Loop A 0 2
Loop A 0 2
Loop A 0 2
Loop A 0 2
Loop B 0 3
Loop B 0 3
Loop B 0 3
Loop B 0 3
This can happen for a number of reasons. I’ll list the possible ones I know:
1: Make sure you actually have a multi-core machine. If it’s a single core machine (no HT), it’ll only run with one thread.
2: If this is on Visual Studio, you need to enable OpenMP support. Just including the header is not enough:
change it to
Yes (/openmp), and it should be enabled.I ran your code with OpenMP setup properly and I get this:
So I think your code is correct.