1

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
0

2 Answers 2

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:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks friend for taking the time to confirm. I have spent 30 minutes scratching my head on this. I am actually on a Core 2 Duo (Penryn) 2.0 Ghz macbook 2009 model and compiling under Xcode 3.23 Snow Leopard 10.6.8 GCC. It's befuddling because the for loops are working.
1

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);
  }
}

Output

Loop A 0 7 2
Loop B 0 4 3
Loop A 5000 7 2
Loop B 5000 4 3

Comments

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.