简体   繁体   English

MIP 中是如何建立来回通信的?

[英]How is back-and-forth communication established in MIP?

For example, I have a root process which sends some computations to be completed by worker processes.例如,我有一个根进程,它发送一些要由工作进程完成的计算。 But because I have limited ( 4 ) processes I have to share the workload to all of them so I send multiple times.但是因为我有有限的( 4 )个进程,所以我必须将工作量分摊给所有人,所以我多次发送。 The workaround that I have found is this:我发现的解决方法是这样的:


    int me = MPI.COMM_WORLD.Rank();

    if(me == 0) {
        sendToWorkers(); //Sends more than once to workers.
    }
    else {
        while(true) {//wait indefinitely, accept data received from root process and work on it.
            MPI.COMM_WORLD.Recv(Buf, 0, Buf.length, MPI.INT, 0, 0);
            doTask(Buf);
        }
    }

Now the problem arises that I want to send data that has completed processing back to the root process but I can't do another while(true);现在问题来了,我想将已经完成处理的数据发送回根进程,但我不能再做一次while(true); . . I am sure there must be a much more elegant way to accomplish this.我相信必须有一种更优雅的方式来实现这一点。

EDIT 1: The reason why I want to send to root process is because it is cleaner.编辑 1:我想发送到 root 进程的原因是因为它更干净。 However, alternatively I can just print computed solutions from the worker processes but the output is all mangled up due to interleaving.但是,或者我可以从工作进程打印计算出的解决方案,但由于交错,输出都被破坏了。 Declaring the print method to be synchronized doesn't work.声明要synchronizedprint方法不起作用。

One simple solution is at the end of task distribution, master must sent a "FINISH/STOP/END" (any custom message to indicate that tasks are over) message to all workers.一个简单的解决方案是在任务分发结束时,master 必须向所有 worker 发送"FINISH/STOP/END" (任何自定义消息以指示任务结束)消息。 Workers getting the finish message exits the loop and sends the result back to the master.获得完成消息的工人退出循环并将结果发送回主站。 Master can start a loop with total tasks and waits for those results. Master 可以启动一个包含所有任务的循环并等待这些结果。

From your example shown, this is a typical master worker model use-case.从您显示的示例中,这是一个典型的主工作模型用例。 Here when you send a task to a worker using MPI_Send() , there is a corresponding MPI_Recv() in your worker process.在这里,当您使用MPI_Send()向工作人员发送任务时,工作MPI_Recv()有相应的MPI_Recv() After receiving task, you perform doTask(Buf) .收到任务后,执行doTask(Buf) Then you again goes to the loop.然后你再次进入循环。 So in your case, to summarise, you receive a new task only after computing the previously received task for that rank right?因此,在您的情况下,总而言之,您只有在计算了该等级的先前收到的任务后才会收到新任务,对吗? In that case, master process can also wait for reply from any of the finished tasks and can send new tasks based on that.在这种情况下,主进程也可以等待任何已完成任务的回复,并可以基于此发送新任务。 May be you can consider that approach.也许你可以考虑这种方法。 If your doTask uses thread, then this becomes complicated.如果您的doTask使用线程,那么这将变得复杂。 Each worker nodes then haves to keep track of its tasks and after all the tasks are completed, master should start a loop and waits for the results.然后每个工作节点必须跟踪其任务,在所有任务完成后,主节点应该启动一个循环并等待结果。

Or you can use multithreaded implementation.或者您可以使用多线程实现。 You may use separate thread for send and receive in master.您可以在 master 中使用单独的线程进行发送和接收。

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

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