DUNE-FEM (unstable)

threadsafevalue.hh
1#ifndef DUNE_FEM_THREADSAFEVALUES_HH
2#define DUNE_FEM_THREADSAFEVALUES_HH
3
4#include <vector>
5
6#include <dune/fem/misc/mpimanager.hh>
7
8namespace Dune {
9
10 namespace Fem {
11
12
16 template <class T>
18 {
19#ifdef USE_SMP_PARALLEL
20 const typename MPIManager::ThreadPoolType& threadPool_;
21 std::vector< T > value_;
22#else
23 T value_;
24#endif
25
26 int thread() const {
27#ifdef USE_SMP_PARALLEL
28 return threadPool_.thread();
29#else
30 return 0;
31#endif
32 }
33 public:
35 typedef T ValueType ;
36
38 template< class ...Args >
39 ThreadSafeValue( Args&& ...args )
40#ifdef USE_SMP_PARALLEL
41 : threadPool_( MPIManager::threadPool() ),
42 value_( threadPool_.maxThreads(), ValueType( std::forward< Args >( args )... ) )
43#else
44 : value_( std::forward< Args >( args )... )
45#endif
46 {}
47
50 :
51#ifdef USE_SMP_PARALLEL
52 threadPool_( MPIManager::threadPool() ),
53#endif
54 value_(
55#ifdef USE_SMP_PARALLEL
56 threadPool_.maxThreads()
57#endif
58 )
59 {}
60
62 size_t size() const {
63#ifdef USE_SMP_PARALLEL
64 return threadPool_.maxThreads();
65#else
66 return 1;
67#endif
68 }
69
71 ValueType& operator * () { return this->operator[]( thread() ); }
73 const ValueType& operator * () const { return this->operator[]( thread() ); }
74
75 operator const ValueType& () const { return this->operator[]( thread() ); }
76 operator ValueType& () { return this->operator[]( thread() ); }
77
79 ValueType& operator [] ( const unsigned int thread ) {
80 assert( thread < size() );
81#ifdef USE_SMP_PARALLEL
82 assert( thread < value_.size() );
83#endif
84 return value_
85#ifdef USE_SMP_PARALLEL
86 [ thread ]
87#endif
88 ;
89 }
90
92 const ValueType& operator [] ( const unsigned int thread ) const {
93 assert( thread < size() );
94#ifdef USE_SMP_PARALLEL
95 assert( thread < value_.size() );
96#endif
97 return value_
98#ifdef USE_SMP_PARALLEL
99 [ thread ]
100#endif
101 ;
102 }
103 };
104
105 } // end namespace Fem
106
107} // end namespace Dune
108
109
110#endif
ThreadSafeValue realizes thread safety for a given variable by creating an instance of this variable ...
Definition: threadsafevalue.hh:18
size_t size() const
return number of threads
Definition: threadsafevalue.hh:62
ValueType & operator*()
return reference to thread private value
Definition: threadsafevalue.hh:71
ValueType & operator[](const unsigned int thread)
return reference to private value for given thread number
Definition: threadsafevalue.hh:79
T ValueType
type of value to be thread safe
Definition: threadsafevalue.hh:35
ThreadSafeValue()
default constructor
Definition: threadsafevalue.hh:49
ThreadSafeValue(Args &&...args)
constructor initializing values for all threads given a init value
Definition: threadsafevalue.hh:39
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)