简体   繁体   English

MPI_Recv - 如何确定计数?

[英]MPI_Recv - How to determine count?

So let's say I have an MPI program with 2 processes, rank 0 and rank 1. 所以,假设我有一个包含2个进程的MPI程序,排名为0,排名为1。

int i[20], j[20], temp, size; 

In process with rank 0, I have 在排名0的过程中,我有

for(temp=0; temp<20; temp++)
    i[temp] =  temp; 
MPI_Send(i, 15, MPI_INT, 1, 1, MPI_COMM_WORLD);

and let's say process with rank 1 then does 然后让我们说排名为1的过程就可以了

// At this point, size is declared, but not assigned any value. 
MPI_Recv(j,size, MPI_INT, 0, 1, MPI_COMM_WORLD): 
cout << "I have received " << size << " elements" ; 

My question is, in the above statement, does "size" need to be declared? 我的问题是,在上述声明中,是否需要声明“大小”? Or does MPI_Recv somehow "know" that it is receiving 15 elements, and automatically sets size = 15? 或者MPI_Recv以某种方式“知道”它正在接收15个元素,并自动设置size = 15? If size is not defined, what happens to the code? 如果未定义大小,代码会发生什么?

Basically, my question is, I am sending a different number of elements to processors with different ranks, all messages originating from rank 0. I want to know if I should first send the size across, and then prepare the processors to receive that many elements, or if I can just send the array and the processes automatically pick the size up from there. 基本上,我的问题是,我向不同级别的处理器发送不同数量的元素,所有消息都来自0级。我想知道我是否应该首先发送大小,然后准备处理器以接收那么多元素,或者如果我可以发送数组,那么进程会自动从那里选择大小。

Look at the documentation for MPI_Recv : 查看MPI_Recv的文档:

Input Parameters 输入参数

count maximum number of elements in receive buffer (integer) count最大接收缓冲区中的元素数(整数)

So yes, you do need to pass in a value. 所以是的,你需要传入一个值。 Note that it need not be the actual number of elements you're receiving, but only the maximum number of elements that your buffer can hold. 请注意,它不必是您接收的元素的实际数量,而只是缓冲区可以容纳的最大元素数。 In your case, you just need to pass 20. The Notes section also mentions how to determine the actual number of elements that were received: 在您的情况下,您只需要传递20. Notes部分还提到了如何确定收到的元素的实际数量:

The count argument indicates the maximum length of a message; count参数表示消息的最大长度; the actual number can be determined with MPI_Get_count . 可以使用MPI_Get_count确定实际数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM