是否可以在程序中的多个文件中使用OpenMP指令?

时间:2021-10-23 22:58:15

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 only created once) in the main function, and then do other OpenMP things like #pragma omp for in other files.

我有一个C程序,它包含多个.c文件和多个.h文件。我想在main函数中有一个#pragma omp parallel指令(所有线程只创建一次),然后在其他文件中执行其他OpenMP操作,如#pragma omp。

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.

但是,我似乎无法做到这一点。在编译主文件时,它抱怨#pragma omp parallel指令的private()和shared()位中提到的某些变量不存在(它们不存在,在该文件中 - 因为它们在另一个文件),当编译另一个文件时,它抱怨我有一个#pragma omp,没有封闭的#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?

代码在文件之间很好地分开,我不想把它全部放回到一个文件中。有没有办法解决?

2 个解决方案

#1


3  

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.

因为thread,i和newthread是在并行块内部定义的 - 无论是否在函数内部 - 它们都必须是私有的。

#2


1  

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

您是否尝试使用“extern XY”来使变量在其他文件中已知?

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)?

它们是局部变量(那么你无论如何都不能这样做)?

#1


3  

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.

因为thread,i和newthread是在并行块内部定义的 - 无论是否在函数内部 - 它们都必须是私有的。

#2


1  

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

您是否尝试使用“extern XY”来使变量在其他文件中已知?

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)?

它们是局部变量(那么你无论如何都不能这样做)?