如何以良好的方式禁用 OpenMP 指令?
我有 C++ 代码,里面有 OpenMP 编译指示。我想在多线程模式(使用 OpenMP)和单线程模式(无 OpenMP)下测试此代码。
目前,要在模式之间切换,我需要注释#pragma omp
(或至少parallel
)。
启用/禁用 OpenMP 的最简洁或默认方法是什么?
I have C++ code with OpenMP pragmas inside. I want to test this code both for multithread mode (with OpenMP) and in single thread mode (no OpenMP).
For now, to switch between modes I need to comment #pragma omp
(or at least parallel
).
What is the cleanest, or default, way to enable / disable OpenMP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不使用 -fopenmp 选项进行编译,您将无法获得并行代码。您可以使用生成所有代码的适当定义和 makefile 来完成此操作。
OpenMP 文档说(仅是一个示例):
请参阅 http://www.openmp.org/ mp-documents/spec30.pdf(条件编译)。
If you do not compile with -fopenmp option, you won't get the parallel code. You can do it with an appropiate define and makefile that generates all codes.
The OpenMP documentation says (only an example):
See http://www.openmp.org/mp-documents/spec30.pdf (conditional compilation).
查看编译器手册以了解禁用 OpenMP 的开关。对于 GCC,OpenMP 默认情况下处于禁用状态,并通过 -fopenmp 选项启用。
另一种选择是在 OMP_NUM_THREADS 环境变量设置为 1 的情况下运行代码,尽管这与一开始不使用 OpenMP 进行编译并不完全相同。
Look into the compiler manual for the switch that disables OpenMP. For GCC, OpenMP is disabled by default and enabled with the -fopenmp option.
Another option would be to run the code with the OMP_NUM_THREADS environment variable set to 1, though that is not exactly the same as compiling without OpenMP in the first place.
通常处理此类事情的方式(一般情况)是使用
#define
和#ifdef
:在头文件中:
编译时,添加 -DSINGLETHREADED 以禁用 OpenMP :
The way such things are usually handled (the general case) is with
#define
s and#ifdef
:In your header file:
When you compile, add -DSINGLETHREADED to disable OpenMP: