9.2
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-2026 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/*----------------------------------------------------------------------------*/
29
30#include "base/cs_defs.h"
31
32#include "base/cs_dispatch.h"
33
34/*----------------------------------------------------------------------------
35 * Standard C++ library headers
36 *----------------------------------------------------------------------------*/
37
38#include <utility>
39
40/*============================================================================
41 * Type definitions
42 *============================================================================*/
43
44namespace cs {
45namespace execution {
47
48public:
49
52{};
53
56{};
57
59inline
60int
61rank() const
62{
63 return _comm_rank;
64};
65
67inline
68int
69n_ranks() const
70{
71 return _comm_n_ranks;
72}
73
75inline
76bool
77active() const
78{
79 bool retval = (_comm_n_ranks > 1) ? true : false;
80 return retval;
81}
82
84inline
85bool
86is_root() const
87{
88 bool retval = (_comm_rank < 1) ? true : false;
89 return retval;
90}
91
93void
95{
96#if defined(HAVE_MPI)
97 if (_comm != MPI_COMM_NULL)
98 MPI_Comm_free(&(this->_comm));
99 this->_comm = MPI_COMM_NULL;
100#endif
101}
102
103#if defined(HAVE_MPI)
105void
107(
108 MPI_Comm comm
109)
110{
111 int _initialized;
112 MPI_Initialized(&_initialized);
113 if (_initialized) {
114 this->_comm = comm;
115 MPI_Comm_rank(this->_comm, &(this->_comm_rank));
116 MPI_Comm_size(this->_comm, &(this->_comm_n_ranks));
117 }
118}
119
121inline
122MPI_Comm
123comm() const
124{
125 return _comm;
126}
127
128#endif
129
130private:
131
132#if defined(HAVE_MPI)
133 MPI_Comm _comm = MPI_COMM_NULL;
134#endif
135
136 int _comm_rank = -1;
137 int _comm_n_ranks = 0;
138};
139
141
142public:
144 {
145 mpi = nullptr;
146 ctx = nullptr;
147 }
148
150 {
151 if (mpi != nullptr)
152 delete mpi;
153
154 if (ctx != nullptr)
155 delete ctx;
156 }
157
160};
161
162const environment *
163default_env(void);
164
166default_context(void);
167
170
172default_mpi(void);
173
174}; // namespace execution
175}; // namespace cs
176
177void
179
180void
182
183/*----------------------------------------------------------------------------*/
187/*----------------------------------------------------------------------------*/
188
189void
191
192/*--------------------------------------------------------------------------*/
196/*--------------------------------------------------------------------------*/
197
198void
200
201/*----------------------------------------------------------------------------*/
205/*----------------------------------------------------------------------------*/
206
207void
209
210/*----------------------------------------------------------------------------*/
211
212namespace cs {
213
214enum class exec_type {
215 device,
216 host,
217 unknown
218};
219
220/*--------------------------------------------------------------------------*/
225/*--------------------------------------------------------------------------*/
226
227template<exec_type _EXEC_, class F, class... Args>
228bool
229parallel_for(cs_lnum_t n, F&& f, Args&&... args) {
230 auto& _ctx = (_EXEC_ != exec_type::host) ?
232 _ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
233 return true;
234}
235
236/*--------------------------------------------------------------------------*/
240/*--------------------------------------------------------------------------*/
241
242template <class F, class... Args>
243bool
244parallel_for(cs_lnum_t n, F&& f, Args&&... args) {
245 auto& _ctx = cs::execution::default_context();
246 _ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
247 return true;
248}
249
250/*--------------------------------------------------------------------------*/
254/*--------------------------------------------------------------------------*/
255
256template <class F, class... Args>
257bool
258parallel_for(cs_dispatch_context& ctx, cs_lnum_t n, F&& f, Args&&... args) {
259 ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
260 return true;
261}
262
263/*--------------------------------------------------------------------------*/
267/*--------------------------------------------------------------------------*/
268
269template <class F, class... Args>
270bool
271parallel_for(cs_host_context& ctx, cs_lnum_t n, F&& f, Args&&... args) {
272 ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
273 return true;
274}
275
276// paralell_for_i_faces
277/*--------------------------------------------------------------------------*/
282/*--------------------------------------------------------------------------*/
283
284template <exec_type _EXEC_, class M, class F, class... Args>
285bool
286parallel_for_i_faces(const M* m, F&& f, Args&&... args) {
287 const cs_lnum_t n = m->n_i_faces;
288 auto& _ctx = (_EXEC_ != exec_type::host) ?
290 _ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
291 return true;
292}
293
294/*--------------------------------------------------------------------------*/
299/*--------------------------------------------------------------------------*/
300
301template <class M, class F, class... Args>
302bool
303parallel_for_i_faces(const M* m, F&& f, Args&&... args) {
304 const cs_lnum_t n = m->n_i_faces;
305 auto& _ctx = cs::execution::default_context();
306 _ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
307 return true;
308}
309
310/*--------------------------------------------------------------------------*/
314/*--------------------------------------------------------------------------*/
315
316template <class M, class F, class... Args>
317bool
318parallel_for_i_faces(cs_dispatch_context& ctx, const M* m, F&& f, Args&&... args) {
319 const cs_lnum_t n = m->n_i_faces;
320 ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
321 return true;
322}
323
324/*--------------------------------------------------------------------------*/
328/*--------------------------------------------------------------------------*/
329
330template <class M, class F, class... Args>
331bool
332parallel_for_i_faces(cs_host_context& ctx, const M* m, F&& f, Args&&... args) {
333 const cs_lnum_t n = m->n_i_faces;
334 ctx.parallel_for(n, static_cast<F&&>(f), static_cast<Args&&>(args)...);
335 return true;
336}
337
338/*--------------------------------------------------------------------------*/
343/*--------------------------------------------------------------------------*/
344
345template <exec_type _EXEC_, class T, class F, class... Args>
346bool
348 cs_lnum_t n,
349 T& sum,
350 F&& f,
351 Args&&... args)
352{
353 auto& _ctx = (_EXEC_ != exec_type::host) ?
355 _ctx.parallel_for_reduce_sum(n,
356 sum,
357 static_cast<F&&>(f),
358 static_cast<Args&&>(args)...);
359 return true;
360}
361
362/*--------------------------------------------------------------------------*/
367/*--------------------------------------------------------------------------*/
368
369template <class T, class F, class... Args>
370bool
372 cs_lnum_t n,
373 T& sum,
374 F&& f,
375 Args&&... args)
376{
377 auto& _ctx = cs::execution::default_context();
378 _ctx.parallel_for_reduce_sum(n,
379 sum,
380 static_cast<F&&>(f),
381 static_cast<Args&&>(args)...);
382 return true;
383}
384
385/*--------------------------------------------------------------------------*/
390/*--------------------------------------------------------------------------*/
391
392template <class T, class F, class... Args>
393bool
396 cs_lnum_t n,
397 T& sum,
398 F&& f,
399 Args&&... args)
400{
402 sum,
403 static_cast<F&&>(f),
404 static_cast<Args&&>(args)...);
405 return true;
406}
407
408/*--------------------------------------------------------------------------*/
413/*--------------------------------------------------------------------------*/
414
415template <class T, class F, class... Args>
416bool
418 cs_host_context& ctx,
419 cs_lnum_t n,
420 T& sum,
421 F&& f,
422 Args&&... args)
423{
425 sum,
426 static_cast<F&&>(f),
427 static_cast<Args&&>(args)...);
428 return true;
429}
430
431// Parallel reduction with general reducer.
432
433/*--------------------------------------------------------------------------*/
438/*--------------------------------------------------------------------------*/
439
440template <exec_type _EXEC_, class T, class R, class F, class... Args>
441bool
443 cs_lnum_t n,
444 T& result,
445 R& reducer,
446 F&& f,
447 Args&&... args)
448{
449 auto& _ctx = (_EXEC_ != exec_type::host) ?
451 _ctx.parallel_for_reduce(n,
452 result,
453 reducer,
454 static_cast<F&&>(f),
455 static_cast<Args&&>(args)...);
456 return true;
457}
458
459/*--------------------------------------------------------------------------*/
464/*--------------------------------------------------------------------------*/
465
466template <class T, class R, class F, class... Args>
467bool
469 cs_lnum_t n,
470 T& result,
471 R& reducer,
472 F&& f,
473 Args&&... args)
474{
475 auto& _ctx = cs::execution::default_context();
476 _ctx.parallel_for_reduce(n,
477 result,
478 reducer,
479 static_cast<F&&>(f),
480 static_cast<Args&&>(args)...);
481 return true;
482}
483
484/*--------------------------------------------------------------------------*/
489/*--------------------------------------------------------------------------*/
490
491template <class T, class R, class F, class... Args>
492bool
495 cs_lnum_t n,
496 T& result,
497 R& reducer,
498 F&& f,
499 Args&&... args)
500{
502 result,
503 reducer,
504 static_cast<F&&>(f),
505 static_cast<Args&&>(args)...);
506 return true;
507}
508
509/*--------------------------------------------------------------------------*/
514/*--------------------------------------------------------------------------*/
515
516template <class T, class R, class F, class... Args>
517bool
519 cs_host_context& ctx,
520 cs_lnum_t n,
521 T& result,
522 R& reducer,
523 F&& f,
524 Args&&... args)
525{
527 result,
528 reducer,
529 static_cast<F&&>(f),
530 static_cast<Args&&>(args)...);
531 return true;
532}
533
534/*--------------------------------------------------------------------------*/
539/*--------------------------------------------------------------------------*/
540
541template<exec_type _EXEC_, class... Args>
542bool
543wait(void)
544{
545 auto& _ctx = (_EXEC_ != exec_type::host) ?
547 _ctx.wait();
548 return true;
549}
550
551/*--------------------------------------------------------------------------*/
555/*--------------------------------------------------------------------------*/
556
557template<class... Args>
558bool
560(void)
561{
562 auto& _ctx = cs::execution::default_context();
563 _ctx.wait();
564 return true;
565}
566
567/*--------------------------------------------------------------------------*/
571/*--------------------------------------------------------------------------*/
572
573template<class... Args>
574bool
576(
578)
579{
580 ctx.wait();
581 return true;
582}
583
584/*--------------------------------------------------------------------------*/
588/*--------------------------------------------------------------------------*/
589
590template<class... Args>
591bool
593(
594 cs_host_context& ctx
595)
596{
597 ctx.wait();
598 return true;
599}
600
601} // namespace cs
602
604
605/*--------------------------------------------------------------------------*/
606
607#endif /* CS_EXECUTION_CONTEXT_H */
Definition: cs_execution_context.h:140
cs_dispatch_context * ctx
Definition: cs_execution_context.h:159
environment()
Definition: cs_execution_context.h:143
~environment()
Definition: cs_execution_context.h:149
mpi_wrapper * mpi
Definition: cs_execution_context.h:158
Definition: cs_execution_context.h:46
CS_F_HOST mpi_wrapper()
Definition: cs_execution_context.h:51
CS_F_HOST_DEVICE int n_ranks() const
Definition: cs_execution_context.h:69
CS_F_HOST void set_comm(MPI_Comm comm)
Definition: cs_execution_context.h:107
CS_F_HOST MPI_Comm comm() const
Definition: cs_execution_context.h:123
CS_F_HOST_DEVICE bool is_root() const
Definition: cs_execution_context.h:86
CS_F_HOST ~mpi_wrapper()
Definition: cs_execution_context.h:55
CS_F_HOST void free()
Definition: cs_execution_context.h:94
CS_F_HOST_DEVICE int rank() const
Definition: cs_execution_context.h:61
CS_F_HOST_DEVICE bool active() const
Definition: cs_execution_context.h:77
auto parallel_for_reduce_sum(cs_lnum_t n, T &sum, F &&f, Args &&... args)
Definition: cs_dispatch.h:2169
auto parallel_for_reduce(cs_lnum_t n, T &result, R &reducer, F &&f, Args &&... args)
Definition: cs_dispatch.h:2195
auto parallel_for(cs_lnum_t n, F &&f, Args &&... args)
Definition: cs_dispatch.h:2146
void wait(void)
Wait (synchronize) until launched computations have finished.
Definition: cs_dispatch.h:2211
Definition: cs_dispatch.h:2288
Definition: cs_dispatch.h:215
bool parallel_for(cs_lnum_t n, F &&f, Args &&... args)
Iterate using a plain omp parallel for.
Definition: cs_dispatch.h:310
bool parallel_for_reduce_sum(cs_lnum_t n, T &sum, F &&f, Args &&... args)
Plain OpenMP parallel reduction with simple sum.
Definition: cs_dispatch.h:369
bool wait(void)
Wait upon completion.
Definition: cs_dispatch.h:488
bool parallel_for_reduce(cs_lnum_t n, T &result, R &reducer, F &&f, Args &&... args)
OpenMP parallel reduction with general reducer.
Definition: cs_dispatch.h:431
static bool _initialized
Definition: cs_boundary_conditions_type.cpp:69
#define CS_F_HOST_DEVICE
Definition: cs_defs.h:555
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:325
#define CS_F_HOST
Definition: cs_defs.h:553
void cs_execution_default_env_init(void)
Initialize the global execution context.
Definition: cs_execution_context.cpp:88
void cs_execution_default_env_init_context(void)
Initialize context (CPU/GPU)
Definition: cs_execution_context.cpp:102
void cs_execution_default_env_finalize()
Free the global execution context pointer.
Definition: cs_execution_context.cpp:112
cs_host_context & default_h_context(void)
Definition: cs_execution_context.cpp:72
const environment * default_env(void)
Definition: cs_execution_context.cpp:52
cs_dispatch_context & default_context(void)
Definition: cs_execution_context.cpp:62
mpi_wrapper & default_mpi(void)
Definition: cs_execution_context.cpp:78
static void sum(const cs_mpi_wrapper &mpi_w, T &first, Vals &... values)
Sum values of a given datatype over a given communicator.
Definition: cs_parall.h:893
Definition: cs_algorithm.h:51
bool parallel_for_reduce_sum(cs_lnum_t n, T &sum, F &&f, Args &&... args)
parallel_for_reduce_sum construct using a dispatch context based on template parameter (enum) "exec_t...
Definition: cs_execution_context.h:347
bool parallel_for_i_faces(const M *m, F &&f, Args &&... args)
parallel_for_i_faces construct using a dispatch context based on template parameter (enum) "exec_type...
Definition: cs_execution_context.h:286
exec_type
Definition: cs_execution_context.h:214
bool wait(void)
wait construct using a dispatch context based on template parameter of exec_type (enum)
Definition: cs_execution_context.h:543
bool parallel_for_reduce(cs_lnum_t n, T &result, R &reducer, F &&f, Args &&... args)
parallel_for_reduce construct using a dispatch context based on template parameter (enum) "exec_type"
Definition: cs_execution_context.h:442
bool parallel_for(cs_lnum_t n, F &&f, Args &&... args)
parallel_for construct using a dispatch context based on template parameter (enum) "exec_type"
Definition: cs_execution_context.h:229