GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/common/initialize.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 4 6 66.7%
Functions: 1 1 100.0%
Branches: 1 14 7.1%

Line Branch Exec Source
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 //
7 /*!
8 * \file
9 * \ingroup Core
10 * \brief The initialize function to be called before using Dumux
11 */
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
26 namespace Dumux::Detail {
27
28 class TBBGlobalControl
29 {
30 public:
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
60 namespace Dumux::Detail {
61
62 class KokkosScopeGuard
63 {
64 public:
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
81 namespace Dumux {
82
83 641 void initialize(int& argc, char* argv[])
84 {
85 // initialize MPI if available
86 // otherwise this will create a sequential (fake) helper
87 641 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 641 times.
641 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 641 }
106
107 } // end namespace Dumux
108
109 #endif
110