說2個進程正在參與。流程0(等級0)具有如何從處理器獲取MPI_Gatherv列,其中每個進程可能發送不同數量的列
A = { a d
b e
c f
}
和處理1(等級1)具有
A = { g
h
i
}
我想兩個處理器來發送這些列序號爲0,這樣等級0將在下面說另一個2D陣列。
B = { a d g
b e h
c f i
}
我創建了MPI_Gatherv新列的數據類型,並正嘗試下面的代碼,這讓我沒有在那裏。
我的具體問題是:
- 我應該如何處理這一
- 應該是什麼send_type和recv_type。
- 應該如何規定的位移(如果他們在新的數據類型或MPI_CHAR期限)
感謝。
這是我的代碼:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
{
int numprocs, my_rank;
long int i, j;
MPI_Status status;
char **A;
char **B;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if(my_rank == 0)
{
A = (char **)calloc((3), sizeof(char *));
B = (char **)calloc((3), sizeof(char *));
for(i=0; i<3; ++i)
{
A[i] = (char *)calloc(2, sizeof(char));
B[i] = (char *)calloc(3, sizeof(char));
}
A[0][0] = 'a';
A[1][0] = 'b';
A[2][0] = 'c';
A[0][1] = 'd';
A[1][1] = 'e';
A[2][1] = 'f';
}
else
{
A = (char **)calloc((3), sizeof(char *));
for(i=0; i<3; ++i)
{
A[i] = (char *)calloc(1, sizeof(char));
}
A[0][0] = 'g';
A[1][0] = 'h';
A[2][0] = 'i';
}
MPI_Datatype b_col_type;
MPI_Type_vector(3, 1, 1, MPI_CHAR, &b_col_type);
MPI_Type_commit(&b_col_type);
int displs[2] = {0, 2};
int recvcounts[2] = {2, 1};
MPI_Gatherv(&A[0][0], recvcounts[my_rank], b_col_type, &B[0][0], recvcounts, displs, b_col_type, 0, MPI_COMM_WORLD);
if(my_rank == 0)
{
for(i=0; i<3; ++i)
{
for(j=0; j<3; ++j)
printf("%c ", B[i][j]);
printf("\n");
}
}
MPI_Finalize();
return 0;
}
你是什麼意思「它讓你無處」?確切的問題是什麼? – suszterpatt 2011-03-20 23:29:31
所以,如果我嘗試運行上面的代碼,在到B得到的東西只是 $的mpirun -np 2 try_gather 一個d 所以只有排名0能夠發送它的2個元素。我試着調整了displs和recvcounts的值,但是我無法得到我想要的結果。目前的代碼至少有2個字符在正確的地方。 我放棄嘗試在註釋部分中設置代碼的格式。基本上'a'和'd'去正確的地方。休息都是空白在陣列B. – Reep 2011-03-20 23:57:10