MPI實現矩陣乘法程式--實現MPI傳遞連續陣列
阿新 • • 發佈:2019-02-11
怎麼樣用用MPI編寫兩個n階的方陣A和B的相乘程式,結果存放在方陣C中,A、B和C都在節點0中呢?
//#include<stdio.h> //#include<math.h> //#pragma comment(lib,"mpi.lib") #include<mpi.h> #include<stdio.h> #include <iostream> #include<math.h> #pragma comment(lib,"mpi.lib") #define n 1000 using namespace std; int main(int argv, char *argc[]) { int rank, p, a; MPI_Init(&argv, &argc); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Status status; if (p!=1) a = n / (p - 1); if (rank == 0) { int* A = new int[n*n]; int* B = new int[n*n]; int* C = new int[n*n]; // int * recptr = NULL; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++)// 時間是 O nn { A[i*n + j] = i + j; //A[i][j] B[i*n + j] = 1; //B[i][j] } if (p == 1) { double tb, te; tb = MPI_Wtime(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { C[i*n + j] = 0; //C[i][j] for (int k = 0; k < n; k++) { C[i*n + j] = A[i*n + k] * B[k*n + j]; } } te = MPI_Wtime(); cout << "time is " << te - tb;// << "s" << endl; } if (p != 1) { double tb, te; tb = MPI_Wtime(); for (int i = 0; i < p-1; i++){//給每個暫存器傳送 陣列 A,B,C MPI_Send(&A[0+0], n*n, MPI_INT, i+1, 1, MPI_COMM_WORLD);//每個傳送 a行,a*n大小的資料 MPI_Send(&B[0+0], n*n, MPI_INT, i+1,2, MPI_COMM_WORLD); } for (int i =0; i < p-1; i++) MPI_Recv(&C[i*a+0], a*n, MPI_INT, i+1,3, MPI_COMM_WORLD, &status);//每個接受 a行,a*n大小的資料 te = MPI_Wtime(); cout << "time is " << te - tb;// << "s" << endl; } delete[] A; delete[] B; delete[] C; } if (p != 1) if (rank != 0){ int* A = new int[n*n]; int* B = new int[n*n]; int* C = new int[n*n]; MPI_Recv(&A[0+0], n*n, MPI_INT, 0, 1, MPI_COMM_WORLD, &status);//從A[0][0]和B[0][0]開始接受 MPI_Recv(&B[0+0], n*n, MPI_INT,0, 2, MPI_COMM_WORLD, &status); for (int i =a*(rank-1); i < (a*(rank)); i++)//按照行間隔分,每個cpu計算自己的a行 for (int j = 0; j < n; j++) { C[i*n + j] = 0; //C[i][j] for (int k = 0; k < n; k++) { C[i*n + j] = A[i*n + k] * B[k*n + j]; } } {//向rank=0傳送自己的那a行C,大小是a*n //int * sendptr = &(C[a*(rank - 1)+0]); MPI_Send(&C[a*(rank - 1) + 0], a*n, MPI_INT, 0,3, MPI_COMM_WORLD);//起始地址是C[rank-1][0],大小是a*n } } MPI_Finalize(); return 0; }
經過測試,不同節點數的加速比如下:
兩個節點之所以沒有變化是因為,一個負責傳送資料,一個負責計算資料,還要傳送回來,實際上和一個計算是沒什麼差別的。但是隨著節點數的增加,時間下降的速度呈指數小於0的指數函式下降趨勢,加速比呈k=0.27的一次方程上漲,從與上一個節點的加速比例比較來看,可見每增加一倍節點的數量,只能提高1.2的速度。