2

当我运行以下示例代码时:

#include "stdio.h"
#include <omp.h>

int main(int argc, char *argv[])
{
  #pragma omp parallel
  {
   int NCPU,tid,NPR,NTHR;
    /* get the total number of CPUs/cores available for OpenMP */
   NCPU = omp_get_num_procs();
   /* get the current thread ID in the parallel region */
   tid = omp_get_thread_num();
   /* get the total number of threads available in this parallel region */
   NPR = omp_get_num_threads();
   /* get the total number of threads requested */
   NTHR = omp_get_max_threads();
   /* only execute this on the master thread! */

   if (tid == 0) {
     printf("%i : NCPU\t= %i\n",tid,NCPU);
     printf("%i : NTHR\t= %i\n",tid,NTHR);
     printf("%i : NPR\t= %i\n",tid,NPR);
   }
   printf("%i : hello multicore user! I am thread %i out of %i\n",tid,tid,NPR);
  }
  return(0);
 }

使用命令:gcc -fopenmp example.c -o example.exe然后./example我得到错误:libgomp: Thread creation failed: Resource temporarily unavailable但是,当我在下运行相同的代码和命令时,sudo我得到了预期的输出:

0 : NCPU    = 4
0 : NTHR    = 4
0 : NPR = 4
2 : hello multicore user! I am thread 2 out of 4
1 : hello multicore user! I am thread 1 out of 4
0 : hello multicore user! I am thread 0 out of 4
3 : hello multicore user! I am thread 3 out of 4

我在具有 4 个内核的 x86_64 架构上运行 Ubuntu 18.04。

Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  2
Core(s) per socket:  2
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               78
Model name:          Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz

我真的不习惯以 root 用户身份使用 Openmp 运行 c 代码。我的问题是,有人可以提供有关为什么会发生这种情况的信息吗?谢谢

4

2 回答 2

2

问题解决了!我正在分配我需要的堆栈限制,ulimit -s <stack-size>而不是这样做,setrlimit()因为我不相信这setrlimit()是有效的。

ulimit -s使用千字节并setrlimit()使用字节。我试图分配32388608千字节而不是字节!

以 root 身份运行允许我执行此操作,但是我假设的普通用户不允许使用那么多内存。

setrlimit()手册页:

硬限制充当软限制的上限:非特权进程 > 只能将其软限制设置为从 0 到硬限制的范围内的值,并且(不可逆地)降低其硬限制。

特权进程...可以对任一限制值进行任意更改。

于 2018-11-19T17:14:10.480 回答
2

修复错误:

"libgomp: Thread creation failed: Resource temporarily unavailable"

专门用于只有 1-cpu 的系统,只需要减少线程:

export OMP_NUM_THREADS=1

继续……

于 2021-09-10T13:50:42.127 回答