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

【转载】设置openmp线程个数

2014-03-05 14:23 176 查看
两种方法
一。首先在函数中设置
#include"omp.h"
#include<iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int main()
{
clock_t start=clock();
#pragma omp parallel num_threads(8)
{
printf("Hello! time=%ld threadID=%d\n",clock()-start, omp_get_thread_num());
}
printf("last time=%ld",clock()-start);
system("pause");
return 0;
}
输出结果:
Hello! time=15 threadID=0
Hello! time=15 threadID=1
Hello! time=15 threadID=3
Hello! time=15 threadID=5
Hello! time=15 threadID=7
Hello! time=15 threadID=2
Hello! time=15 threadID=4
Hello! time=15 threadID=6
last time=15请按任意键继续. . .
二。在环境变量中设置



程序如下
#include"omp.h"
#include<iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int main()
{
clock_t start=clock();
#pragma omp parallel
{
printf("Hello! time=%ld threadID=%d\n",clock()-start, omp_get_thread_num());
}
printf("last time=%ld",clock()-start);
system("pause");
return 0;
}
输出如下:
Hello! time=15 threadID=0
Hello! time=15 threadID=2
Hello! time=15 threadID=1
Hello! time=15 threadID=3
last time=15请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: