c - 如何使用 openmp 产生一次线程

标签 c linux gcc openmp

我在我的程序中使用 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/

相关文章:

c - 使用 `GCCs` 预处理器作为汇编程序

c - 安全地直接写入内存地址

c - -I 目录与 -isystem 目录

c - 如何从 C 执行 nm 并捕获输出

linux - 链接器找不到具有显式路径的库文件

clone() 函数隐式声明

c# - 签名不兼容互操作 - 从 c# 调用 c 方法

php - Linux 上文件/文件夹名称中的特殊字符;重命名 php 函数不起作用

c - GCC 不优化未初始化静态常量的结构副本

c - 如何使用原始名称为库函数创建包装器?