环境:
mac OSX
gcc 版本: 4.9
gcc -v :
gcc version 4.9.2 20141029 (prerelease) (GCC)
如果gcc版本是4.2,直接编译含omp.h的文件时,会报错(No such file or directory)
安装4.9的具体步骤如下:
http://stackoverflow.com/questions/20340117/omp-h-library-isnt-found-in-the-gcc-version-4-2-1-in-mavericks
如果报错说,fatal error: stdio.h: No such file or directory
#include “stdio.h”
再执行xcode-select –install 即可解决路径依赖的问题
测试代码如下:
#include <stdio.h>
#include "omp.h"
int main () {
int nthreads, tid;
/* Fork team of threads with private var tid */
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num(); /* get thread id */ printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0) {
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master and terminate */ }
编译:
» gcc -fopenmp hello.c
输出:
Hello World from thread = 2
Hello World from thread = 1
Hello World from thread = 0
Hello World from thread = 3
Number of threads = 4
系统默认开启线程为4
修改方式:
export OMP_NUM_THREADS=20
openmp教程:
http://blog.youkuaiyun.com/fulva/article/details/8089592
http://blog.youkuaiyun.com/fulva/article/details/8014116
本文介绍了在Mac OS X环境下,如何从gcc版本4.2升级到4.9以支持OpenMP,并提供了升级的具体步骤。在遇到stdio.h文件找不到的错误时,通过执行`xcode-select –install`可以解决。最后,展示了编译和运行一个简单的OpenMP程序的过程,以及如何修改默认的线程数。

被折叠的 条评论
为什么被折叠?



