2017-09-28 136 views
0

我想在我的mpi程序中執行邊界交換。 我有結構,看起來像:笛卡爾拓撲中的邊界交換mpi

cell** local_petri_A; 

local_petri_A = calloc(p_local_petri_x_dim,sizeof(*local_petri_A)); 

for(int i = 0; i < p_local_petri_x_dim ; i ++){ 
     local_petri_A[i] = calloc(p_local_petri_y_dim,sizeof(**local_petri_A)); 
    } 

其中單元爲:

typedef struct { 
    int color; 
    int strength; 
} cell; 

我想有一個交流計劃像這樣的畫面:border exchange in cartesian topology

所以我把我的計劃在笛卡爾拓撲結構中,首先定義mpi類型以執行交換: void create_types(){

//////////////////////////////// 
//////////////////////////////// 
// cell type 
const int nitems=2; 
int   blocklengths[2] = {1,1}; 
MPI_Datatype types[2] = {MPI_INT, MPI_INT}; 
MPI_Aint  offsets[2]; 

offsets[0] = offsetof(cell, color); 
offsets[1] = offsetof(cell, strength); 

MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_cell_t); 
MPI_Type_commit(&mpi_cell_t); 
//////////////////////////////// 
/////////////////////////////// 

MPI_Type_vector (x_inside , 1 , 1 , mpi_cell_t , & border_row_t); 
MPI_Type_commit (& border_row_t); 
/*we put the stride to x_dim to get only one column*/ 
MPI_Type_vector (y_inside , 1 , p_local_petri_x_dim , MPI_DOUBLE , & border_col_t); 
MPI_Type_commit (& border_col_t); 

}

,最後嘗試執行從南北交流:

/*send to the north receive from the south */ 
    MPI_Sendrecv (& local_petri_A[0][1] , 1 , border_row_t , p_north , TAG_EXCHANGE ,& local_petri_A [0][ p_local_petri_y_dim -1] , 1 , border_row_t , p_south , TAG_EXCHANGE ,cart_comm , MPI_STATUS_IGNORE); 
    /*send to the south receive from the north */ 
    MPI_Sendrecv (& local_petri_A[0][ p_local_petri_y_dim -2] , 1 , border_row_t , p_south , TAG_EXCHANGE ,& local_petri_A [0][0] , 1 , border_row_t , p_north , TAG_EXCHANGE ,cart_comm , MPI_STATUS_IGNORE); 

注意:本節x_inside和y_inside是數組的「內部」尺寸(無鬼影部分)和p_local_petri_dim是整個陣列的尺寸。

然後,我有這樣的錯誤:enter image description here

有什麼,我做錯了?

非常感謝您的幫助。

回答

1

問題在於你分配2D數組的方式。 您分配一個數組數組,因此連續內存中不可能有兩行。因此,您的ddt列與您的二維數組佈局不匹配。您可以參考MPI_Bcast a dynamic 2d array正確地分配您的數組。作爲一個注意事項,Fortran沒有這種問題,所以如果這是一個選項,那會讓你的生活更輕鬆。

+0

好吧,它不會爲列而工作,但行呢?在這裏,我只嘗試發送一些行,我仍然有一個錯誤,我的分配行在memry中是contiguos,不是嗎? –

+0

有沒有辦法使它適合mpi類型? –

+0

行肯定是連續的。請使用[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)編輯您的問題,我將查看它 –