Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
omp.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 /// \file This file defines a small wrapper class which stores thread local
22 /// instances of a given object.
23 
24 #ifndef FUB_EXT_OMP_HPP
25 #define FUB_EXT_OMP_HPP
26 
27 #include "fub/core/assert.hpp"
28 
29 #if defined(_OPENMP)
30 extern "C" {
31 #include <omp.h>
32 }
33 #endif
34 
35 #include <algorithm>
36 #include <numeric>
37 #include <functional>
38 #include <vector>
39 
40 namespace fub {
41 
42 template <typename T, typename Allocator = std::allocator<T>> class OmpLocal {
43 public:
44  OmpLocal() = default;
45  explicit OmpLocal(const T& value, Allocator alloc = Allocator());
46 
47  T& Get() noexcept;
48  const T& Get() const noexcept;
49 
50  T* operator->() noexcept;
51  const T* operator->() const noexcept;
52 
53  T& operator*() noexcept;
54  const T& operator*() const noexcept;
55 
56  const T& Min() const noexcept;
57 
58  template <typename BinaryOperation = std::plus<>>
59  auto Accumulate(BinaryOperation op = BinaryOperation()) const noexcept;
60 
61 private:
62  std::vector<T, Allocator> instances_{};
63 };
64 
65 #if defined(_OPENMP)
66 template <typename T, typename Allocator>
67 OmpLocal<T, Allocator>::OmpLocal(const T& value, Allocator alloc)
68  : instances_(static_cast<std::size_t>(::omp_get_max_threads()), value,
69  alloc) {}
70 
71 template <typename T, typename Allocator>
72 T& OmpLocal<T, Allocator>::Get() noexcept {
73  const int thread_num = ::omp_get_thread_num();
74  FUB_ASSERT(thread_num >= 0);
75  const std::size_t index = static_cast<std::size_t>(thread_num);
76  return instances_[index];
77 }
78 
79 template <typename T, typename Allocator>
80 const T& OmpLocal<T, Allocator>::Get() const noexcept {
81  const int thread_num = ::omp_get_thread_num();
82  FUB_ASSERT(thread_num >= 0);
83  const std::size_t index = static_cast<std::size_t>(thread_num);
84  return instances_[index];
85 }
86 #else
87 template <typename T, typename Allocator>
88 OmpLocal<T, Allocator>::OmpLocal(const T& value, Allocator alloc)
89  : instances_(1, value, alloc) {}
90 
91 template <typename T, typename Allocator>
93  return instances_[0];
94 }
95 
96 template <typename T, typename Allocator>
97 const T& OmpLocal<T, Allocator>::Get() const noexcept {
98  return instances_[0];
99 }
100 #endif
101 
102 template <typename T, typename Allocator>
104  return &Get();
105 }
106 
107 template <typename T, typename Allocator>
108 const T* OmpLocal<T, Allocator>::operator->() const noexcept {
109  return &Get();
110 }
111 
112 template <typename T, typename Allocator>
114  return Get();
115 }
116 
117 template <typename T, typename Allocator>
118 const T& OmpLocal<T, Allocator>::operator*() const noexcept {
119  return Get();
120 }
121 
122 template <typename T, typename Allocator>
123 const T& OmpLocal<T, Allocator>::Min() const noexcept {
124  return *std::min_element(instances_.begin(), instances_.end());
125 }
126 
127 template <typename T, typename Allocator>
128 template <typename BinaryOperator>
129 auto OmpLocal<T, Allocator>::Accumulate(BinaryOperator op) const noexcept {
130  FUB_ASSERT(!instances_.empty());
131  return std::accumulate(instances_.begin() + 1, instances_.end(), instances_[0], op);
132 }
133 
134 } // namespace fub
135 
136 #endif
#define FUB_ASSERT(x)
Definition: assert.hpp:39
Definition: omp.hpp:42
std::vector< T, Allocator > instances_
Definition: omp.hpp:62
OmpLocal(const T &value, Allocator alloc=Allocator())
Definition: omp.hpp:88
T & Get() noexcept
Definition: omp.hpp:92
T * operator->() noexcept
Definition: omp.hpp:103
T & operator*() noexcept
Definition: omp.hpp:113
auto Accumulate(BinaryOperation op=BinaryOperation()) const noexcept
OmpLocal()=default
const T & Min() const noexcept
Definition: omp.hpp:123
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
std::ptrdiff_t index
Definition: type_traits.hpp:179