GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
parallel_execution_omp.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_OMP_PARALLEL_EXECUTION_OMP_H
22 #define GRPPI_OMP_PARALLEL_EXECUTION_OMP_H
23 
24 #ifdef GRPPI_OMP
25 
26 #include "../common/mpmc_queue.h"
27 
28 #include <type_traits>
29 
30 #include <omp.h>
31 
32 
33 namespace grppi {
34 
41 
42 public:
52  parallel_execution_omp{impl_concurrency_degree()}
53  {}
54 
67  parallel_execution_omp(int concurrency_degree, bool order = true) noexcept :
68  concurrency_degree_{concurrency_degree},
69  ordering_{order}
70  {
71  omp_set_num_threads(concurrency_degree_);
72  }
73 
77  void set_concurrency_degree(int degree) noexcept {
78  concurrency_degree_ = degree;
79  omp_set_num_threads(concurrency_degree_);
80  }
81 
85  int concurrency_degree() const noexcept {
86  return concurrency_degree_;
87  }
88 
92  void enable_ordering() noexcept { ordering_=true; }
93 
97  void disable_ordering() noexcept { ordering_=false; }
98 
102  bool is_ordered() const noexcept { return ordering_; }
103 
107  void set_queue_attributes(int size, queue_mode mode) noexcept {
108  queue_size_ = size;
109  queue_mode_ = mode;
110  }
111 
118  template <typename T>
120  return {queue_size_, queue_mode_};
121  }
122 
126  int get_thread_id() const noexcept {
127  int result;
128  #pragma omp parallel
129  {
130  result = omp_get_thread_num();
131  }
132  return result;
133  }
134 
135 private:
136 
144  static int impl_concurrency_degree() {
145  int result;
146  #pragma omp parallel
147  {
148  result = omp_get_num_threads();
149  }
150  return result;
151  }
152 
153 private:
154 
155  int concurrency_degree_;
156 
157  bool ordering_;
158 
159  constexpr static int default_queue_size = 100;
160  int queue_size_ = default_queue_size;
161 
162  queue_mode queue_mode_ = queue_mode::blocking;
163 };
164 
169 template <typename E>
170 constexpr bool is_parallel_execution_omp() {
171  return std::is_same<E, parallel_execution_omp>::value;
172 }
173 
178 template <typename E>
179 constexpr bool is_supported();
180 
185 template <>
187  return true;
188 }
189 
190 } // end namespace grppi
191 
192 #else // GRPPI_OMP undefined
193 
194 namespace grppi {
195 
196 
199 struct parallel_execution_omp {};
200 
206 template <typename E>
207 constexpr bool is_parallel_execution_omp() {
208  return false;
209 }
210 
215 template <typename E>
216 constexpr bool is_supported();
217 
221 template <>
222 constexpr bool is_supported<parallel_execution_omp>() {
223  return false;
224 }
225 
226 }
227 
228 #endif // GRPPI_OMP
229 
230 #endif
Definition: callable_traits.h:24
parallel_execution_omp(int concurrency_degree, bool order=true) noexcept
Set num_threads to _threads in order to run in parallel.
Definition: parallel_execution_omp.h:67
bool is_ordered() const noexcept
Is execution ordered.
Definition: parallel_execution_omp.h:102
constexpr bool is_supported()
Metafunction that determines if type E is supported in the current build.
void disable_ordering() noexcept
Disable ordering.
Definition: parallel_execution_omp.h:97
parallel_execution_omp() noexcept
Default construct an OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:51
int get_thread_id() const noexcept
Get index of current thread in the thread table.
Definition: parallel_execution_omp.h:126
constexpr bool is_supported< parallel_execution_omp >()
Specialization stating that parallel_execution_omp is supported. This metafunction evaluates to false...
Definition: parallel_execution_omp.h:186
constexpr bool is_parallel_execution_omp()
Metafunction that determines if type E is parallel_execution_omp.
Definition: parallel_execution_omp.h:170
queue_mode
Definition: mpmc_queue.h:35
Definition: mpmc_queue.h:38
mpmc_queue< T > make_queue() const
Makes a communication queue for elements of type T.
Definition: parallel_execution_omp.h:119
void set_concurrency_degree(int degree) noexcept
Set number of grppi threads.
Definition: parallel_execution_omp.h:77
void enable_ordering() noexcept
Enable ordering.
Definition: parallel_execution_omp.h:92
OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:40
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_omp.h:85
void set_queue_attributes(int size, queue_mode mode) noexcept
Sets the attributes for the queues built through make_queue<T>(()
Definition: parallel_execution_omp.h:107