MCPcopy Create free account
hub / github.com/AdvancedCompiler/AdvancedCompiler / main

Function main

code/Chapter12/12-6-mpi-hip-cannon.cpp:46–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44 hipFree(d_C);
45}
46int main(int argc, char *argv[]){
47 int id,p,part,num,i,
48 upRank,downRank,leftRank,rightRank;
49 float *a, *b, *c;
50 float *A,*B,*C;
51 int coord[2],x,y;//本进程坐标
52 int position[2] = {0, 0};//确定(0,0)位置上的进程id
53 double start_time, end_time;
54 MPI_Comm MPI_COMM_CART;
55 MPI_Status status;
56 MPI_Init(&argc, &argv);
57 MPI_Comm_size(MPI_COMM_WORLD, &p);
58 int periodic[2];
59 int size[2];
60 part = sqrt(p);
61 //如果进程数不是平方数,则终止进程
62 if(part*part!=p){
63 printf("总进程数必须是一个平方数!\n");
64 MPI_Finalize();
65 return 0;
66 }
67 //如果行数不是进程数的整数倍
68 if(DIMS%p!=0){
69 printf("总进程数开方之后必须能整除矩阵总行数!\n");
70 MPI_Finalize();
71 return 0;
72 }
73 num = DIMS/part;
74 //建立笛卡尔通信域
75 size[0] = size[1] = part;//虚拟拓扑维数
76 periodic[0] = periodic[1] = 1;//虚拟拓扑中进程下标是否循环
77 MPI_Cart_create(MPI_COMM_WORLD, 2, size, periodic, 1, &MPI_COMM_CART);
78 MPI_Comm_rank(MPI_COMM_CART,&id);
79 MPI_Cart_coords(MPI_COMM_CART,id,2,coord);
80 x = coord[0];
81 y = coord[1];
82 //为各个进程分配对应部分块矩阵的空间
83 a = (float *)malloc(sizeof(float)*num*num);
84 b = (float *)malloc(sizeof(float)*num*num);
85 c = (float *)malloc(sizeof(float)*num*num);
86 if (id==0){
87 A = (float *)malloc(sizeof(float)*DIMS*DIMS);
88 B = (float *)malloc(sizeof(float)*DIMS*DIMS);
89 C = (float *)malloc(sizeof(float)*DIMS*DIMS);
90 Init_Matrix(A,DIMS*DIMS,2);
91 Init_Matrix(B,DIMS*DIMS,2);
92 }
93 start_time = MPI_Wtime();
94 //分发矩阵
95 Matrix_cannon_scatter(DIMS,id,num,part,A,a,MPI_COMM_CART);
96 Matrix_cannon_scatter(DIMS,id,num,part,B,b,MPI_COMM_CART);
97 Init_Matrix(c,num*num,1);
98 //获得当前矩阵块横坐标方向相聚x的左右方向的邻居进程号
99 //分块a循环左移x位
100 MPI_Cart_shift(MPI_COMM_CART, 1, x, &leftRank, &rightRank);
101 MPI_Sendrecv_replace(a, num*num, MPI_FLOAT, leftRank, 0, rightRank, 0, MPI_COMM_CART, &status);
102 //获得当前矩阵块纵坐标方向相聚y的上下邻居进程号
103 //分块b循环上移y位

Callers

nothing calls this directly

Calls 4

Init_MatrixFunction · 0.70
Matrix_cannon_scatterFunction · 0.70
Mul_MatrixdcuFunction · 0.70
Matrix_cannon_gatherFunction · 0.70

Tested by

no test coverage detected