c++ - openmp Linux 中的段错误

标签 c++ linux segmentation-fault openmp

我最近刚刚学习openMp,当我在Linux上编译并运行以下程序时,我遇到了“SEGMENT FAULT”。有人可以帮助我解决它吗?

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

 void Hello(void); /* Thread function */ 

 int main(int argc, char* argv[]) {     
  /* Get number of threads from command line */     
 int thread_count = strtol(argv[1], NULL, 10); 

 # pragma omp parallel num_threads(thread_count)    
 Hello(); 

  return 0; } /* main */ 

  void Hello(void) {     
  int my_rank = omp_get_thread_num();     
  int thread_count = omp_get_num_threads(); 

  printf("Hello World from thread %d of %d\n", my_rank, thread_count); 

  } /* Hello */ 

最佳答案

您必须使用与线程数相对应的参数来启动可执行文件:

g++ -fopenmp test.cpp -o myExec
./myExec 4

否则,您必须对 thread_count 变量进行硬编码,例如:

 int thread_count = 4

那么你就不需要任何争论了。

关于c++ - openmp Linux 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50616151/

相关文章:

c - 如果一个简单的 linux c 程序链接到自定义的 glibc 库,则会出现段错误

arrays - 生成伪语言的 C 程序 - 全局 3D 数组太大(段错误)?

c++ - 我认为这个引用指向两个不同的对象有什么问题?

linux - Nagios:NRPE:无法读取输出

c++ - Makefile 报告 `func[abi:cxx11]()' 的多个定义

c - POSIX TIMER- 有多个计时器

c - 我的 C 程序出现段错误错误

c++ - 指向指针/C++ 的指针中的数据类型声明意义)

c++ - 在具有异常处理的函数中返回一个类

c++ - 重载函数运算符是否允许创建在语法上像 C++ 中的函数一样工作的对象?