2

What I need to do is to rotate 2-d array by 90 degrees (3x3 on 3 processors, 4x4 on 4 etc. ) using derived datatypes in MPI. I found that using Alltoall function in C on array:

[ 1][ 2][ 3][ 4]
[ 5][ 6][ 7][ 8]
[ 9][10][11][12]
[13][14][15][16]

I'll get data distributed like this:

1:[ 1][ 5][ 9][13]
2:[ 2][ 6][10][14]
3:[ 3][ 7][11][15]
4:[ 4][ 8][12][16]

What should I do next (what steps should I take) to collect this vectors as an array on one processor (root) in proper order (order that reflects 90 degrees rotation)?

Thanks in advance.

4

2 回答 2

2

所以我终于弄清楚了应该如何使用AlltoallvGather函数来完成。
Alltoallv让我以相反的顺序在进程之间分配数据:

0:[ 4][ 8][12][16]
1:[ 3][ 7][11][15]
2:[ 2][ 6][10][14]
3:[ 1][ 5][ 9][13]

然后我用来Gather将进程上的数据收集rank 0到缓冲区中:

...
//size is a number of processors
MPI_Type_vector(size, 1,1, MPI_INT, &vec ); 
MPI_Type_commit( &vec );

if(rank==0){
int buffer[size*size];
//recv is a name of an array with data on every processor
MPI_Gather(recv,1,vec, buffer, size, MPI_INT, 0, MPI_COMM_WORLD );
}else{
MPI_Gather(recv,1,vec,NULL,0,MPI_INT,0,MPI_COMM_WORLD);
}
...

结果我收到:

[ 4][ 8][12][16]
[ 3][ 7][11][15]
[ 2][ 6][10][14]
[ 1][ 5][ 9][13]
于 2013-11-06T10:29:57.637 回答
1

您要查找的操作称为Transpose 我会给您伪代码以将长度为 N 的方阵转置 N

int matrix[N][N];

for (n=0; n<N-1; ++n)
    for (m=n+1; m<N; ++m)
        swap matrix[n][m] with matrix[m][n]

这非常有效,因为它可以就地执行操作。然而,正如我所说,这是一个方阵。您可能可以使用它来弄清楚如何为矩形矩阵执行此操作。

于 2013-10-18T16:43:34.313 回答