2015-05-02 55 views
-1

我努力整天地用MPI的幫助來實現矩陣乘法,來自互聯網的所有例子都不適合我(我不知道爲什麼,它編譯,運行但不計算)。下面是我在做什麼:將根據矩陣的mpi行計算出來的所有數據收集到根矩陣

從慶典:

的mpirun -n 2輸出/ LB8

它讀矩陣的2x4(每處理1行),並開始計算。 問題是在SENDRECV塊(或通常在收集數據)

void Matrix_MPY(double **matrix_a, double **matrix_b, double ***matrix_c, int a_rows, int a_cols) { 
    int i, j; 
    int process_rank, process_count; 
    MPI_Comm_rank(MPI_COMM_WORLD, &process_rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &process_count); 

    if (a_rows % process_count != 0) { 
     error_code = NOT_DEVIDED_BY_RANK_EXCEPTION; 
     return; 
    } 

    int rows_per_process = a_rows/process_count; 
    int current_row = rows_per_process * process_rank; 

    double **temp; 
    temp = (double **) malloc(sizeof(double *) * a_rows); 
    for (i = 0; i < a_rows; ++i){ 
     temp[i] = (double *) malloc(sizeof(double) * a_rows); 
    } 

    for (i = current_row; i < current_row + rows_per_process; ++i) { 
     for (j = 0; j < a_rows; ++j) 
     { 
      int k; 
      for(k = 0; k < a_cols; ++k){ 
       temp[i][j] += matrix_a[i][k] * matrix_b[k][j]; 
      } 
     } 
     MPI_Sendrecv(temp[i], a_rows, MPI_DOUBLE, ROOT, TAG, temp[i], a_rows, MPI_DOUBLE, process_rank, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
    } 

    *matrix_c = temp; 
} 

回答

0

該溶液爲我

.... 
     if (process_rank != ROOT) 
     MPI_Send(temp[i], a_rows, MPI_DOUBLE, ROOT, i, MPI_COMM_WORLD); 
} 

if (process_rank == ROOT) { 
    for (i = 1; i < process_count; ++i) 
    { 
     for (j = i * rows_per_process; j < i * rows_per_process + rows_per_process; ++j) 
     { 
      MPI_Recv(temp[j], a_rows, MPI_DOUBLE, i, j, MPI_COMM_WORLD, MPI_STATUSES_IGNORE); 
     } 
    } 
} 

*matrix_c = temp; 
工作