用mpi数据类型置换一个三维数组

时间:2012-10-26 19:36:55

标签: mpi

假设你有一个3d数组A [I] [J] [K],但你想将其置换为B [J] [K] [I]。

此问题类似于此处讨论的2-d数组转置问题,但不同于此: Can you transpose array when sending using MPI_Type_create_subarray?

MPI标准提供了使用用户定义的数据类型的多维数组操作的示例,但不是这种特殊情况:http://www.mpi-forum.org/docs/mpi-11-html/node61.html

1 个答案:

答案 0 :(得分:1)

执行此操作的方法是使用MPI_TYPE_VECTORMPI_TYPE_CREATE_HVECTOR,但魔鬼在细节中。

/* data[I][J][K] is I by J by K (stored in array 'dim_sizes[] = {I, J, K}'
   and we want permuted[J][K][I] */

/* new innermost dimension is I items, strided across the old JK face*/
MPI_Type_vector(dim_sizes[0], 1, dim_sizes[1]*dim_sizes[2], 
    MPI_DOUBLE, &one_d);
MPI_Type_commit(&one_d);

/* new middle dimenson is K items, strided over the K row, which isn't
 * actually a stride in this case.  We use hvector here because we 
 * operate directly in terms of array items */
MPI_Type_create_hvector(dim_sizes[2], 1, sizeof(double), one_d, &two_d);
MPI_Type_commit(&two_d);

/* new outermost dimension is J items, strided over the old J row */
MPI_Type_create_hvector(dim_sizes[1], 1, dim_sizes[2]*sizeof(double), two_d,
    &transposed_type);
MPI_Type_commit(&transposed_type);

现在,您可以将transposed_type提供给您的发送/接收电话,或将其设为您的MPI文件视图。