码迷,mamicode.com
首页 > 其他好文 > 详细

OpenMp之reduction求和

时间:2014-09-30 12:32:59      阅读:571      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   2014   c   on   

// OpenMP1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"omp.h"
#include<Windows.h>
#include<time.h>
#include<iostream>
using namespace std;

#define NUM_THREADS 4
int _tmain(int argc, _TCHAR* argv[])
	{
	 omp_set_num_threads(NUM_THREADS);
	 long long sum=0;
	 clock_t time_begin =clock();
	#pragma omp parallel for reduction(+:sum)
		 
         for(long i=1;i<=1000000000;i++)
			  {
			   sum+=i;
			  }
			
		 

	  clock_t time_end=clock();
	  double s1=time_end-time_begin; 
	  cout<<"Sum="<<sum<<endl;
	  cout<<"并行时间="<<s1<<endl;
	//******************************************************
	  sum=0;
	  time_begin=clock();
	  for(long i=1;i<=1000000000;i++)
	  {
	   sum+=i;
	  }
	  time_end=clock();
	  double s2=time_end-time_begin;
	  cout<<"Sum="<<sum<<endl;
	  cout<<"串行时间="<<s2<<endl;
	  cout<<"加速比为"<<(s2/s1)<<endl;


	 system("pause");
	 return 0;
	}

OpenMp之reduction求和

标签:blog   io   os   ar   for   sp   2014   c   on   

原文地址:http://blog.csdn.net/christprince007/article/details/39692601

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!