我在我的程序中使用 openmp,但速度没有加快而是变慢了。我的猜测是因为它每次在循环中使用 openmp 时都会生成线程。有没有办法在程序中产生一次线程。
最佳答案
根据其他评论,您绝对应该提供代码。
通常,openMP 应用程序设计为运行的线程数是其处理器数的 1-4 倍。
对于任何对控制生成的线程数感兴趣的人,来自:https://computing.llnl.gov/tutorials/openMP/
How Many Threads?
The number of threads in a parallel region is determined by the following factors, in order of precedence:
- Evaluation of the IF clause
- Setting of the NUM_THREADS clause
- Use of the omp_set_num_threads() library function
- Setting of the OMP_NUM_THREADS environment variable
- Implementation default - usually the number of CPUs on a node, though it could be dynamic (see next bullet).
Threads are numbered from 0 (master thread) to N-1
如何设置线程数环境变量的示例:
export OMP_NUM_THREADS=8
希望对您有所帮助。
关于c - 如何使用 openmp 产生一次线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604194/