Reputation: 1265
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
Upvotes: 1
Views: 129
Reputation: 414825
It might just happen too fast. I've tried a larger number of iterations and it works as expected:
// gcc -std=c99 *.c -fopenmp && ./a.out
#include <stdio.h>
#include <omp.h>
enum { N = 10000, M = N/2 };
static void loop(char* s, int d) {
for(int i=0;i<N;++i)
if (i % M == 0)
printf("Loop %s %d %d %d\n", s, i, omp_get_thread_num(),d);
}
int main() {
#pragma omp parallel sections
{
#pragma omp section
loop("A", 2);
#pragma omp section
loop("B", 3);
}
}
Loop A 0 7 2
Loop B 0 4 3
Loop A 5000 7 2
Loop B 5000 4 3
Upvotes: 1
Reputation: 471559
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:
Project -> Properties -> Configuration Properties -> C/C++ -> Language -> Open MP Support
change it to Yes (/openmp)
, and it should be enabled.
I ran your code with OpenMP setup properly and I get this:
Loop A 0 2
Loop B 2 3
Loop B 2 3
Loop B 2 3
Loop B 2 3
Loop A 0 2
Loop A 0 2
Loop A 0 2
So I think your code is correct.
Upvotes: 3