|
| 1 | +// testing MPI_Init_thread |
| 2 | +#include <iostream> |
| 3 | +#include <stdio.h> |
| 4 | +#include <mpi.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +int main(int argc, char **argv) { |
| 8 | + int rank, size, mpiversion, mpisubversion; |
| 9 | + int resultlen = -1, mpi_thread_supported=-1; |
| 10 | + char mpilibversion[MPI_MAX_LIBRARY_VERSION_STRING]; |
| 11 | + |
| 12 | +// -------------------------------------------------------------------------- |
| 13 | +// int MPI_Init_thread( int *argc, char ***argv, int required, int *provided ) |
| 14 | +// |
| 15 | +// { MPI_THREAD_SINGLE} |
| 16 | +// Only one thread will execute. |
| 17 | +// |
| 18 | +// { MPI_THREAD_FUNNELED} |
| 19 | +// The process may be multi-threaded, but only the main thread will make MPI calls |
| 20 | +// (all MPI calls are funneled to the main thread). |
| 21 | +// |
| 22 | +// { MPI_THREAD_SERIALIZED} |
| 23 | +// The process may be multi-threaded, and multiple threads may make MPI calls, but |
| 24 | +// only one at a time: MPI calls are not made concurrently from two distinct |
| 25 | +// threads (all MPI calls are serialized). |
| 26 | +// |
| 27 | +// { MPI_THREAD_MULTIPLE} |
| 28 | +// Multiple threads may call MPI, with no restrictions. |
| 29 | +// -------------------------------------------------------------------------- |
| 30 | + |
| 31 | +#if defined(_MPI_THREAD_SINGLE) |
| 32 | + cout << "mpi_thread_required=MPI_THREAD_SINGLE "; |
| 33 | + int ev = MPI_Init_thread( 0,0, MPI_THREAD_SINGLE, &mpi_thread_supported ); |
| 34 | +#elif defined(_MPI_THREAD_FUNNELED) |
| 35 | + cout << "mpi_thread_required=MPI_THREAD_FUNNELED "; |
| 36 | + int ev = MPI_Init_thread( 0,0, MPI_THREAD_FUNNELED, &mpi_thread_supported ); |
| 37 | +#elif defined(_MPI_THREAD_SERIALIZED) |
| 38 | + cout << "mpi_thread_required=MPI_THREAD_SERIALIZED "; |
| 39 | + int ev = MPI_Init_thread( 0,0, MPI_THREAD_SERIALIZED, &mpi_thread_supported ); |
| 40 | +#elif defined(_MPI_THREAD_MULTIPLE) |
| 41 | + cout << "mpi_thread_required=MPI_THREAD_MULTIPLE "; |
| 42 | + int ev = MPI_Init_thread( 0,0, MPI_THREAD_MULTIPLE, &mpi_thread_supported ); |
| 43 | +#else |
| 44 | + cout << "mpi_thread_required=none "; |
| 45 | + int ev = MPI_Init(0,0); |
| 46 | +#endif |
| 47 | + |
| 48 | + switch ( mpi_thread_supported ) |
| 49 | + { |
| 50 | + case MPI_THREAD_SINGLE: cout << "mpi_thread_supported=MPI_THREAD_SINGLE" ; break; |
| 51 | + case MPI_THREAD_FUNNELED: cout << "mpi_thread_supported=MPI_THREAD_FUNNELED" ; break; |
| 52 | + case MPI_THREAD_SERIALIZED: cout << "mpi_thread_supported=MPI_THREAD_SERIALIZED" ;break; |
| 53 | + case MPI_THREAD_MULTIPLE: cout << "mpi_thread_supported=MPI_THREAD_MULTIPLE" ; break; |
| 54 | + default: cout << "mpi_thread_supported=UNKNOWN" ; |
| 55 | + } |
| 56 | + |
| 57 | + // Return the level of thread support provided by the MPI library: |
| 58 | + int mpi_thread_required=-1; |
| 59 | + MPI_Query_thread( &mpi_thread_required ); |
| 60 | + switch ( mpi_thread_supported ) |
| 61 | + { |
| 62 | + case MPI_THREAD_SINGLE: cout << " mpi_thread_queried=MPI_THREAD_SINGLE " << mpi_thread_required << std::endl; break; |
| 63 | + case MPI_THREAD_FUNNELED: cout << " mpi_thread_queried=MPI_THREAD_FUNNELED " << mpi_thread_required << std::endl; break; |
| 64 | + case MPI_THREAD_SERIALIZED: cout << " mpi_thread_queried=MPI_THREAD_SERIALIZED "<< mpi_thread_required << std::endl; break; |
| 65 | + case MPI_THREAD_MULTIPLE: cout << " mpi_thread_queried=MPI_THREAD_MULTIPLE " << mpi_thread_required << std::endl; break; |
| 66 | + default: cout << " mpi_thread_queried=UNKNOWN " << mpi_thread_required << std::endl; |
| 67 | + } |
| 68 | + |
| 69 | + MPI_Get_version( &mpiversion, &mpisubversion ); |
| 70 | + MPI_Get_library_version(mpilibversion, &resultlen); |
| 71 | + printf( "# MPI-%d.%d = %s", mpiversion, mpisubversion, mpilibversion); |
| 72 | + |
| 73 | + rank = MPI::COMM_WORLD.Get_rank(); |
| 74 | + size = MPI::COMM_WORLD.Get_size(); |
| 75 | + cout << "tid=0 out of 1 from rank " << rank << " out of " << size << "\n"; |
| 76 | + |
| 77 | + //std::cout << " mpi_thread_queried=" << mpi_thread_required << std::endl; |
| 78 | + |
| 79 | + MPI::Finalize(); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} /* end func main */ |
0 commit comments