并行计算—OpenMP—负载均衡

来源:互联网 发布:电力数据采集器图 编辑:程序博客网 时间:2024/05/19 16:34
// OpenMP.cpp : 定义控制台应用程序的入口点。//负载均衡#include "stdafx.h"#include "omp.h"#include <Windows.h>#include <time.h>#define NUM_THREADS 2void smallwork(){printf("smallwork ThreadID %d\n",omp_get_thread_num());}void bigwork(){long long sum=0;for(long i=1;i<=1000000000;i++)sum+=i;printf("bigwork ThreadID %d sum=%lld\n",omp_get_thread_num(),sum);}int _tmain(int argc, _TCHAR* argv[]){omp_set_num_threads(NUM_THREADS);clock_t t1,t2;t1=clock();    #pragma omp parallel forfor(int i=1;i<=4;i++){if(i<=2)          //线程1、2上做smallwork,3、4上做bigworksmallwork();elsebigwork();}t2=clock();printf("first time=%d\n\n",t2-t1);t1=clock();#pragma omp parallel forfor(int i=1;i<=4;i++){if(i%2)          //线程1、3上做smallwork,2、4上做bigworksmallwork();elsebigwork();}t2=clock();printf("second time=%d\n\n",t2-t1);system("pause");return 0;}

运行结果:


0 0
原创粉丝点击