ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
hydra.m
Go to the documentation of this file.
1%> \brief
2%> Return the MPI image count and the current image ID (e.g., MPI rank + 1)
3%> if the application has been launched via a Hydra-based MPI ``mpiexec`` launcher.<br>
4%>
5%> \details
6%> This function attempts to detect the invocation of the current MATLAB
7%> session via a Hydra-based MPI ``mpiexec`` binary MPI launcher instance.<br>
8%> The runtime detection is achieved by checking for runtime environment
9%> variables that a Hydra-based ``mpiexec`` binary executable defines upon launch.<br>
10%>
11%> Specifically, a Hydra-basedMPI provides an informal list of
12%> environment variables that are defined on every MPI process.<br>
13%> These variables may not be defined on all platforms.<br>
14%> Some are only defined at runtime, while others are defined at compile-time.<br>
15%>
16%> <ol>
17%> <li> On **Unix systems**, the best approach to investigate the Hydra-MPI-specific runtime variables
18%> is to launch a Hydra-based ``mpiexec`` binary executable with the ``printenv`` Unix command.<br>
19%> \code{.sh}
20%> mpiexec -np 1 printenv
21%> \endcode
22%> To see the compile-time variables and compare them with the runtime list from the above, try:<br>
23%> \code{.sh}
24%> printenv
25%> \endcode
26%> <li> On **Windows systems**, the best approach to investigate the Hydra-specific runtime variables
27%> is to launch a Hydra-based ``mpiexec`` binary executable with the ``set`` Windows batch command.<br>
28%> \code{.bat}
29%> mpiexec -n 1 cmd /k set
30%> \endcode
31%> To see the compile-time variables and compare them with the runtime list from the above, try:<br>
32%> \code{.bat}
33%> set
34%> \endcode
35%> Note that as of 2024, the only ParaMonte-compatible Hydra-based
36%> MPI library on Windows is the Intel MPI library.<br>
37%> </ol>
38%>
39%> The most persistent environment variables for Hydra-based ``mpiexec`` binary appear to be the following:<br>
40%>
41%> <ol>
42%> <li> ``PMI_RANK`` : The rank of the current process in the current MPI communication world.<br>
43%> <li> ``PMI_SIZE`` : The size (i.e., number of images/processes) of the current MPI communication world.<br>
44%> <li> ``MPI_LOCALRANKID`` : The **local** rank of the current process relative to the local host.<br>
45%> This environment variable may not be defined on all platforms (e.g., Windows) by all Hydra-based MPI libraries.<br>
46%> <li> ``MPI_LOCALNRANKS`` : The size (i.e., number of images/processes) of the current MPI communication on the local host.<br>
47%> This environment variable may not be defined on all platforms (e.g., Windows) by all Hydra-based MPI libraries.<br>
48%> </ol>
49%>
50%> \return
51%> ``nproc`` : The output scalar MATLAB non-negative whole number containing
52%> the number of MPI images in the current invocation of a
53%> Hydra-based MPI ``mpiexec`` binary MPI launcher.<br>
54%> An output value of ``0`` implies the algorithm failed to detect
55%> a ParaMonte-compatible Hydra-based MPI library or failed to infer the image count.<br>
56%> ``rankp1`` : The output scalar MATLAB non-negative whole number containing
57%> the image ID (e.g., MPI rank + 1) of the current MPI image in
58%> the current invocation of a Hydra-based ``mpiexec`` binary MPI launcher.<br>
59%> An output value of ``0`` implies the algorithm failed to detect
60%> a ParaMonte-compatible MPI library or failed to infer the image count.<br>
61%> Note that the image ID always starts at ``1``, unlike the MPI rank.<br>
62%> The argument ``rankp1`` stands for ``rank + 1``.<br>
63%> An output value of ``0`` implies the algorithm failed to detect
64%> a ParaMonte-compatible Hydra-based MPI library or failed to infer the image count.<br>
65%>
66%> \interface{hydra}
67%> \code{.m}
68%>
69%> [nproc, rankp1] = pm.lib.mpi.runtime.hydra()
70%>
71%> \endcode
72%>
73%> \see
74%> [pm.lib.mpi.runtime.mmpi()](@ref mmpi)<br>
75%> [pm.lib.mpi.runtime.impi()](@ref impi)<br>
76%> [pm.lib.mpi.runtime.ompi()](@ref ompi)<br>
77%> [pm.lib.mpi.runtime.hydra()](@ref hydra)<br>
78%> [pm.lib.mpi.runtime.nproc()](@ref nproc)<br>
79%> [pm.lib.mpi.runtime.rankp1()](@ref rankp1)<br>
80%> [pm.lib.mpi.runtime.isimpi()](@ref isimpi)<br>
81%> [pm.lib.mpi.runtime.detect()](@ref detect)<br>
82%>
83%> \example{hydra}
84%> \include{lineno} example/lib/mpi/runtime/hydra/main.m
85%> \output{hydra}
86%> \include{lineno} example/lib/mpi/runtime/hydra/main.out.m
87%>
88%> \final{hydra}
89%>
90%> \author
91%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
92%> \AmirShahmoradi, 12:10 AM Wednesday, November 13, 2024, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
93function [nproc, rankp1] = hydra()
94
95 for varname = ["PMI_SIZE", "MPI_LOCALNRANKS"]
96 nproc = getenv(varname);
97 if ~isempty(nproc)
98 nproc = str2double(nproc);
99 break;
100 else
101 nproc = [];
102 end
103 end
104 if isempty(nproc)
105 nproc = 0;
106 end
107
108 for varname = ["PMI_RANK", "MPI_LOCALRANKID"]
109 rankp1 = getenv(varname);
110 if ~isempty(rankp1)
111 rankp1 = str2double(rankp1) + 1;
112 break;
113 else
114 rankp1 = [];
115 end
116 end
117 if isempty(rankp1)
118 rankp1 = 0;
119 end
120
121end
function list()
Return a list of MATLAB strings containing the names of OS platforms supported by the ParaMonte MATLA...
function detect(in vendor)
Return the MPI image count and current image ID (e.g., MPI rank + 1) and the MPI library name as it a...
function hydra()
Return the MPI image count and the current image ID (e.g., MPI rank + 1) if the application has been ...
function impi()
Return the MPI image count and the current image ID (e.g., MPI rank + 1) if the application has been ...
function isimpi()
Return true if and only if the application has been invoked via an Intel MPI mpiexec launcher,...
function lib()
Return a scalar MATLAB string containing the path to the lib directory of the ParaMonte library packa...
function mmpi()
Return the MPI image count and the current image ID (e.g., MPI rank + 1) if the application has been ...
function nproc(in vendor)
Return the runtime number of MPI processes with which the mpiexec launcher may have been invoked.
function ompi()
Return the MPI image count and the current image ID (e.g., MPI rank + 1) if the application has been ...
function rankp1(in vendor)
Return the ID (MPI rank + 1) of the current MPI image (process), starting from the number one.