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

发布于 2024-10-31 19:57:02 字数 441 浏览 1 评论 0原文

我有一个由多个 .c 文件和多个 .h 文件组成的 C 程序。我希望在主函数中有一个 #pragma omp parallel 指令(以便所有线程仅创建一次),然后执行其他 OpenMP 操作,例如 #pragma omp for 在其他文件中。

但是,我似乎无法做到这一点。编译主文件时,它抱怨 #pragma omp parallelprivate()shared() 位中提到的一些变量指令不存在(在该文件中它们不存在 - 因为它们在另一个文件中),并且在编译另一个文件时它抱怨我有一个 #pragma omp for 没有封闭的#pragma omp parallel

代码在文件之间很好地分割,我不想将它们全部放回到一个文件中。有什么办法解决这个问题吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-11-07 19:57:02

你绝对可以:

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

和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.

但是不行,你不能在一个例程中设置另一个例程中变量的共享属性——它们只是不在范围内。您无法设置它们的共享,就像您无法设置它们的值一样。

一旦你启动了(比如说)inner,那里的一切都是私人的;您必须将任何共享的内容作为共享传递。

只是为了澄清有关“所有内容都是私有的”的一点:上面的内容与 thread、i 和 newthread 没有任何不同,

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

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

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.

断舍离 2024-11-07 19:57:02

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

难道不能对声明相关变量的所有文件使用通用标头吗?

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文