OpenMP:"libgomp: Thread creation failed: Resource temporarily unavailable" 当代码 运行 作为普通用户

OpenMP: "libgomp: Thread creation failed: Resource temporarily unavailable" when code run as regular user

当我运行下面的例子代码:

#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

Im 运行ning Ubuntu 18.04 x86_64 4 核架构。

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 代码。我的问题是,有人可以提供有关为什么会发生这种情况的信息吗? 谢谢

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

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

运行 作为 root 允许我这样做,但是我假设普通用户不允许使用那么多内存。

来自 setrlimit() 手册页:

The hard limit acts as a ceiling for the soft limit: an unprivileged process > may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit.

A privileged process ... may make arbitrary changes to either limit value.

修复错误:

"libgomp: Thread creation failed: Resource temporarily unavailable"

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

export OMP_NUM_THREADS=1

继续……