OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
worker.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
39#ifndef PFC_MPI_WORKER_HPP
40#define PFC_MPI_WORKER_HPP
41
42#include <mpi.h>
43
44#include <iostream>
45
46namespace pfc {
47
56 MPI_Comm m_comm;
57 int m_rank;
58 int m_num_procs;
59 bool m_owns_mpi;
60
61public:
75 bool verbose = true)
76 : m_comm(comm) {
77 int initialized = 0;
78 MPI_Initialized(&initialized);
79 if (!initialized) {
80 MPI_Init(&argc, &argv);
81 m_owns_mpi = true;
82 } else {
83 m_owns_mpi = false;
84 }
85 MPI_Comm_rank(m_comm, &m_rank);
86 MPI_Comm_size(m_comm, &m_num_procs);
87 if (m_owns_mpi && verbose) {
88 std::cout << "MPI_Init(): initialized " << m_num_procs << " processes"
89 << std::endl;
90 }
91 if (m_rank != 0) {
92 mute();
93 }
94 }
101 if (m_owns_mpi) {
102 int finalized = 0;
103 MPI_Finalized(&finalized);
104 if (!finalized) {
105 MPI_Finalize();
106 }
107 }
108 }
109
115 int get_rank() const { return m_rank; }
116
122 int get_num_ranks() const { return m_num_procs; }
123
130 void mute() { std::cout.setstate(std::ios::failbit); }
131
138 void unmute() { std::cout.clear(); }
139};
140
141} // namespace pfc
142
143#endif // PFC_MPI_WORKER_HPP
An MPI worker class that wraps MPI_Init and MPI_Finalize.
Definition worker.hpp:55
~MPI_Worker()
Destroys the MPI worker instance and finalizes MPI if it owns it.
Definition worker.hpp:100
void unmute()
Unmutes the standard output.
Definition worker.hpp:138
void mute()
Mutes the standard output.
Definition worker.hpp:130
int get_num_ranks() const
Returns the number of processes in the MPI communicator.
Definition worker.hpp:122
MPI_Worker(int argc, char *argv[], MPI_Comm comm=MPI_COMM_WORLD, bool verbose=true)
Constructs an MPI worker instance and initializes MPI if needed.
Definition worker.hpp:74
int get_rank() const
Returns the rank of this worker process in the MPI communicator.
Definition worker.hpp:115
Represents the global simulation domain (the "world").
Definition world.hpp:91