C MPI多动态数组传递 [英] C MPI multiple dynamic array passing

查看:84
本文介绍了C MPI多动态数组传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试ISend()两个数组:arr1,arr2和一个整数n,它是arr1,arr2的大小.我从此帖子了解到,发送了一个结构不能全部包含这三个选项,因为n仅在运行时才知道.显然,我需要先接收n,因为否则接收过程将不知道要接收多少个元素.不使用冗长的Send()来实现此目标的最有效方法是什么?

I'm trying to ISend() two arrays: arr1,arr2 and an integer n which is the size of arr1,arr2. I understood from this post that sending a struct that contains all three is not an option since n is only known at run time. Obviously, I need n to be received first since otherwise the receiving process wouldn't know how many elements to receive. What's the most efficient way to achieve this without using the blokcing Send() ?

推荐答案

发送数组的大小是多余的(并且效率很低),因为MPI提供了一种探查传入消息而不接收消息的方法,该消息仅按顺序提供了足够的信息正确分配内存.使用MPI_PROBE进行探测,该外观与MPI_RECV非常相似,不同之处在于它不使用与缓冲区相关的参数.探测操作返回一个状态对象,然后可以查询该对象以获取给定MPI数据类型的元素数量,该元素可以使用MPI_GET_COUNT从消息的内容中提取,因此显式发送元素数量变得多余.

Sending the size of the array is redundant (and inefficient) as MPI provides a way to probe for incoming messages without receiving them, which provides just enough information in order to properly allocate memory. Probing is performed with MPI_PROBE, which looks a lot like MPI_RECV, except that it takes no buffer related arguments. The probe operation returns a status object which can then be queried for the number of elements of a given MPI datatype that can be extracted from the content of the message with MPI_GET_COUNT, therefore explicitly sending the number of elements becomes redundant.

这是一个具有两个等级的简单示例:

Here is a simple example with two ranks:

if (rank == 0)
{
    MPI_Request req;

    // Send a message to rank 1
    MPI_Isend(arr1, n, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &req);
    // Do not forget to complete the request!
    MPI_Wait(&req, MPI_STATUS_IGNORE);
}
else if (rank == 1)
{
    MPI_Status status;

    // Wait for a message from rank 0 with tag 0
    MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
    // Find out the number of elements in the message -> size goes to "n"
    MPI_Get_count(&status, MPI_DOUBLE, &n);
    // Allocate memory
    arr1 = malloc(n*sizeof(double));
    // Receive the message. ignore the status
    MPI_Recv(arr1, n, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

MPI_PROBE也接受通配符等级MPI_ANY_SOURCE和通配符标签MPI_ANY_TAG.然后,可以查询状态结构中的相应条目,以找出实际的发件人等级和实际的消息标签.

MPI_PROBE also accepts the wildcard rank MPI_ANY_SOURCE and the wildcard tag MPI_ANY_TAG. One can then consult the corresponding entry in the status structure in order to find out the actual sender rank and the actual message tag.

探测消息大小的方法是,每条消息都带有一个称为 envelope 的标头.信封由发送者等级,接收者等级,消息标签和通信者组成.它还包含有关总消息大小的信息.在两个通信过程之间,信封是作为初始握手的一部分发送的.

Probing for the message size works as every message carries a header, called envelope. The envelope consists of the sender's rank, the receiver's rank, the message tag and the communicator. It also carries information about the total message size. Envelopes are sent as part of the initial handshake between the two communicating processes.

这篇关于C MPI多动态数组传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆