Openmp 矩阵乘法和奇偶排序

奇偶排序

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include
using namespace std;
const int NUM = 8000;
const int MAX = 1000000;
const int MIN = 0;
const int NUM_THREADS = 8;
int arr[NUM];
int main() {
 printf("Odd-even transportion sort start..." );
 printf("Generate random data... ");
 memset(arr, 0, NUM * sizeof(arr[0]));
 srand(time(NULL));
 omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for
 for (int i = 0; i < NUM; i++) {
  arr[i] = MIN + rand() % (MAX - MIN);
 }
 printf("Done."  );
 printf("Sorting... " );
 double start_time = omp_get_wtime();
 for (int i = 0; i < NUM; i++) {
  if (i % 2 == 0) {
#pragma omp parallel for
   for (int j = 0; j < NUM - 1; j += 2) {
    if (arr[j] > arr[j + 1]) {
     int temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
    }
   }
  } else {
#pragma omp parallel for
   for (int j = 1; j < NUM - 1; j += 2) {
    if (arr[j] > arr[j + 1]) {
     int temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
    }
   }
  }
 }
 double end_time = omp_get_wtime();
 printf(" parallel time=%fs\n",end_time - start_time);
 printf("Done.");
 printf("Result:"  );
 int counter = 1;
 int row = 20;
 for (int i = 0; i < NUM; i++,counter++) {
  printf(" %d\t",arr[i] );
  if (counter % row == 0) printf("\n");
 }
}

奇偶排序2.0

#include 
#include 
#include 
#include
#include 
#include 
#include 
#include
using namespace std;
const int N = 800000;
const int MAX = 10000000;
const int MIN = 0;
const int NUM_THREADS = 8;
int arr[N];
void solve(int NUM, int NUM_THREADS)
{
 memset(arr, 0, NUM * sizeof(arr[0]));
 srand(time(NULL));
 omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for
 for (int i = 0; i < NUM; i++) {
  arr[i] = MIN + rand() % (MAX - MIN);
 }
 printf("Done."  );
 printf("Sorting... " );
 double start_time = omp_get_wtime();
 for (int i = 0; i < NUM; i++) {
  if (i % 2 == 0) {
#pragma omp parallel for
   for (int j = 0; j < NUM - 1; j += 2) {
    if (arr[j] > arr[j + 1]) {
     int temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
    }
   }
  } else {
#pragma omp parallel for
   for (int j = 1; j < NUM - 1; j += 2) {
    if (arr[j] > arr[j + 1]) {
     int temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
    }
   }
  }
 }
 double end_time = omp_get_wtime();
 printf(" parallel time=%fs\n",end_time - start_time);
 printf("Done.");
 printf("Result:"  );
 int counter = 1;
 int row = 20;
 for (int i = 0; i < NUM; i++,counter++) {
  printf(" %d\t",arr[i] );
  if (counter % row == 0) printf("\n");
}
}
int main(int argc, char* argv[]) 
{
 printf("Odd-even transportion sort start..." );
 printf("Generate random data... ");
 int NUM,NUM_THREADS;
 while(scanf("%d%d",&NUM,&NUM_THREADS)!=EOF)
 {
  printf("you input number  %d .number of parallel threads %d\n",NUM,NUM_THREADS);
  solve(NUM,NUM_THREADS);
 }
      return 0;
}

矩阵乘法

#include
#include 
#include 
#include 
#define NN 2000
int A[NN][NN], B[NN][NN];
long long C[NN][NN];
void solve(int n, int num_thread)
{
 int i, j, t, k, paralleltime,serialtime;
 clock_t startTime, endTime;
 long long sum;
 omp_set_num_threads(num_thread);
 //--------------对矩阵A和矩阵B进行初始化-------------
 for(i=0;i<n;i++)   
 {
  t=i+1;
  for(j=0;j<n;j++)
  {
   A[i][j]=t++;
   B[i][j]=1;
  }
 }
 //----------------矩阵乘法并行算法------------------
 double start_time = omp_get_wtime();
 sum=0;
    #pragma omp parallel shared(A,B,C) private(i,j,k)
 {
        #pragma omp for schedule(dynamic)
  for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
   {
    C[i][j]=0;
    for(k=0;k<n;k++)
    {
     C[i][j]+=A[i][k]*B[k][j];
    }
   }
  }
 }
 for(i=0;i<n;i++)
  for(j=0;j<n;j++) 
   sum+=C[i][j];
 double end_time = omp_get_wtime();
 printf("sum of c=%lld parallel time=%fs\n",sum,end_time - start_time);
//--------------------------------------------------------
//--------------------------------------------------------
 start_time = omp_get_wtime();
 sum = 0;
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   C[i][j]=0;
   for(k=0;k<n;k++)
   {
    C[i][j]+=A[i][k]*B[k][j];
   }
  }
 }
 for(i=0;i<n;i++)
  for(j=0;j<n;j++) 
   sum+=C[i][j];
  end_time = omp_get_wtime();
 printf("sum of c =%lld sequential time=%fs\n",sum,end_time - start_time);
 //----------------------------------------------------
}
int main(int argc, char* argv[])
{
 int n, num_thread;
 printf("number of rows of matrix  N<=2000.number of parallel threads\n\n");
 while(scanf("%d%d",&n,&num_thread)!=EOF)
 {
  printf("you input number of rows of matrix %d .number of parallel threads %d\n",n,num_thread);
  solve(n,num_thread);
 }
 return 0;
}

你可能感兴趣的:(Openmp 矩阵乘法和奇偶排序)