I have a simple C++ struct that basically wraps a standard C array:
struct MyArray {
T* data;
int length;
// ...
}
where T
is a numeric type like float
or double
. length
is the number of elements in the array. Typically my arrays are very large (tens of thousands up to tens of millions of elements).
I have an MPI program where I would like to expose two instances of MyArray
, say a_old
and a_new
, as shared memory objects via MPI 3 shared memory. The context is that each MPI rank reads from a_old
. Then, each MPI rank writes to certain indices of a_new
(each rank only writes to its own set of indices - no overlap). Finally, a_old = a_new
must be set on all ranks. a_old
and a_new
are the same size. Right now I'm making my code work by syncing (Isend/Irecv
) each rank's updated values with other ranks. However, due to the data access pattern, there's no reason I need to incur the overhead of message passing and could instead have one shared memory object and just put a barrier before a_old = a_new
. I think this would give me better performance (though please correct me if I'm wrong).
I have had trouble finding complete code examples of doing shared memory with MPI 3. Most sites only provide reference documentation or incomplete snippets. Could someone walk me through a simple and complete code example that does the sort of thing I'm trying to achieve (updating and syncing a numeric array via MPI shared memory)? I understand the main concepts of creating shared memory communicators and windows, setting fences, etc., but it would really help my understanding to see one example that puts it all together.
Also, I should mention that I'll only be running my code on one node, so I don't need to worry about needing multiple copies of my shared-memory object across nodes; I just need one copy of my data for the single node on which my MPI processes are running. Despite this, other solutions like OpenMP aren't feasible for me in this case, since I have a ton of MPI code and can't rewrite everything for the sake of one or two arrays I'd like to share.
Here is a code that feeds your description. In comments I put little descriptions about the code. Generally its presenting a dynamic RMA Window and the memory has to be allocated and at to the window.
MPI_Win_lock_all(0, win)
Description from Open MPI Documentation:Starts an RMA access epoch to all processes in win, with a lock type of MPI_LOCK_SHARED. During the epoch, the calling process can access the window memory on all processes in win by using RMA operations.
Where I have used
MPI_INFO_NULL
you can use an MPI_Info object to provide additional information to MPI but it is depends on your memory access pattern.Using shared memory with MPI-3 is relatively simple.
First, you allocate the shared memory window using
MPI_Win_allocate_shared
:Here, only rank 0 allocates memory. It doesn't really matter which process allocates it as it is shared. It is even possible to have each process allocate a portion of the memory, but since by the default the allocation is contiguous, both methods are equivalent.
MPI_Win_shared_query
is then used by all other processes to find out the location in their virtual address space of the beginning of the shared memory block. That address might vary among the ranks and therefore one should not pass around absolute pointers.You can now simply load from and store into
a_old.data
respectivelya_new.data
. As the ranks in your case work on disjoint sets of memory locations, you don't really need to lock the window. Use window locks to implement e.g. protected initialisation ofa_old
or other operations that require synchronisation. You might also need to explicitly tell the compiler not to reorder the code and to emit a memory fence in order to have all outstanding load/store operations finished before e.g. you callMPI_Barrier()
.The
a_old = a_new
code suggests copying one array onto the other. Instead, you could simply swap the data pointers and eventually the size fields. Since only the data of the array is in the shared memory block, swapping the pointers is a local operation, i.e. no synchronisation needed. Assuming that both arrays are of equal length:You still need a barrier to make sure that all other processes have finished processing before continuing further.
At the very end, simply free the window:
A complete example (in C) follows:
Disclaimer: Take this with a grain of salt. My understanding of MPI's RMA is still quite weak.