【转载】设置openmp线程个数

来源:互联网 发布:iphoto 免费 mac 编辑:程序博客网 时间:2024/05/17 10:52

两种方法

一。首先在函数中设置

#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请按任意键继续. . .

二。在环境变量中设置

设置openmp线程个数 - QueenyLv - includeQueenyLv.h
 

 

 

 程序如下

#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请按任意键继续. . .

0 0