您的位置:首页 > 运维架构

第一个OPENMP程序

2016-05-02 10:50 423 查看

环境:

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.csdn.net/fulva/article/details/8089592

http://blog.csdn.net/fulva/article/details/8014116
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  openmp gcc