| 49 | hipFree(d_C); |
| 50 | } |
| 51 | int main(int argc, char *argv[]){ |
| 52 | float *A,*B,*C,*tempA,*tempC; |
| 53 | int world_rank, world_size,*lens,*displs,temp,i; |
| 54 | double start_time, end_time; |
| 55 | MPI_Init(&argc, &argv); |
| 56 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); |
| 57 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); |
| 58 | //总进程数需要大于等于DCU_NUM |
| 59 | if(world_size<DCU_NUM){ |
| 60 | printf("总进程数world_size应大于等于DCU_NUM(%d)!!!\n",DCU_NUM); |
| 61 | MPI_Finalize(); |
| 62 | return 0; |
| 63 | } |
| 64 | //为所有进程创建B的空间并在0进程进行初始化 |
| 65 | B=(float *)malloc(sizeof(float)*DIMS*DIMS); |
| 66 | //lens存储每个进程处理的矩阵行数 |
| 67 | lens=(int *)malloc(sizeof(int)*world_size); |
| 68 | displs=(int *)malloc(sizeof(int)*world_size); |
| 69 | |
| 70 | if(world_rank==0){ |
| 71 | A=(float *)malloc(sizeof(float)*DIMS*DIMS); |
| 72 | C=(float *)malloc(sizeof(float)*DIMS*DIMS); |
| 73 | Init_Matrix(A,DIMS*DIMS,2); |
| 74 | Init_Matrix(B,DIMS*DIMS,2); |
| 75 | Init_Matrix(C,DIMS*DIMS,1); |
| 76 | //进程0~DCU_NUM-1 |
| 77 | temp=SPEED_UP*DIMS/(DCU_NUM*SPEED_UP+world_size-DCU_NUM); |
| 78 | for (i = 0;i < DCU_NUM; i++){ |
| 79 | //lens[i]为DIMS的整数倍 |
| 80 | lens[i]=temp*DIMS; |
| 81 | } |
| 82 | temp=DIMS-temp*DCU_NUM; |
| 83 | if(world_size>DCU_NUM){ |
| 84 | //进程DCU_NUM~world_size-1 |
| 85 | if(temp%(world_size-DCU_NUM)==0){ |
| 86 | temp/=(world_size-DCU_NUM); |
| 87 | for(i=DCU_NUM;i<world_size;i++){ |
| 88 | lens[i]=temp*DIMS; |
| 89 | } |
| 90 | }else{ |
| 91 | for(i=DCU_NUM;i<world_size-1;i++){ |
| 92 | lens[i]=(temp/(world_size-DCU_NUM))*DIMS; |
| 93 | } |
| 94 | lens[world_size-1]=(temp-(temp/(world_size-DCU_NUM))*(world_size-DCU_NUM-1))*DIMS; |
| 95 | } |
| 96 | } |
| 97 | //计算偏移量 |
| 98 | displs[0]=0; |
| 99 | for(i=1;i<world_size;i++){ |
| 100 | displs[i]=lens[i-1]+displs[i-1]; |
| 101 | } |
| 102 | } |
| 103 | start_time=MPI_Wtime(); |
| 104 | //广播矩阵B以及lens到其他所有进程 |
| 105 | MPI_Bcast(B,DIMS*DIMS,MPI_FLOAT,0,MPI_COMM_WORLD); |
| 106 | MPI_Bcast(lens,world_size,MPI_INT,0,MPI_COMM_WORLD); |
| 107 | MPI_Bcast(displs,world_size,MPI_INT,0,MPI_COMM_WORLD); |
| 108 | //根据所写核函数,需分配DIMS维方阵的空间,多余部分空间补0 |
nothing calls this directly
no test coverage detected