MeDiPack is a Message Differentiation Package. The goal is to provide a complete wrapper implementation for MPI such that an Algorithmic Differentiation (AD) tool can be used in all communication routines. For an introduction to an AD tool see e.g. CoDiPack.
MeDiPack is designed such that the integration into an existing application should pose no major problems. It is assumed here, that the application is already able to use an AD tool for the generation of derivative results. Thus only the additional steps for the MeDiPack integration are discussed here. The general steps for the interation of MeDiPack are:
The download of MeDiPack is available from the github web page of MeDiPack or it can be directly cloned from github:
In order to be able to include the header and source file the compiler command line needs to be extended by:
Now the MeDiPack header can be included in a global header file:
The rename of MPI_ to AMPI_ can be performed with the regular expression "\bMPI_" -> "AMPI_". This should find all uses of MPI in you files and replace them with the MeDiPack replacement. This change is necessary since MeDiPack needs to modify the default data structures of MPI. The AMPI methods are using this data structure and call the appropriate version of the MPI method. If the method does not need any handling for the AD tool it is directly forwarded to the MPI call. If a special treatment for AD is required the additional operations are performed before and/or after the MPI method is called.
The next step is the initialization of the AD tool. It needs to be done after MPI is initialized. Usually the AD tool provides an implementation of the medi::ADToolInterface from MeDiPack. For CoDiPack the interfaces are provided in the tools/mpi folder.
The constructor call MpiTypes()
will create all the specifics for the AD tool. The MPI datatype is then initialized and can be used to send AD variables over the network. For a simple send/recv pair the code is:
For all MPI functions and variables the AMPI replacements are used. Otherwise the only other change is the use of the MPI data type provided by the AD tool. mpiTypes->MPI_TYPE
provides all information for MeDiPack such that the special handling for AD can be executed.
During the compilation of a MPI application that uses MeDiPack you might get errors of the kind:
MeDiPack changes the definitions of the MPI functions such that they are type safe. The type given by AMPI_INT
will force the corresponding buffer to be of the type int*
. If that is not the case errors like the one above are generated.
This type checking is only possible if the direct type definitions of the AD tools or the default MPI types are used. If the above line is changed to:
the type checking can no longer be performed. This is also true if custom user types are used in MeDiPack.
There are some additional options that can be used to configure MeDiPack on a global level:
-DMEDI_NO_CONST_SEND
, MeDiPack can be configured to adhere to this specification.MPI_VERSION
and MPI_SUBVERSION
. If these macros are not available the default is 3.1. The default and detection can be overwritten by setting the macro MEDI_MPI_TARGET
to MMmm where MM is the major version and mm the minor version. For MPI 3.1 this would be 301.The complete code for this tutorial is: