通过MPI进程同步数组,如果每个线程改变了一部分? [英] Synchronize array over MPI processes, if each thread changed a part of it?

查看:80
本文介绍了通过MPI进程同步数组,如果每个线程改变了一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用 MPI 并行化的程序.我之前没有与 MPI 合作过.

I have a program I want to parallelize using MPI. I have not worked with MPI before.

该程序计算大量对象随时间的行为.的数据这些对象存储在数组中,例如double precision :: body_x(10000) x 坐标.

The program calculates the behavior for a large numer of objects over time. The data of these objects is stored in arrays, e.g. double precision :: body_x(10000) for the x coordinate.

要计算对象的行为,需要有关所有其他对象的信息,所以每个线程都需要保存所有数据,但只会更新其中的一部分.但在此之前每个线程需要从所有其他线程获取信息的新时间步.

To calculate the behavior of an object the information about all other objects is needed, so every thread needs to hold all data but will only update a portion of it. But before the new timestep every thread needs to get the information from all other threads.

据我所知 MPI_Allgather 可以用于此目的,但它需要一个发送缓冲区和一个接收缓冲区.如果每个线程更新,如何在不同线程上同步数组数组的不同部分?我是否必须将整个数组从每个线程发送到master 在接收缓冲区中,更新 masters 数组的特定部分,毕竟线程是否已从主站重新广播其数据?

As I understand MPI_Allgather could be used for this, but it needs a send buffer and a recive buffer. How can I synchronize an array over different threads if each thread updated a different part of the array? Do I have to send the whole array from each thread to the master in a recive buffer, update the specific part of the masters array and after all threads have sent their data re-broadcast from the master?

这是一个非常基本的问题,但我对 MPI 很陌生,我发现的所有示例都是非常简单,不涉及这一点.感谢您的帮助.

This is a pretty basic question, but I'm very new to MPI and all examples I found are pretty simple and do not cover this. Thanks for any help.

伪示例(假设 Fortran 样式向量的第一个索引为 1):(是的,发送/接收最好是非阻塞的,这是为了简单起见)

Pseudo-Example (assuming Fortran-Style vectors with first index 1): (Yes the send/recive would better be done non-blocking, this is for the sake of simplicity)

if (master) then
   readInputFile
end if

MPI_Bcast(numberOfObject)

allocate body_arrays(numberOfObjects)

if (master) then
   fill body_arrays ! with the data from the input file
end if

MPI_Bcast(body_arrays)

objectsPerThread = numberOfObjects / threadCount

myStart = threadID * objectsPerThread + 1
myEnd   = (threadID + 1) * objectsPerThread

do while (t < t_end)

   do i = myStart, myEnd
      do stuff for body_arrays(i)
   end do

   ! here is the question
   if (.not. master)
      MPI_Send(body_arrays, toMaster)
   else
      do i = 1, threadCount - 1
         MPI_Recive(body_arrays_recive, senderID)
         body_arrays(senderID*objectsPerThread+1, (senderId+1)*objectsPerThread) = body_arrays_recive(senderID*objectsPerThread+1, (senderId+1)*objectsPerThread)
   end if

   MPI_Bcast(body_arrays)
   ! ----

   t = t + dt
end do

推荐答案

听起来您想要 MPI_Allgather.为了避免需要单独的发送缓冲区,您可以使用 MPI_IN_PLACE 值.这告诉 MPI 对发送和接收使用相同的缓冲区.

It sounds like you want MPI_Allgather. To avoid needing a separate send buffer, you may be able to use the MPI_IN_PLACE value. That tells MPI to use the same buffer for both send and receive.

参见 http://mpi-forum.org/docs/mpi-2.2/mpi22-report/node99.htm#Node99

这篇关于通过MPI进程同步数组,如果每个线程改变了一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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