PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
parallelUtil.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "parallelUtil.h"
12#include <iostream>
13#include <sstream>
14
15namespace {
16
17 // MPI-related
20
21 // Thread-related (via Taskflow)
22 unsigned int numThreads = 0;
23}
24
25void util::parallel::initMpi(int argc, char *argv[]) {
26
27 MPI_Initialized(&mpiInitialized);
28 if (!mpiInitialized)
29 MPI_Init(&argc, &argv);
31}
32
34 d_comm = MPI_COMM_WORLD;
35 MPI_Comm_size(d_comm, &d_mpiSize);
36 MPI_Comm_rank(d_comm, &d_mpiRank);
38}
39
40std::string util::parallel::MpiStatus::printStr(int nt, int lvl) const {
41 std::string tabS = "";
42 for (int i = 0; i < nt; i++)
43 tabS += "\t";
44 std::ostringstream oss;
45 oss << tabS << "------- MpiStatus --------" << std::endl << std::endl;
46 //oss << tabS << "Comm = " << d_comm << std::endl;
47 oss << tabS << "MPI Size = " << d_mpiSize << std::endl;
48 oss << tabS << "MPI Rank = " << d_mpiRank << std::endl;
49 oss << tabS << "MPI Enabled = " << d_mpiEnabled << std::endl;
50 oss << tabS << std::endl;
51
52 return oss.str();
53}
54
56 if (mpistatus_p != nullptr)
57 return;
58
59 mpistatus_p = new util::parallel::MpiStatus();
60}
61
63
64 // for now, we do not call assert and rather create MpiStatus if it is not instantiated
65 /*
66 //assert((mpistatus_p != nullptr) && "mpistatus_p "
67 // "(pointer of struct type util::parallel::MpiStatus) is not initialized. "
68 // "Call util::parallel::initMpiStatus() possibly right after MPI_Init().");
69 */
70 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
71 if (mpistatus_p == nullptr)
73
74 return mpistatus_p->d_mpiEnabled;
75}
76
78 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
79 if (mpistatus_p == nullptr)
81
82 return mpistatus_p->d_mpiSize;
83}
84
86 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
87 if (mpistatus_p == nullptr)
89
90 return mpistatus_p->d_mpiRank;
91}
92
94 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
95 if (mpistatus_p == nullptr)
97
98 return mpistatus_p->d_comm;
99}
100
102 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
103 if (mpistatus_p == nullptr)
105
106 return mpistatus_p;
107}
108
109void util::parallel::initNThreads(unsigned int nThreads) {
110 if (numThreads > 0) {
111 std::cout << "Number of threads numThreads is already initialized.\n";
112 } else
113 numThreads = nThreads;
114}
115
117 if (numThreads == 0)
118 numThreads = std::thread::hardware_concurrency();
119 return numThreads;
120}
121
122
util::parallel::MpiStatus * mpistatus_p
bool isMpiEnabled()
Function to check if MPI is enabled.
void initMpiStatus()
Initializes MpiStatus struct.
unsigned int getNThreads()
Get number of threads to be used by taskflow.
void initNThreads(unsigned int nThreads=std::thread::hardware_concurrency())
Initializes MpiStatus struct.
const MpiStatus * getMpiStatus()
Returns pointer to MpiStatus struct.
void initMpi(int argc=0, char *argv[]=nullptr)
Initializes MPI and also creates MpiStatus struct.
int mpiSize()
Get size (number) of processors.
int mpiRank()
get rank (id) of this processor
MPI_Comm mpiComm()
Get MPI comm.
Struct that stores MPI-related information.
int d_mpiRank
Rank (id) of this processor.
bool d_mpiEnabled
Specifies if MPI is enabled (yes if code executed with more than one processor)
int d_mpiSize
Size (number) of processors.
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
MPI_Comm d_comm
MPI comm.