version 3.10-dev
initialize.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_COMMON_INITIALIZE_HH
13#define DUMUX_COMMON_INITIALIZE_HH
14
15#include <string>
16#include <algorithm>
17#include <cstdlib>
18
19#include <dune/common/parallel/mpihelper.hh>
20
21#if HAVE_TBB
22#include <oneapi/tbb/info.h>
23#include <oneapi/tbb/global_control.h>
24
25#ifndef DOXYGEN
26namespace Dumux::Detail {
27
28class TBBGlobalControl
29{
30public:
31 static oneapi::tbb::global_control& instance(int& argc, char* argv[])
32 {
33 int maxNumThreads = oneapi::tbb::info::default_concurrency();
34 if (const char* dumuxNumThreads = std::getenv("DUMUX_NUM_THREADS"))
35 maxNumThreads = std::max(1, std::stoi(std::string{ dumuxNumThreads }));
36
37 static oneapi::tbb::global_control global_limit(
38 oneapi::tbb::global_control::max_allowed_parallelism, maxNumThreads
39 );
40
41 return global_limit;
42 }
43};
44
45} // namespace Dumux::Detail
46#endif // DOXYGEN
47
48#endif // HAVE_TBB
49
50
51#if DUMUX_HAVE_OPENMP
52#include <omp.h>
53#endif // DUMUX_HAVE_OPENMP
54
55
56#if DUMUX_HAVE_KOKKOS
57#include <Kokkos_Core.hpp>
58
59#ifndef DOXYGEN
60namespace Dumux::Detail {
61
62class KokkosScopeGuard
63{
64public:
65 static Kokkos::ScopeGuard& instance(int& argc, char* argv[])
66 {
67 Kokkos::InitArguments arguments;
68 if (const char* dumuxNumThreads = std::getenv("DUMUX_NUM_THREADS"))
69 arguments.num_threads = std::max(1, std::stoi(std::string{ dumuxNumThreads }));
70
71 static Kokkos::ScopeGuard guard(arguments);
72 return guard;
73 }
74};
75
76} // namespace Dumux::Detail
77#endif // DOXYGEN
78
79#endif // DUMUX_HAVE_KOKKOS
80
81namespace Dumux {
82
83void initialize(int& argc, char* argv[])
84{
85 // initialize MPI if available
86 // otherwise this will create a sequential (fake) helper
87 Dune::MPIHelper::instance(argc, argv);
88
89#if HAVE_TBB
90 // initialize TBB and keep global control alive
91 Detail::TBBGlobalControl::instance(argc, argv);
92#endif
93
94#if DUMUX_HAVE_OPENMP
95 if (const char* dumuxNumThreads = std::getenv("DUMUX_NUM_THREADS"))
96 omp_set_num_threads(
97 std::max(1, std::stoi(std::string{ dumuxNumThreads }))
98 );
99#endif
100
101#if DUMUX_HAVE_KOKKOS
102 // initialize Kokkos (command line / environmental variable interface)
103 Detail::KokkosScopeGuard::instance(argc, argv);
104#endif
105}
106
107} // end namespace Dumux
108
109#endif
Distance implementation details.
Definition: cvfelocalresidual.hh:25
Definition: adapt.hh:17
void initialize(int &argc, char *argv[])
Definition: initialize.hh:83