问题
Is it possible to have one function to wrap both MPI_Init
and MPI_Init_thread
? The purpose of this is to have a cleaner API while maintaining backward compatibility. What happens to a call to MPI_Init_thread
when it is not supported by the MPI run time? How do I keep my wrapper function working for MPI implementations when MPI_Init_thread
is not supported?
回答1:
MPI_INIT_THREAD
is part of the MPI-2.0 specification, which was released 15 years ago. Virtually all existing MPI implementations are MPI-2 compliant except for some really archaic ones. You might not get the desired level of thread support, but the function should be there and you should still be able to call it instead of MPI_INIT
.
You best and most portable option is to have a configure
-like mechanism probe for MPI_Init_thread
in the MPI library, e.g. by trying to compile a very simple MPI program and see if it fails with an unresolved symbol reference, or you can directly examine the export table of the MPI library with nm
(for archives) or objdump
(for shared ELF objects). Once you've determined that the MPI library has MPI_Init_thread
, you can have a preprocessor symbol defined, e.g. CONFIG_HAS_INITTHREAD
. Then have your wrapped similar to this one:
int init_mpi(int *pargc, char ***pargv, int desired, int *provided)
{
#if defined(CONFIG_HAS_INITTHREAD)
return MPI_Init_thread(pargc, pargv, desired, provided);
#else
*provided = MPI_THREAD_SINGLE;
return MPI_Init(pargc, pargv);
#endif
}
Of course, if the MPI library is missing MPI_INIT_THREAD
, then MPI_THREAD_SINGLE
and the other thread support level constants will also not be defined in mpi.h
, so you might need to define them somewhere.
来源:https://stackoverflow.com/questions/16226646/mpi-one-function-for-mpi-init-and-mpi-init-thread