G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsThreaded.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #ifdef _OPENMP
17 #include <omp.h>
18 #endif
19 
20 namespace gismo
21 {
22 
23 namespace util
24 {
25 
26 // Usage:
27 // gsThreaded<C> a;
28 // a.mine();
29 template<class C, class Allocator = std::allocator<C> >
30 class gsThreaded
31 {
32 #ifdef _OPENMP
33  std::vector<C,Allocator> m_array;
34  #else
35  C m_c;
36 #endif
37 
38 public:
39 
40 #ifdef _OPENMP
41  gsThreaded() : m_array(omp_get_max_threads()) { }
42 
44  operator C&() { return m_array[omp_get_thread_num()]; }
45  operator const C&() const { return m_array[omp_get_thread_num()]; }
46 
48  C& mine() { return m_array[omp_get_thread_num()]; }
49  const C& mine() const { return m_array[omp_get_thread_num()]; }
50 
52  C& operator = (C other) { return m_array[omp_get_thread_num()] = give(other); }
53 #else
54  operator C&() { return m_c; }
56  operator const C&() const { return m_c; }
57 
59  C& mine() { return m_c; }
60  const C& mine() const { return m_c; }
61 
63  C& operator = (C other) { return m_c = give(other); }
64 #endif
65 
66 };//gsThreaded
67 
68 }//util
69 
70 }//gismo
S give(S &x)
Definition: gsMemory.h:266