MPI在结构体中发送/接收多个可变长度数组

4

我需要能够从排名0发送和接收两个uint64_t数组到所有排名。

我有一个如下的typedef结构:

typedef struct _mpi_data_t {
    uint64_t *array1;
    uint64_t *array2;
    size_t array1_size;
    size_t array2_size;
} mpi_data_t;

在rank 0中,我填充数组并计算位移以创建MPI数据类型。

MPI_Get_address(&mpi_data_send, &address[0]);
MPI_Get_address(&mpi_data_send.array1[0], &address[1]);
MPI_Get_address(&mpi_data_send.array2[0], &address[2]);
MPI_Get_address(&mpi_data_send.array1_size, &address[3]);
displacements[0] = address[1] - address[0];
displacements[1] = address[2] - address[0];
displacements[2] = address[3] - address[0]; 

我将块计数数组设置如下:

我也将块计数数组设置如下:

int block_count[3] = {mpi_data_send.array1_size, mpi_data_send.array2_size, 2};

我的问题是,当接收到这种数据类型时,其他进程无法使用相同的位移和块计数创建相同的数据类型规范,因为数组是由发送方(rank 0)生成的。

我该如何发送和接收这两个动态大小的数组?

1个回答

1
您可以使用MPI_ProbeMPI_Get_count来查找接收消息的长度。
在您的代码中没有提供发送和接收的格式,但作为建议,您可以单独发送数组并接收它们。 这个C示例可能会有所帮助:
// Author: Wes Kendall
// Copyright 2011 www.mpitutorial.com
// This code is provided freely with the tutorials on mpitutorial.com. Feel
// free to modify it for your own use. Any distribution of the code must
// either provide a link to www.mpitutorial.com or keep this header intact.
//
// Example of using MPI_Probe to dynamically allocated received messages
//
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char** argv) {
  MPI_Init(NULL, NULL);

  int world_size;
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  if (world_size != 2) {
    fprintf(stderr, "Must use two processes for this example\n");
    MPI_Abort(MPI_COMM_WORLD, 1);
  }
  int world_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

  int number_amount;
  if (world_rank == 0) {
    const int MAX_NUMBERS = 100;
    int numbers[MAX_NUMBERS];
    // Pick a random amont of integers to send to process one
    srand(time(NULL)+world_rank);
    number_amount = (rand() / (float)RAND_MAX) * MAX_NUMBERS;
    // Send the amount of integers to process one
    MPI_Send(numbers, number_amount, MPI_INT, 1, 0, MPI_COMM_WORLD);
    printf("0 sent %d numbers to 1\n", number_amount);
  } else if (world_rank == 1) {
    MPI_Status status;
    // Probe for an incoming message from process zero
    MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
    // When probe returns, the status object has the size and other
    // attributes of the incoming message. Get the size of the message.
    MPI_Get_count(&status, MPI_INT, &number_amount);
    // Allocate a buffer just big enough to hold the incoming numbers
    int* number_buf = (int*)malloc(sizeof(int) * number_amount);
    // Now receive the message with the allocated buffer
    MPI_Recv(number_buf, number_amount, MPI_INT, 0, 0, MPI_COMM_WORLD,
         MPI_STATUS_IGNORE);
    printf("1 dynamically received %d numbers from 0.\n",
       number_amount);
    free(number_buf);
  }
  MPI_Finalize();
}

在这个例子中,随机长度的数组(小于100)在rank 0中分配并发送到rank 1。

1
谢谢!那非常有帮助。我已经拿了你的示例代码并成功地应用到我的代码中了。 - user1818923

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接