logo资料库

矩阵相乘FOX并行算法.pdf

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
矩阵相乘的另一种算法是 FOX 算法,请写出 FOX 算法的并行程序。 #include "mpi.h" #include #include #include const int root_id = 0; const int max_procs_size = 16; int main(int argc,char *argv[]) { double start_time, end_time, time; int procs_id, procs_size; MPI_Status status; MPI_Request reqSend, reqRecv; MPI_Init(&argc,&argv); start_time = MPI_Wtime(); MPI_Comm_size(MPI_COMM_WORLD,&procs_size); MPI_Comm_rank(MPI_COMM_WORLD,&procs_id); // 参数检查 int N=0; { for (int i=1; i(procs_size))); const int n = N / procs_size_sqrt; const int n_sqr = n*n; if (procs_size<4 || procs_size> max_procs_size) ...{ printf("The fox algorithm requires at least 4 processors and at most %d processors. ", max_procs_size); MPI_Finalize(); return 0; }
if (procs_size_sqrt*procs_size_sqrt != procs_size ) ...{ printf("The number of process must be a square. "); MPI_Finalize(); return 0; } if (N % procs_size_sqrt !=0) ...{ printf("N mod procs_size_sqrt !=0 "); MPI_Finalize(); return 0; } //初始化矩阵 int * A = new int[n_sqr]; int * B = new int[n_sqr]; int * C = new int[n_sqr]; int * T = new int[n_sqr]; for (int i=0; i
int procs_cart_rank, procs_coords[2]; dims[0] = dims[1] = procs_size_sqrt; periods[0] = periods[1] = true; MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, false, &cart_all); MPI_Comm_rank(cart_all, &procs_cart_rank); MPI_Cart_coords(cart_all, procs_cart_rank, 2, procs_coords); MPI_Comm_split(cart_all, procs_coords[0], procs_coords[1], &cart_row); MPI_Comm_split(cart_all, procs_coords[1], procs_coords[0], &cart_col); int rank_cart_row, rank_cart_col; MPI_Comm_rank(cart_row, & rank_cart_row); MPI_Comm_rank(cart_col, & rank_cart_col); // 计算并传递 for (int round = 0; round < procs_size_sqrt; ++ round) ...{ MPI_Isend(B, n_sqr, MPI_INT, (procs_coords[0] - 1 + procs_size_sqrt) % procs_size_sqrt, 1, cart_col, &reqSend); int broader = (round + procs_coords[0]) % procs_size_sqrt; if (broader == procs_coords[1]) std::copy(A,A+n_sqr,T); MPI_Bcast(T, n_sqr, MPI_INT, broader , cart_row); for (int row=0; row
} // 释放 MPI_Comm_free(&cart_col); MPI_Comm_free(&cart_row); MPI_Comm_free(&cart_all); delete []A; delete []B; delete []C; delete []T; end_time = MPI_Wtime(); MPI_Finalize(); printf("task %d consumed %lf seconds ", procs_id, end_time-start_time); return 0; }
分享到:
收藏