PeriDEM 0.3.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 - 2026 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 int initialized = 0;
35 MPI_Initialized(&initialized);
36 if (!initialized)
37 return;
38 int finalized = 0;
39 MPI_Finalized(&finalized);
40 if (!finalized)
41 MPI_Finalize();
42}
43
45 d_comm = MPI_COMM_WORLD;
46 MPI_Comm_size(d_comm, &d_mpiSize);
47 MPI_Comm_rank(d_comm, &d_mpiRank);
49}
50
51std::string util::parallel::MpiStatus::printStr(int nt, int lvl) const {
52 std::string tabS = "";
53 for (int i = 0; i < nt; i++)
54 tabS += "\t";
55 std::ostringstream oss;
56 oss << tabS << "------- MpiStatus --------" << std::endl << std::endl;
57 //oss << tabS << "Comm = " << d_comm << std::endl;
58 oss << tabS << "MPI Size = " << d_mpiSize << std::endl;
59 oss << tabS << "MPI Rank = " << d_mpiRank << std::endl;
60 oss << tabS << "MPI Enabled = " << d_mpiEnabled << std::endl;
61 oss << tabS << std::endl;
62
63 return oss.str();
64}
65
67 if (mpistatus_p != nullptr)
68 return;
69
70 mpistatus_p = new util::parallel::MpiStatus();
71}
72
74
75 // MpiStatus is created if it is not instantiated rather than asserting.
76 /*
77 //assert((mpistatus_p != nullptr) && "mpistatus_p "
78 // "(pointer of struct type util::parallel::MpiStatus) is not initialized. "
79 // "Call util::parallel::initMpiStatus() possibly right after MPI_Init().");
80 */
81 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
82 if (mpistatus_p == nullptr)
84
85 return mpistatus_p->d_mpiEnabled;
86}
87
89 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
90 if (mpistatus_p == nullptr)
92
93 return mpistatus_p->d_mpiSize;
94}
95
97 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
98 if (mpistatus_p == nullptr)
100
101 return mpistatus_p->d_mpiRank;
102}
103
105 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
106 if (mpistatus_p == nullptr)
108
109 return mpistatus_p->d_comm;
110}
111
113 //assert(mpiInitialized && "mpiInitialized is false indicating MPI_Init() or util::parallel::initMpi() has not been called.\n");
114 if (mpistatus_p == nullptr)
116
117 return mpistatus_p;
118}
119
120void util::parallel::initNThreads(unsigned int nThreads) {
121 if (numThreads > 0) {
122 std::cout << "Number of threads numThreads is already initialized.\n";
123 } else
124 numThreads = nThreads;
125}
126
128 if (numThreads == 0)
129 numThreads = std::thread::hardware_concurrency();
130 return numThreads;
131}
132
133
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
void finalizeMpi()
Call MPI_Finalize if this process initialized MPI.
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.