如何将动态数组从属节点发送到主节点 [英] How do I send a dynamic array from slave to the master node

查看:172
本文介绍了如何将动态数组从属节点发送到主节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成一个简单的MPI程序,并且在项目的最后一部分上苦苦挣扎. 我将2个包含起点和终点的int发送到从属节点.使用这些,我需要创建一个数组并填充它.我需要将此发送回主节点.从属代码如下:

I'm finishing off a simple MPI program and I'm struggling on the last part of the project. I send 2 ints containing a start point and end point to the slave node. And using these I need to create an array and populate it. I need to send this back to the Master node. Slave code below:

printf("Client waiting for start point and endpoint array\n");fflush(stdout);

int startEnd [2];

MPI_Recv(startEnd, 2, MPI_INT, 0, 100, MPI_COMM_WORLD, &status);
int end = startEnd[1];
int start = startEnd[0];
printf("Recieved Start End of %d \t %d\n", startEnd[0], startEnd[1]);fflush(stdout);

unsigned char TargetHash[MAX_HASH_LEN];
MPI_Recv(TargetHash, MAX_HASH_LEN, MPI_CHAR, 0, 100, MPI_COMM_WORLD, &status);

int sizeToCompute = (end - start);
uint64* pStartPosIndexE = new uint64[sizeToCompute];
int iterator = 0;
for (int nPos = end; nPos >= start; nPos--)
{
    cwc.SetHash(TargetHash);
    cwc.HashToIndex(nPos);
    int i;
    for (i = nPos + 1; i <= cwc.GetRainbowChainLength() - 2; i++)
    {
        cwc.IndexToPlain();
        cwc.PlainToHash();
        cwc.HashToIndex(i);
    }

    pStartPosIndexE[iterator] = cwc.GetIndex();                 
}

这是创建动态长度数组的正确方法吗,我将如何将该数组发送回主节点?

Is this the correct way to create the array of dynamic length and how would I send this array back to the master node?

推荐答案

发送动态分配的数组与发送静态数组没有什么不同.当数组大小变化时,接收代码会稍微复杂一些,但不会复杂得多:

Sending dynamically allocated arrays is no different than sending static arrays. When the array size varies, the receive code gets a bit more complicated, but not that much more complicated:

// ---------- Sender code ----------

MPI_Send(pStartPosIndexE, sizeToCompute, MPI_UINT64, 99, ...);

// --------- Receiver code ---------

// Wait for a message with tag 99
MPI_Status status;
MPI_Probe(MPI_ANY_SOURCE, 99, MPI_COMM_WORLD, &status);

// Get the number of elements in the message
int nElems;
MPI_Get_elements(&status, MPI_UINT64_T, &nElems);

// Allocate buffer of appropriate size
uint64 *result = new uint64[nElems];

// Receive the message
MPI_Recv(result, nElems, MPI_UINT64_T, status.MPI_SOURCE, 99, ...);

使用MPI_ProbeMPI_ANY_SOURCE的源等级通常是在主/工作者应用程序中执行的,在该应用程序中,工作者是按照先到先得的方式处理的.

Using MPI_Probe with source rank of MPI_ANY_SOURCE is what is usually done in master/worker applications where workers are processed on a first-come-first-served basis.

这篇关于如何将动态数组从属节点发送到主节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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