开发者

Is it possible to use OpenMP directives in multiple files in a program?

开发者 https://www.devze.com 2023-02-25 09:30 出处:网络
I\'ve got a C program which consists of multiple .c files and multiple .h files. I\'d like to have one #pragma omp parallel directive (so that all of the threads are on开发者_StackOverflow社区ly creat

I've got a C program which consists of multiple .c files and multiple .h files. I'd like to have one #pragma omp parallel directive (so that all of the threads are on开发者_StackOverflow社区ly created once) in the main function, and then do other OpenMP things like #pragma omp for in other files.

However, I can't seem to do this. When compiling the main file it complains that some of the variables mentioned in the private() and shared() bits of the #pragma omp parallel directive don't exist (which they don't, in that file - because they're in the other file), and when compiling the other file it complains that I have an #pragma omp for without an enclosing #pragma omp parallel.

The code is nicely split between the files, and I'd hate to have to put it all back into one file. Is there any way around this?


You definitely can:

outer.c:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

void inner(int n);

int main(int argc, char **argv) {
    int n=15;

    #pragma omp parallel shared(n) num_threads(4)
    {
        inner(n);
    }

    return 0;
}

inner.c:

#include <omp.h>
#include <stdio.h>

void inner(int n) {
    int thread = omp_get_thread_num();
    printf("%3d: got %d\n", thread, n);

    #pragma omp for
    for  (int i=0;i<n;i++) {
        int newthread=omp_get_thread_num();
        printf("%3d: doing iter %d.\n",newthread,i);
    }
}

and running:

$ make
gcc -fopenmp -std=c99   -c -o outer.o outer.c
gcc -fopenmp -std=c99   -c -o inner.o inner.c
gcc -o nested outer.o inner.o -fopenmp -std=c99 -lgomp    
$ ./nested 
  3: got 15
  3: doing iter 12.
  3: doing iter 13.
  3: doing iter 14.
  0: got 15
  0: doing iter 0.
  0: doing iter 1.
  0: doing iter 2.
  0: doing iter 3.
  1: got 15
  1: doing iter 4.
  1: doing iter 5.
  1: doing iter 6.
  1: doing iter 7.
  2: got 15
  2: doing iter 8.
  2: doing iter 9.
  2: doing iter 10.
  2: doing iter 11.

But no, you can't set the sharing attributes of variables in one routine from another -- they're just not in scope. You can't set their sharing any more than you can set their value.

Once you've launched (say) inner, everything there is private; you'd have to pass any shared things in as shared.

Just to clairify that bit about "everything there is private": the above isn't any different than

    int n=15;

    #pragma omp parallel shared(n) num_threads(4)
    {
        int thread = omp_get_thread_num();
        printf("%3d: got %d\n", thread, n);

        #pragma omp for
        for  (int i=0;i<n;i++) {
            int newthread=omp_get_thread_num();
            printf("%3d: doing iter %d.\n",newthread,i);
        }
    }

since thread, i, and newthread are defined inside the parallel block -- whether inside a function or not -- they're all necessarily private.


Did you try "extern XY" to make the variables known in the other file?

Can't you use a common header for all your files that declares the variables in question?

Are they local variables (then you cannot do this anyway)?

0

精彩评论

暂无评论...
验证码 换一张
取 消