ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
mmpi.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 the MPICH MPI ``mpiexec`` launcher.<br>
4%>
5%> \details
6%> This function attempts to detect the invocation of the current MATLAB
7%> session via an MPICH MPI ``mpiexec`` binary MPI launcher instance.<br>
8%> The runtime detection is achieved by checking for runtime environment
9%> variables that the MPICH MPI ``mpiexec`` binary executable defines upon launch.<br>
10%>
11%> Specifically, MPICH MPI provides a long 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 MPICH-specific runtime variables
18%> is to launch the MPICH ``mpiexec`` binary executable with the ``printenv`` Unix command.<br>
19%> ```bash
20%> mpiexec -np 1 printenv
21%> ```
22%> To see the compile-time variables and compare them with the runtime list from the above, try:<br>
23%> ```bash
24%> printenv
25%> ```
26%> <li> On **Windows systems**, the best approach to investigate the MPICH-specific runtime variables
27%> is to launch the MPICH ``mpiexec`` binary executable with the ``set`` Windows batch command.<br>
28%> ```batch
29%> mpiexec -localonly -n 1 cmd /k set
30%> ```
31%> To see the compile-time variables and compare them with the runtime list from the above, try:<br>
32%> ```batch
33%> set
34%> ```
35%> </ol>
36%>
37%> The most persistent environment variables for MPICH ``mpiexec`` binary appear to be the following:<br>
38%>
39%> <ol>
40%> <li> ``PMI_RANK`` : The rank of the current process in the current MPI communication world.<br>
41%> This environment variable is common to all Hydra-based MPI libraries, such as MPICH and MPICH MPI libraries.<br>
42%> <li> ``PMI_SIZE`` : The size (i.e., number of images/processes) of the current MPI communication world.<br>
43%> This environment variable is common to all Hydra-based MPI libraries, such as MPICH and MPICH MPI libraries.<br>
44%> <li> ``MPI_LOCALRANKID`` : The **local** rank of the current process relative to the local host.<br>
45%> This environment variable is common to all Hydra-based MPI libraries, such as MPICH and MPICH 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 is common to all Hydra-based MPI libraries, such as MPICH and MPICH MPI libraries.<br>
48%> </ol>
49%>
50%> Therefore, this function first attempts to detect the Hydra process manager.<br>
51%> If identified, the function will then attempt to identify the MPICH ``mpiexec`` runtime variables.<br>
52%>
53%> \return
54%> ``nproc`` : The output scalar MATLAB non-negative whole number containing
55%> the number of MPI images in the current invocation of the
56%> MPICH MPI ``mpiexec`` binary MPI launcher.<br>
57%> An output value of ``0`` implies the algorithm failed to detect
58%> a ParaMonte-compatible MPICH MPI library or failed to infer the image count.<br>
59%> ``rankp1`` : The output scalar MATLAB non-negative whole number containing
60%> the image ID (e.g., MPI rank + 1) of the current MPI image in
61%> the current invocation of the MPICH MPI ``mpiexec`` binary MPI launcher.<br>
62%> An output value of ``0`` implies the algorithm failed to detect
63%> a ParaMonte-compatible MPI library or failed to infer the image count.<br>
64%> Note that the image ID always starts at ``1``, unlike the MPI rank.<br>
65%> The argument ``rankp1`` stands for ``rank + 1``.<br>
66%> An output value of ``0`` implies the algorithm failed to detect
67%> a ParaMonte-compatible MPICH MPI library or failed to infer the image count.<br>
68%>
69%> \interface{mmpi}
70%> \code{.m}
71%>
72%> [nproc, rankp1] = pm.lib.mpi.runtime.mmpi()
73%>
74%> \endcode
75%>
76%> \see
77%> [pm.lib.mpi.runtime.mmpi()](@ref mmpi)<br>
78%> [pm.lib.mpi.runtime.impi()](@ref impi)<br>
79%> [pm.lib.mpi.runtime.ompi()](@ref ompi)<br>
80%> [pm.lib.mpi.runtime.hydra()](@ref hydra)<br>
81%> [pm.lib.mpi.runtime.nproc()](@ref nproc)<br>
82%> [pm.lib.mpi.runtime.rankp1()](@ref rankp1)<br>
83%> [pm.lib.mpi.runtime.isimpi()](@ref isimpi)<br>
84%> [pm.lib.mpi.runtime.detect()](@ref detect)<br>
85%>
86%> \example{mmpi}
87%> \include{lineno} example/lib/mpi/runtime/mmpi/main.m
88%> \output{mmpi}
89%> \include{lineno} example/lib/mpi/runtime/mmpi/main.out.m
90%>
91%> \final{mmpi}
92%>
93%> \author
94%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
95%> \AmirShahmoradi, 12:10 AM Wednesday, November 13, 2024, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
96function [nproc, rankp1] = mmpi()
97
98 %%%%
99 %%%% Ensure the consistency of environment variables and non-Intel identity of ``mpiexec``.
100 %%%%
101
102 if pm.lib.mpi.runtime.isimpi()% || nproc == 0 || rankp1 == 0
103 rankp1 = 0;
104 nproc = 0;
105 else
106 [nproc, rankp1] = pm.lib.mpi.runtime.hydra();
107 end
108
109end
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.