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 std::vector< T > value_;
21#else
22 T value_;
23#endif
24 public:
26 typedef T ValueType ;
27
29 template< class ...Args >
30 ThreadSafeValue( Args&& ...args )
31#ifdef USE_SMP_PARALLEL
32 : value_( MPIManager::maxThreads(), ValueType( std::forward< Args >( args )... ) )
33#else
34 : value_( std::forward< Args >( args )... )
35#endif
36 {}
37
40 : value_(
41#ifdef USE_SMP_PARALLEL
42 MPIManager::maxThreads()
43#endif
44 )
45 {}
46
48 size_t size() const { return MPIManager::maxThreads(); }
49
51 ValueType& operator * () { return this->operator[]( MPIManager::thread() ); }
53 const ValueType& operator * () const { return this->operator[]( MPIManager::thread() ); }
54
55 operator const ValueType& () const { return this->operator[]( MPIManager::thread() ); }
56 operator ValueType& () { return this->operator[]( MPIManager::thread() ); }
57
59 ValueType& operator [] ( const unsigned int thread ) {
60 assert( thread < size() );
61#ifdef USE_SMP_PARALLEL
62 assert( thread < value_.size() );
63#endif
64 return value_
65#ifdef USE_SMP_PARALLEL
66 [ thread ]
67#endif
68 ;
69 }
70
72 const ValueType& operator [] ( const unsigned int thread ) const {
73 assert( thread < size() );
74#ifdef USE_SMP_PARALLEL
75 assert( thread < value_.size() );
76#endif
77 return value_
78#ifdef USE_SMP_PARALLEL
79 [ thread ]
80#endif
81 ;
82 }
83 };
84
85 } // end namespace Fem
86
87} // end namespace Dune
88
89
90#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:48
ValueType & operator*()
return reference to thread private value
Definition: threadsafevalue.hh:51
ValueType & operator[](const unsigned int thread)
return reference to private value for given thread number
Definition: threadsafevalue.hh:59
T ValueType
type of value to be thread safe
Definition: threadsafevalue.hh:26
ThreadSafeValue()
default constructor
Definition: threadsafevalue.hh:39
ThreadSafeValue(Args &&...args)
constructor initializing values for all threads given a init value
Definition: threadsafevalue.hh:30
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 27, 22:29, 2024)