8.3
general documentation
cs_execution_context.h
Go to the documentation of this file.
1#ifndef __CS_EXECUTION_CONTEXT_H__
2#define __CS_EXECUTION_CONTEXT_H__
3
4/*============================================================================
5 * Class to handle different execution policies (MPI, OpenMP, CUDA, ...)
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2024 EDF S.A.
12
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free Software
15 Foundation; either version 2 of the License, or (at your option) any later
16 version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25 Street, Fifth Floor, Boston, MA 02110-1301, USA.
26*/
27
28// Valid only for C++
29
31#ifdef __cplusplus
34/*----------------------------------------------------------------------------*/
35
36#include "cs_defs.h"
37
38/*----------------------------------------------------------------------------
39 * Standard C++ library headers
40 *----------------------------------------------------------------------------*/
41
42#include <utility>
43
44#if defined(SYCL_LANGUAGE_VERSION)
45#include <sycl/sycl.hpp>
46#endif
47
48/*============================================================================
49 * Type definitions
50 *============================================================================*/
51
52/*----------------------------------------------------------------------------*/
59/*----------------------------------------------------------------------------*/
60
62
63/* Public methods
64 --------------*/
65
66public:
67
68 /*--------------------------------------------------------------------------*/
72 /*--------------------------------------------------------------------------*/
73
75 {
76#if defined(HAVE_MPI)
77 this->_comm = MPI_COMM_NULL;
78#endif
79 this->_comm_rank = 0;
80 this->_comm_n_ranks = 0;
81 this->_thread_id = 0;
82 };
83
84 /*--------------------------------------------------------------------------*/
88 /*--------------------------------------------------------------------------*/
89
91 {
92 };
93
94 /*--------------------------------------------------------------------------*/
100 /*--------------------------------------------------------------------------*/
101
102 int rank() const
103 {
104 return _comm_rank;
105 };
106
107 /*--------------------------------------------------------------------------*/
113 /*--------------------------------------------------------------------------*/
114
115 int n_ranks() const
116 {
117 return _comm_n_ranks;
118 };
119
120 /*--------------------------------------------------------------------------*/
126 /*--------------------------------------------------------------------------*/
127
128 bool use_mpi() const
129 {
130 bool retval = (_comm_n_ranks > 1) ? true : false;
131 return retval;
132 };
133
134 /*--------------------------------------------------------------------------*/
140 /*--------------------------------------------------------------------------*/
141
142 bool is_mpi_root() const
143 {
144 bool retval = (_comm_rank < 1) ? true : false;
145 return retval;
146 };
147
148 /*--------------------------------------------------------------------------*/
154 /*--------------------------------------------------------------------------*/
155
156 int thread_id() const
157 {
158 return _thread_id;
159 };
160
161 /*--------------------------------------------------------------------------*/
165 /*--------------------------------------------------------------------------*/
166
167 void
169 {
170#if defined(HAVE_MPI)
171 if (_comm != MPI_COMM_NULL)
172 MPI_Comm_free(&(this->_comm));
173 this->_comm = MPI_COMM_NULL;
174#endif
175 }
176
177 /*--------------------------------------------------------------------------*/
181 /*--------------------------------------------------------------------------*/
182
183 int
185 {
186 int retval = 0;
187#if defined(HAVE_MPI)
188 if (_comm != MPI_COMM_NULL)
189 retval = MPI_Barrier(_comm);
190#endif
191 return retval;
192 }
193
195#if defined(HAVE_MPI)
198 /*--------------------------------------------------------------------------*/
202 /*--------------------------------------------------------------------------*/
203
204 void
206 (
207 MPI_Comm comm
208 )
209 {
210 int _initialized;
211 MPI_Initialized(&_initialized);
212 if (_initialized) {
213 this->_comm = comm;
214 MPI_Comm_rank(this->_comm, &(this->_comm_rank));
215 MPI_Comm_size(this->_comm, &(this->_comm_n_ranks));
216 }
217 };
218
219 /*--------------------------------------------------------------------------*/
225 /*--------------------------------------------------------------------------*/
226
227 MPI_Comm comm() const
228 {
229 return _comm;
230 };
231
233#endif // defined(HAVE_MPI)
236/* Private attributes
237 -------------------- */
238
239private:
240
241#if defined(HAVE_MPI)
242 MPI_Comm _comm;
243#endif
244
245 int _comm_rank;
246 int _comm_n_ranks;
247 int _thread_id;
249#if defined(__NVCC__)
250 cudaStream_t _stream;
252#elif defined(SYCL_LANGUAGE_VERSION)
253 sycl::queue _queue;
254#endif
255
256};
257
258/*----------------------------------------------------------------------------*/
265/*----------------------------------------------------------------------------*/
266
269
270/*----------------------------------------------------------------------------*/
276/*----------------------------------------------------------------------------*/
277
280
282#endif /* __cplusplus */
285/*----------------------------------------------------------------------------*/
286
288
289/*----------------------------------------------------------------------------*/
293/*----------------------------------------------------------------------------*/
294
295void
297
298/*----------------------------------------------------------------------------*/
302/*----------------------------------------------------------------------------*/
303
304void
306
307/*----------------------------------------------------------------------------*/
308
310
311#endif /* __CS_EXECUTION_CONTEXT_H__ */
Definition: cs_execution_context.h:61
int rank() const
Getter function for MPI communicator rank id.
Definition: cs_execution_context.h:102
void comm_free()
Free the MPI communicator.
Definition: cs_execution_context.h:168
void set_comm(MPI_Comm comm)
Set the execution context MPI communicator.
Definition: cs_execution_context.h:206
int barrier()
Call MPI_Barrier over the MPI communicator, do nothing if no MPI.
Definition: cs_execution_context.h:184
bool is_mpi_root() const
Is the current cpu/task the MPI root (rank 0) ?
Definition: cs_execution_context.h:142
cs_execution_context()
Constructor.
Definition: cs_execution_context.h:74
bool use_mpi() const
Does the execution context uses MPI parallelism ?
Definition: cs_execution_context.h:128
virtual ~cs_execution_context()
Destructor.
Definition: cs_execution_context.h:90
int thread_id() const
Getter function for thread id.
Definition: cs_execution_context.h:156
int n_ranks() const
Getter function for MPI communicator number of ranks.
Definition: cs_execution_context.h:115
MPI_Comm comm() const
Getter function for MPI communicator.
Definition: cs_execution_context.h:227
static bool _initialized
Definition: cs_boundary_conditions_type.cpp:74
#define BEGIN_C_DECLS
Definition: cs_defs.h:542
#define END_C_DECLS
Definition: cs_defs.h:543
void cs_execution_context_glob_finalize(void)
Free the global execution context pointer.
Definition: cs_execution_context.cpp:87
const cs_execution_context * cs_execution_context_glob_get(void)
Get the global execution context.
Definition: cs_execution_context.cpp:60
void cs_execution_context_glob_init(void)
Initialize the global execution context.
Definition: cs_execution_context.cpp:74
const cs_execution_context * cs_execution_context_get(void)
Get the current execution context. For the moment only global context is returned.
Definition: cs_execution_context.cpp:50