Home > OS >  "omp_get_num_threads" in "pragma omp parallel for" is giving me compile error
"omp_get_num_threads" in "pragma omp parallel for" is giving me compile error

Time:12-12

I am getting

error: for statement expected before ‘printf’

in my code:

float *vector_matrix_product(float *A, int m, int n, float *x)
{
    float *b = (float*) calloc(m, sizeof(float));

    #pragma omp parallel for default(none) shared(A,m,n,x,b), private(i,j)
            printf("Threads: %d\n", omp_get_num_threads()); // << here
            for (int i = 0; i < m; i  ) {
                for (int j = 0; j < n; j  ) {
                    b[i]  = A[i * n   j] * x[j];
                }
            }

    return b;
}

because I wish to determine the number of threads in my parallel region. What's the mistake? Do I need brackets?

CodePudding user response:

The #pragma omp parallel for statement should occur on the line right before the for loop.

If you would like to spawn threads and not immediately use them for the for loop, you can spawn the threads first using #pragma omp parallel. This spawns the threads. Then you can add additional lines of code like printf, etc.

And later when you want to use the threads for the for loop, write #pragma omp for just before the for loop. This way, you won't get the error.

E.g.

#pragma omp parallel
    printf("something");
    #pragma omp for
      for(...){}
  • Related