Remove reliance on MPI_Comm_rank (#56)

* Remove reliance on MPI_Comm_rank

- read /proc/<PID>/tasks/<PID>/children of parent process to deduce the rank
- Old format relied on user calling MPI_Comm_rank(MPI_COMM_WORLD, ...)
- if MPI_Comm_rank called with subcommunicators only, multiple ranks would write to same file

* Tweak mpi example
This commit is contained in:
Jonathan R. Madsen
2022-06-20 00:50:49 -05:00
committed by GitHub
parent f27f062e88
commit 8eff363ed3
5 changed files with 171 additions and 16 deletions
+19 -6
View File
@@ -20,6 +20,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <mpi.h>
#include <cfloat>
#include <chrono>
#include <cmath>
@@ -30,15 +32,12 @@ THE SOFTWARE.
#include <iostream>
#include <mutex>
#include <random>
#include <sstream>
#include <thread>
#include <type_traits>
#include <unistd.h>
#include <vector>
static std::mutex print_lock{};
using auto_lock_t = std::unique_lock<std::mutex>;
#include <mpi.h>
std::string _name = {};
template <typename Tp, size_t N>
@@ -105,10 +104,24 @@ main(int argc, char** argv)
printf("[%s] Number of iterations: %i\n", _name.c_str(), nitr);
MPI_Init(&argc, &argv);
int _mpi_thread_provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_SINGLE, &_mpi_thread_provided);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
auto _ppid = getppid();
std::ifstream _ifs{ "/proc/" + std::to_string(_ppid) + "/task/" +
std::to_string(_ppid) + "/children" };
std::stringstream _ss{};
while(_ifs)
{
std::string _s{};
_ifs >> _s;
_ss << _s << " ";
}
printf("[%s] RANK = %i, PID = %i, PPID = %i :: %s\n", _name.c_str(), rank, getpid(),
getppid(), _ss.str().c_str());
MPI_Barrier(MPI_COMM_WORLD);
for(int i = 0; i < nitr; ++i)
{