首页 > 代码库 > OpenMP之矩阵转置

OpenMP之矩阵转置

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

#include "stdafx.h"
#include"omp.h"
#include<Windows.h>
#include<iostream>
#include<time.h>
#define NUM_THREADS 2

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{ 
	omp_set_num_threads(NUM_THREADS);
	 int a[400][400];//inint matrix
	 int counter=1;
     for(int  i=0;i<400;i++)
     {
         for(int  j=0;j<400;j++)
         {
             a[i][j]=counter;
             counter++;
         }
     }
     // for(int i=0;i<20;i++)
      //   {
      //    for(int j=0;j<20;j++)
       //    cout<<a[i][j]<<" ";
       //   cout<<endl;
        // }
	  cout<<"parallel begin"<<endl;
	  clock_t time_begin=clock();
	  int i=0,j=0;
#pragma omp parallel for private(i,j)
     for(i=0;i<400;i++)
      for(j=i+1;j<400;j++)//从i+1开始。执行交换
      {
		 long  tmp=0;
       tmp=a[i][j];
       a[i][j]=a[j][i];
       a[j][i]=tmp;
      }
	clock_t time_end=clock();
	double s1=time_end-time_begin;
	 cout<<"parallel end"<<endl;
	 cout<<"parallel time ="<<s1<<endl;
	 //============================================================================================================
	 time_begin=clock();
	 cout<<"serail begin"<<endl;
	   for(int  i=0;i<400;i++)
      for(int j=i+1;j<400;j++)//从i+1开始。执行交换
      {
		int tmp=0;
       tmp=a[i][j];
       a[i][j]=a[j][i];
       a[j][i]=tmp;
      }
	  time_end=clock();
	  cout<<"serail end"<<endl;
	  double s2=time_end-time_begin;
	  cout<<"serail time="<<s2<<endl;

 

	 system("pause");
	return 0;
}


OpenMP之矩阵转置