@@ -1487,6 +1487,27 @@ This simulation will continue using the HIP runtime...\n", runtime_type, HIP, CO
14871487// CUDA-aware MPI
14881488// we need to call cudaSetDevice before MPI_Init to ensure that the same GPU is chosen by MPI and your application
14891489
1490+ // CUDA-aware support compile-time information
1491+ #ifdef WITH_CUDA_AWARE_MPI
1492+ // rank info
1493+ #if defined(OPEN_MPI ) && OPEN_MPI
1494+ // OpenMPI
1495+ #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI uses OPEN_MPI CUDA-aware local rank\n")
1496+ #elif defined(MVAPICH2_NUMVERSION ) && (MVAPICH2_NUMVERSION >= 20205300 )
1497+ // MVAPICH
1498+ #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI uses MVAPICH2 CUDA-aware local rank\n")
1499+ #else
1500+ // unknown
1501+ #pragma message ("\n\nCompiling with: unknown CUDA-aware local rank environment, use -DENV_LOCAL_RANK \"<MY_LOCAL_RANK>\" setting\n")
1502+ #endif
1503+ // MPIX query
1504+ #if defined(MPIX_CUDA_AWARE_SUPPORT )
1505+ #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI has MPIX_CUDA_AWARE_SUPPORT\n")
1506+ #else
1507+ #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI has no MPIX_CUDA_AWARE_SUPPORT, please check MPI installation\n")
1508+ #endif // MPIX_CUDA_AWARE_SUPPORT
1509+ #endif // WITH_CUDA_AWARE_MPI
1510+
14901511extern EXTERN_LANG
14911512void FC_FUNC_ (check_cuda_aware_mpi ,
14921513 CHECK_CUDA_AWARE_MPI ) (int * has_cuda_aware_mpi_f ) {
@@ -1507,23 +1528,17 @@ void FC_FUNC_ (check_cuda_aware_mpi,
15071528 // for Open MPI, use "OMPI_COMM_WORLD_LOCAL_RANK".
15081529#if defined(OPEN_MPI ) && OPEN_MPI
15091530// OpenMPI
1510- #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI uses OPEN_MPI CUDA-aware local rank\n")
15111531#define ENV_LOCAL_RANK "OMPI_COMM_WORLD_LOCAL_RANK"
1512-
15131532#elif defined(MVAPICH2_NUMVERSION ) && (MVAPICH2_NUMVERSION >= 20205300 )
15141533// MVAPICH
1515- #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI uses MVAPICH2 CUDA-aware local rank\n")
15161534#define ENV_LOCAL_RANK "MV2_COMM_WORLD_LOCAL_RANK"
1517-
15181535#else
15191536// unknown
1520- #pragma message ("\n\nCompiling with: unknown CUDA-aware local rank environment, use -DENV_LOCAL_RANK \"<MY_LOCAL_RANK>\" setting\n")
15211537// defines local rank environment variables as unknown if not set by compilation flag, mostly to be able to run getenv() command
15221538#ifndef ENV_LOCAL_RANK
15231539#define ENV_LOCAL_RANK "UNKNOWN_LOCAL_RANK"
15241540#endif
1525-
1526- #endif
1541+ #endif // OPEN_MPI
15271542
15281543 // sets GPU device before MPI initialization
15291544 // MPI will then recognize the setting and take over the GPU device setup
@@ -1536,6 +1551,9 @@ void FC_FUNC_ (check_cuda_aware_mpi,
15361551 int rank = 0 ;
15371552 char * localRankStr = NULL ;
15381553
1554+ // info
1555+ const char * msg ;
1556+
15391557 // local rank info from environment
15401558 if ((localRankStr = getenv (ENV_LOCAL_RANK )) != NULL ) {
15411559 // catching OpenMPI environment rank
@@ -1559,28 +1577,27 @@ void FC_FUNC_ (check_cuda_aware_mpi,
15591577 //printf("debug: compile time check for CUDA-aware MPI - rank %d\n",rank);
15601578
15611579#if defined(MPIX_CUDA_AWARE_SUPPORT )
1562- #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI has MPIX_CUDA_AWARE_SUPPORT\n")
1563- int ret = MPIX_Query_cuda_support ();
1564- if (ret == 1 ) {
1565- // MPI library has CUDA-aware support
1566- has_cuda_aware_mpi = 1 ;
1567- } else {
1568- // MPI library does not have CUDA-aware support
1569- has_cuda_aware_mpi = 0 ;
1570- }
1580+ // assume CUDA-aware support, will run query later to double-check
1581+ has_cuda_aware_mpi = 1 ;
1582+ msg = "\
1583+ This version has been compiled with flag WITH_CUDA_AWARE_MPI and MPI library has MPIX query cuda support.\n \
1584+ We assume CUDA-aware MPI.\n\n" ;
15711585#else
1572- #pragma message ("\n\nCompiling with: WITH_CUDA_AWARE_MPI has no MPIX_CUDA_AWARE_SUPPORT, please check MPI installation\n")
1573- // user info
1574- if (rank == 0 ){
1575- printf ("\
1576- This version has been compiled with flag WITH_CUDA_AWARE_MPI, but MPI library cannot determine if there is CUDA-aware support.\n \
1577- Please check MPI installation.\n\n" );
1578- }
1586+ // no MPIX query support
15791587 has_cuda_aware_mpi = 0 ;
1588+ msg = "\
1589+ This version has been compiled with flag WITH_CUDA_AWARE_MPI, but MPI library cannot determine if there is CUDA-aware support.\n \
1590+ Please check MPI installation.\n\n" ;
1591+ //debug
1592+ //printf("[check_cuda_aware_mpi] rank = %d - no MPIX has cuda aware MPI = %d\n",rank,has_cuda_aware_mpi);
15801593#endif // MPIX_CUDA_AWARE_SUPPORT
15811594
1595+ // user info
1596+ if (rank == 0 ){
1597+ printf ("%s" ,msg );
1598+ }
15821599 // debug
1583- //printf("debug: query cuda support: MPI library CUDA-aware support - rank %d has support %d\n\n",rank,has_cuda_aware_mpi);
1600+ //printf("[check_cuda_aware_mpi] query cuda support: MPI library CUDA-aware support - rank %d has support %d\n\n",rank,has_cuda_aware_mpi);
15841601
15851602 // sets local rank's GPU association
15861603 if (has_cuda_aware_mpi ){
@@ -1606,3 +1623,72 @@ Please check MPI installation.\n\n");
16061623 // return value
16071624 * has_cuda_aware_mpi_f = has_cuda_aware_mpi ;
16081625}
1626+
1627+ /* ----------------------------------------------------------------------------------------------- */
1628+
1629+ // note: MPIX_Query_cuda_support() can only be called after MPI_init().
1630+ // However, for CUDA-aware MPI, we must set the device with cudaSetDevice() before MPI_init().
1631+ // Thus, we use a compile-time check for MPIX_CUDA_AWARE_SUPPORT to see if there is CUDA-aware MPI support.
1632+ // And then use the query here after setting device and MPI initialization as a validity check of the loaded MPI system.
1633+
1634+ extern EXTERN_LANG
1635+ void FC_FUNC_ (query_cuda_aware_mpi ,
1636+ QUERY_CUDA_AWARE_MPI ) (int * myrank_f , int * has_cuda_aware_mpi_f ) {
1637+
1638+ TRACE ("query_cuda_aware_mpi" );
1639+
1640+ // rank
1641+ int myrank = * myrank_f ;
1642+
1643+ // flags
1644+ int has_cuda_aware_mpi = 0 ;
1645+
1646+ #ifdef WITH_CUDA_AWARE_MPI
1647+ // debug output to file
1648+ char filename [BUFSIZ ];
1649+ FILE * fp ;
1650+ const char * msg ;
1651+
1652+ sprintf (filename ,"./OUTPUT_FILES/gpu_aware_info.txt" );
1653+
1654+ #if defined(MPIX_CUDA_AWARE_SUPPORT )
1655+ int ret = MPIX_Query_cuda_support ();
1656+ if (ret == 1 ) {
1657+ // MPI library loaded has CUDA-aware support
1658+ has_cuda_aware_mpi = 1 ;
1659+ msg = "CUDA-aware support is available. MPIX query returned CUDA support.\n" ;
1660+ } else {
1661+ // MPI library does not have CUDA-aware support
1662+ has_cuda_aware_mpi = 0 ;
1663+ msg = "CUDA-aware support is disabled or unavailable. MPIX query returned no CUDA support.\n" ;
1664+ }
1665+ //debug
1666+ //printf("[query_cuda_aware_mpi] myrank = %d - MPIX query: has cuda aware MPI = %d\n",myrank,has_cuda_aware_mpi);
1667+ #else
1668+ // no MPIX query support
1669+ has_cuda_aware_mpi = 0 ;
1670+ msg = "\
1671+ This version has been compiled with flag WITH_CUDA_AWARE_MPI, but MPI library cannot determine if there is CUDA-aware support.\n \
1672+ Please check MPI installation.\n" ;
1673+ //debug
1674+ //printf("[query_cuda_aware_mpi] myrank = %d - no MPIX has cuda aware MPI = %d\n",myrank,has_cuda_aware_mpi);
1675+ #endif // MPIX_CUDA_AWARE_SUPPORT
1676+
1677+ // user info
1678+ if (myrank == 0 ){
1679+ printf ("%s" ,msg );
1680+ }
1681+
1682+ // file output
1683+ if (myrank == 0 ){
1684+ fp = fopen (filename ,"w" );
1685+ if (fp != NULL ){
1686+ fprintf (fp , "%s" , msg );
1687+ fclose (fp );
1688+ }
1689+ }
1690+ #endif // WITH_CUDA_AWARE_MPI
1691+
1692+ // return value
1693+ * has_cuda_aware_mpi_f = has_cuda_aware_mpi ;
1694+ }
0 commit comments