我想从double数组中收集数据,并同时组织它们。假设我们有两个MPI级别:
if(rank == 0)
P = {0,1,4,5,8,9};
else
P = {2,3,6,7,10,11}; 如何收集位于P中的信息并按顺序找到它们,即:主目录中的P应该包含P= [0 1 2....9 10 11]
我可以按原样收集P,然后在root中重新组织它,但是随着P的增加,这种方法不会很有效。我已经尝试过创建一个MPI_Type_vector,但是我还没有把它做好。有什么想法吗?
发布于 2014-04-30 16:24:00
这在一定程度上取决于你所说的“有序”是什么意思。如果您的意思是,就像上面的例子一样,每个向量都是由数据块组成的,并且您希望这些块以固定的已知顺序交织在一起,是的,您当然可以这样做。(这个问题也可以被解读为,你是否可以作为集合的一部分进行排序;这要困难得多。)
您有正确的方法;您希望按原样发送数据,但将数据接收到指定的按处理器分解的块中。在这里,您希望接收到的数据类型如下所示:
MPI_Datatype vectype;
MPI_Type_vector(NBLOCKS, BLOCKSIZE, size*BLOCKSIZE, MPI_CHAR, &vectype);也就是说,对于给定处理器的输入,您将将其接收到大小为NBLOCKS的BLOCKSIZE块中,每个块都由多个处理器分隔,这些块的大小是块大小的倍数。实际上,您可以接收到该类型;但是,要收集到该类型,您需要设置区段,以便将来自每个处理器的数据收集到正确的位置:
MPI_Datatype gathertype;
MPI_Type_create_resized(vectype, 0, BLOCKSIZE*sizeof(char), &gathertype);
MPI_Type_commit(&gathertype);例如,在this answer中给出了调整大小的原因,并且可能在此站点的其他地方也是如此。
将这些合并到示例代码中,我们将得到以下信息:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char **argv) {
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
const int BLOCKSIZE=2; /* each block of data is 2 items */
const int NBLOCKS =3; /* each task has 3 such blocks */
char locdata[NBLOCKS*BLOCKSIZE];
for (int i=0; i<NBLOCKS*BLOCKSIZE; i++)
locdata[i] = 'A' + (char)rank; /* rank 0 = 'AAA..A'; rank 1 = 'BBB..B', etc */
MPI_Datatype vectype, gathertype;
MPI_Type_vector(NBLOCKS, BLOCKSIZE, size*BLOCKSIZE, MPI_CHAR, &vectype);
MPI_Type_create_resized(vectype, 0, BLOCKSIZE*sizeof(char), &gathertype);
MPI_Type_commit(&gathertype);
char *globaldata = NULL;
if (rank == 0) globaldata = malloc((NBLOCKS*BLOCKSIZE*size+1)*sizeof(char));
MPI_Gather(locdata, BLOCKSIZE*NBLOCKS, MPI_CHAR,
globaldata, 1, gathertype,
0, MPI_COMM_WORLD);
if (rank == 0) {
globaldata[NBLOCKS*BLOCKSIZE*size] = '\0';
printf("Assembled data:\n");
printf("<%s>\n", globaldata);
free(globaldata);
}
MPI_Type_free(&gathertype);
MPI_Finalize();
return 0;
}跑步意味着:
$ mpirun -np 3 ./vector
Assembled data:
<AABBCCAABBCCAABBCC>
$ mpirun -np 7 ./vector
Assembled data:
<AABBCCDDEEFFGGAABBCCDDEEFFGGAABBCCDDEEFFGG>https://stackoverflow.com/questions/23392525
复制相似问题