OpenMP C++代码
将矩阵A按行分块,放入六个内核中计算Ci=Ai*B,i=(0,1,2,3,4,5)。
#include <iostream>
#include <omp.h> // OpenMP编程需要包含的头文件
#include <time.h>
#include <stdlib.h>
using namespace std;
#define MatrixOrder 1000
int A[MatrixOrder][MatrixOrder] = { 0 };
int B[MatrixOrder][MatrixOrder] = { 0 };
int C[MatrixOrder][MatrixOrder] = { 0 };
void matrixInit()
{
for (int row = 0; row < MatrixOrder; row++) {
for (int col = 0; col < MatrixOrder; col++) {
srand(row + col);
A[row][col] = (rand() % 10);
B[row][col] = (rand() % 10);
}
}
//#pragma omp barrier
}
int main()
{
matrixInit();
clock_t t1 = clock(); //开始计时;
for (int row = 0; row < MatrixOrder; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
// printf("%d ", C[row][col]);
C[row][col] = 0;
}
// printf("\n");
}
clock_t t2 = clock(); //结束计时
cout << "串行time: " << t2 - t1 << endl;
clock_t t3 = clock(); //开始计时;
omp_set_num_threads(6);
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
for (int row = 0; row < MatrixOrder / 6; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
#pragma omp section
for (int row = MatrixOrder / 6; row < MatrixOrder / 3; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
#pragma omp section
for (int row = MatrixOrder / 3; row < MatrixOrder / 2; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
#pragma omp section
for (int row = MatrixOrder / 2; row < 2 * MatrixOrder / 3; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
#pragma omp section
for (int row = 2 * MatrixOrder / 3; row < 5 * MatrixOrder / 6; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
#pragma omp section
for (int row = 5 * MatrixOrder / 6; row < MatrixOrder; row++) {
for (int col = 0; col < MatrixOrder; col++) {
for (int trans = 0; trans < MatrixOrder; trans++) {
C[row][col] += A[row][trans] * B[trans][col];
}
}
}
}
}
clock_t t4 = clock(); //结束计时
cout << "并行time: " << t4 - t3 << endl;
cout << "加速比:" << double((t2-t1)/double((t4-t3)))<<endl;
//for (int row = 0; row < MatrixOrder; row++) {
// for (int col = 0; col < MatrixOrder; col++) {
// for (int trans = 0; trans < MatrixOrder; trans++) {
// }
// printf("%d ", C[row][col]);
// }
// printf("\n");
//}
system("pause");
return 0;
}