gpt4 book ai didi

c - OpenMP:当代码以普通用户身份运行时为 "libgomp: Thread creation failed: Resource temporarily unavailable"

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:36 31 4
gpt4 key购买 nike

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

#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:线程创建失败:资源暂时不可用 但是,当我在 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 代码。我的问题是,有人可以提供有关为什么会发生这种情况的信息吗?谢谢

最佳答案

修复错误:

"libgomp: Thread creation failed: Resource temporarily unavailable"

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

export OMP_NUM_THREADS=1

继续......

关于c - OpenMP:当代码以普通用户身份运行时为 "libgomp: Thread creation failed: Resource temporarily unavailable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351194/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com