C mpi发送/接收结构中的多个可变长度数组

C mpi发送/接收结构中的多个可变长度数组,c,mpi,mpich,C,Mpi,Mpich,我需要能够从秩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; int block_count[3] = {mpi_data_send.array1_size, mpi_data_send.a

我需要能够从秩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;
int block_count[3] = {mpi_data_send.array1_size, mpi_data_send.array2_size, 2};
在秩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]; 
我还设置了块计数数组,如下所示:

typedef struct _mpi_data_t {
    uint64_t *array1;
    uint64_t *array2;
    size_t array1_size;
    size_t array2_size;
} mpi_data_t;
int block_count[3] = {mpi_data_send.array1_size, mpi_data_send.array2_size, 2};
我的问题是,当接收此数据类型时,其他列组不可能创建具有相同位移和块计数的相同数据类型规范,因为数组是由列组0(发送方)生成的


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

您可以使用
MPI\u Probe
MPI\u Get\u 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();
}
//作者:韦斯·肯德尔
//版权所有2011 www.mpitatorial.com
//此代码随mpittutorial.com上的教程免费提供。感觉
//您可以自由修改它以供自己使用。任何代码的分发都必须
//请提供指向www.mpittutorial.com的链接或保持此标题完整。
//
//使用MPI_探测器动态分配接收消息的示例
//
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
MPI_Init(NULL,NULL);
国际大世界;
MPI_Comm_大小(MPI_Comm_WORLD和WORLD_大小);
如果(世界大小!=2){
fprintf(stderr,“本例必须使用两个进程\n”);
MPI_中止(MPI_通信世界,1);
}
国际世界排名;
MPI通信等级(MPI通信世界级和世界级);
整数金额;
如果(世界排名==0){
常数int MAX_数=100;
整数[最大值];
//随机选取一组整数发送到进程1
srand(时间(空)+世界排名);
数量\金额=(兰德()/(浮动)兰德\最大)*最大数量;
//将整数的数量发送到进程1
MPI_发送(数字、数字金额、MPI_INT、1、0、MPI_通信世界);
printf(“0向1发送了%d个数字”,数字\金额);
}else if(世界排名==1){
MPI_状态;
//探测来自进程0的传入消息
MPI_探测器(0、0、MPI_通信世界和状态);
//当探测器返回时,状态对象具有大小和其他
//传入消息的属性。获取消息的大小。
MPI获取计数(状态、MPI整数和数量金额);
//分配一个足够大的缓冲区来容纳传入的数字
整数*数字*金额=(整数*)malloc(整数*数字*金额);
//现在使用分配的缓冲区接收消息
MPI记录(编号buf、编号金额、MPI INT、0、0、MPI通信世界、,
MPI_状态_忽略);
printf(“1动态接收到来自0的%d个数字。\n”,
数量(单位金额);
免费(数量);
}
MPI_Finalize();
}

在本例中,随机长度数组(小于100)分配到秩0并发送到秩1。

谢谢!这很有帮助。我已经获取了您的示例代码,并在我的代码中成功地应用了它。