使用MPI_Type_create_subarray发送时可以转置数组吗?

问题描述 投票:7回答:2

我正在尝试使用C中的MPI转置矩阵。每个进程都有一个方形子矩阵,我想将其发送到正确的进程(网格上的“相反”一个),并将其作为通讯的一部分进行转置。 。

我正在使用MPI_Type_create_subarray,该参数具有该顺序的参数,分别是MPI_ORDER_CMPI_ORDER_FORTRAN分别用于行主和列主。我以为,如果我作为其中一个发送而又作为另一个接收,那么我的矩阵将作为通信的一部分转置。但是,这似乎没有发生-只是保持不移位。

代码的重要部分在下面,整个代码文件位于this gist。有谁知道为什么这行不通?这种方法应该进行移调工作吗?我已经读过MPI_ORDER_CMPI_ORDER_FORTRAN的说明,但可能不会。

/* ----------- DO TRANSPOSE ----------- */
/* Find the opposite co-ordinates (as we know it's a square) */
coords2[0] = coords[1];
coords2[1] = coords[0];

/* Get the rank for this process */
MPI_Cart_rank(cart_comm, coords2, &rank2);

/* Send to these new coordinates */

tag = (coords[0] + 1) * (coords[1] + 1);

/* Create new derived type to receive as */
/* MPI_Type_vector(rows_in_core, cols_in_core, cols_in_core, MPI_DOUBLE, &vector_type); */
sizes[0] = rows_in_core;
sizes[1] = cols_in_core;

subsizes[0] = rows_in_core;
subsizes[1] = cols_in_core;

starts[0] = 0;
starts[1] = 0;

MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_FORTRAN, MPI_DOUBLE, &send_type);
MPI_Type_commit(&send_type);

MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_C, MPI_DOUBLE, &recv_type);
MPI_Type_commit(&recv_type);


/* We're sending in row-major form, so it's just rows_in_core * cols_in_core lots of MPI_DOUBLE */
MPI_Send(&array[0][0], 1, send_type, rank2, tag ,cart_comm);

/* Receive from these new coordinates */
MPI_Recv(&new_array[0][0], 1, recv_type, rank2, tag, cart_comm, &status);
c mpi parallel-processing hpc openmpi
2个回答
8
投票

我本以为这也可以,但显然不行。

如果您在MPI标准的relevant bit中实际定义了结果类型映射,那么原因就很清楚了-MPI_Type_create_subarray映射出子数组在整个数组中所占的区域,但在内存中进行了遍历线性顺序,因此数据布局不会改变。换句话说,当大小等于子大小时,子数组只是一个连续的内存块;对于严格小于整个数组的子数组,您只是在更改要发送/接收到的子区域,而不是数据顺序。您只选择一个子区域就可以看到效果:

int sizes[]={cols,rows};
int subsizes[]={2,4};
int starts[]={1,1};

MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_FORTRAN, MPI_INT, &ftype);
MPI_Type_commit(&ftype);

MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_C, MPI_INT, &ctype);
MPI_Type_commit(&ctype);

MPI_Isend(&(send[0][0]), 1, ctype, 0, 1, MPI_COMM_WORLD,&reqc);
MPI_Recv(&(recvc[0][0]), 1, ctype, 0, 1, MPI_COMM_WORLD, &statusc);

MPI_Isend(&(send[0][0]), 1, ctype, 0, 1, MPI_COMM_WORLD,&reqf);
MPI_Recv(&(recvf[0][0]), 1, ftype, 0, 1, MPI_COMM_WORLD, &statusf);

/*...*/

printf("Original:\n");
printarr(send,rows,cols);
printf("\nReceived -- C order:\n");
printarr(recvc,rows,cols);
printf("\nReceived: -- Fortran order:\n");
printarr(recvf,rows,cols);

给您这个:

  0   1   2   3   4   5   6 
 10  11  12  13  14  15  16 
 20  21  22  23  24  25  26 
 30  31  32  33  34  35  36 
 40  41  42  43  44  45  46 
 50  51  52  53  54  55  56 
 60  61  62  63  64  65  66 

Received -- C order:
  0   0   0   0   0   0   0 
  0  11  12  13  14   0   0 
  0  21  22  23  24   0   0 
  0   0   0   0   0   0   0 
  0   0   0   0   0   0   0 
  0   0   0   0   0   0   0 
  0   0   0   0   0   0   0 

Received: -- Fortran order:
  0   0   0   0   0   0   0 
  0  11  12   0   0   0   0 
  0  13  14   0   0   0   0 
  0  21  22   0   0   0   0 
  0  23  24   0   0   0   0 
  0   0   0   0   0   0   0 
  0   0   0   0   0   0   0 

因此发送和接收相同的数据;真正发生的是数组的大小,子大小和开始被颠倒。

可以使用MPI数据类型进行转置-标准甚至给出了一个couple of examples,我在这里将其中一个音译为C-但是您必须自己创建类型。好消息是它实际上只不过是子数组的内容:

MPI_Type_vector(rows, 1, cols, MPI_INT, &col);
MPI_Type_hvector(cols, 1, sizeof(int), col, &transpose);
MPI_Type_commit(&transpose);

MPI_Isend(&(send[0][0]), rows*cols, MPI_INT, 0, 1, MPI_COMM_WORLD,&req);
MPI_Recv(&(recv[0][0]), 1, transpose, 0, 1, MPI_COMM_WORLD, &status);

MPI_Type_free(&col);
MPI_Type_free(&transpose);

printf("Original:\n");
printarr(send,rows,cols);
printf("Received\n");
printarr(recv,rows,cols);



$ mpirun -np 1 ./transpose2 
Original:
  0   1   2   3   4   5   6 
 10  11  12  13  14  15  16 
 20  21  22  23  24  25  26 
 30  31  32  33  34  35  36 
 40  41  42  43  44  45  46 
 50  51  52  53  54  55  56 
 60  61  62  63  64  65  66 
Received
  0  10  20  30  40  50  60 
  1  11  21  31  41  51  61 
  2  12  22  32  42  52  62 
  3  13  23  33  43  53  63 
  4  14  24  34  44  54  64 
  5  15  25  35  45  55  65 
  6  16  26  36  46  56  66 

0
投票

[最近发表了一篇论文,该论文利用MPI子数组数据类型,利用ALLTOALLW命令执行n-D矩阵转置。请参阅Dalcin,L.,Mortensen,M.和&Keyes,D.(2019)的使用高级MPI的快速并行多维FFT,其中包括伪代码和显式C代码。

© www.soinside.com 2019 - 2024. All rights reserved.