是否可以在程序的多个文件中使用 OpenMP 指令?
我有一个由多个 .c 文件和多个 .h 文件组成的 C 程序。我希望在主函数中有一个 #pragma omp parallel 指令(以便所有线程仅创建一次),然后执行其他 OpenMP 操作,例如 #pragma omp for
在其他文件中。
但是,我似乎无法做到这一点。编译主文件时,它抱怨 #pragma omp parallel
的 private()
和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你绝对可以:
outer.c:
inner.c:
和running:
但是不行,你不能在一个例程中设置另一个例程中变量的共享属性——它们只是不在范围内。您无法设置它们的共享,就像您无法设置它们的值一样。
一旦你启动了(比如说)inner,那里的一切都是私人的;您必须将任何共享的内容作为共享传递。
只是为了澄清有关“所有内容都是私有的”的一点:上面的内容与 thread、i 和 newthread 没有任何不同,
因为 thread、i 和 newthread 是在并行块内定义的 - 无论是否在函数内部 - 它们都必须是私有的。
You definitely can:
outer.c:
inner.c:
and running:
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
since thread, i, and newthread are defined inside the parallel block -- whether inside a function or not -- they're all necessarily private.
您是否尝试过“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)?