Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4CacheDetails.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id$
27 //
28 // ---------------------------------------------------------------
29 // GEANT 4 class header file
30 //
31 // Class Description:
32 // The classes contained in this header files are used by
33 // G4Cache to store a TLS instance of the cached object.
34 // These classes should not be used by client code, but
35 // are used by one of the G4Cache classes.
36 //
37 // This class is container of the cached value
38 // Not memory efficient, but CPU efficient (constant time access)
39 // A different version with a map instead of a vector should be
40 // memory efficient
41 // and less CPU efficient (log-time access).
42 // If really a lot of these objects are used
43 // we may want to consider the map version to save some memory
44 //
45 // These are simplified "split-classes" without any
46 // copy-from-master logic. Each cached object is associated
47 // a unique identified (an integer), that references an instance
48 // of the cached value (of template type VALTYPE) in a
49 // static TLS data structure
50 //
51 // In case the cache is used for a cached object the object class
52 // has to provide a default constructor. Alternatively pointers to
53 // objects can be stored in the cache and this limitation is removed
54 // but explicit handling of memory (new/delete) of cached object becomes
55 // client responsibility.
56 //
57 // History:
58 // 21 Oct 2013: A. Dotti - First implementation
59 //
60 // Todos: - Understand if map based class can be more efficent than
61 // vector one
62 // - Evaluate use of specialized allocator for TLS "new"
63 
64 #include <vector>
65 #include "G4Threading.hh"
66 
67 #ifdef g4cdebug
68 #include <iostream>
69 #include <sstream>
70 using std::cout;
71 using std::endl;
72 #endif
73 
74 // A TLS storage for a cache of type VALTYPE
75 template<class VALTYPE>
77 public:
78  inline void Initialize( unsigned int id );
79  // Initliaze TLS storage
80 
81  inline void Destroy( unsigned int id , G4bool last);
82  // Cleanup TLS storage for instance id. If last==true
83  // destroy and cleanup object container
84 
85  inline VALTYPE& GetCache(unsigned int id) const;
86  // Returns cached value for instance id
87 private:
88  typedef std::vector<VALTYPE*> cache_container;
89  // Implementation detail: the cached object is stored as a
90  // pointer. See Note (1)
91  static G4ThreadLocal cache_container *cache;
92 };
93 
94 // Note (1) : Object is stored as a pointer to avoid too large
95 // std::vector in case of stored objects and allow use
96 // of spcialized allocators
97 
98 // Template specialization for pointers
99 // Note that objects are not owned by cache, for this
100 // version of the cache the explicit new/delete of the
101 // cached object
102 template<class VALTYPE>
103 class G4CacheReference<VALTYPE*> {
104 public:
105  inline void Initialize( unsigned int id );
106 
107  inline void Destroy( unsigned int id , G4bool last);
108 
109  inline VALTYPE*& GetCache(unsigned int id) const;
110 
111 private:
112  typedef std::vector<VALTYPE*> cache_container;
113  static G4ThreadLocal cache_container *cache;
114 };
115 
116 // Template specialization for probably the most used case: double
117 // Be more efficient avoiding unnecessary "new/delete"
118 template<>
120 public:
121  inline void Initialize( unsigned int id );
122 
123  inline void Destroy( unsigned int id , G4bool last);
124 
125  inline G4double& GetCache(unsigned int id) const;
126 
127 private:
128  typedef std::vector<G4double> cache_container;
129  static G4ThreadLocal std::vector<G4double> *cache;
130 };
131 
132 
133 //================================
134 // Implementation details follow
135 //================================
136 
137 //======= Implementation: G4CacheReference<V>
138 
139 template<class V>
140 void G4CacheReference<V>::Initialize(unsigned int id)
141 {
142 #ifdef g4cdebug
143  if ( cache == 0 )
144  cout<<"Generic template"<<endl;
145 #endif
146  //Create cache container
147  if ( cache == 0 )
148  cache = new cache_container;
149  if ( cache->size() <= id )
150  cache->resize(id+1,static_cast<V*>(0));
151  if ( (*cache)[id] == 0 ) (*cache)[id]=new V;
152 
153 }
154 
155 template<class V>
156 void G4CacheReference<V>::Destroy( unsigned int id, G4bool last )
157 {
158  if ( cache ) {
159 #ifdef g4cdebug
160  cout<<"Destroying element"<<id<<" is last?"<<last<<endl;
161 #endif
162  if ( cache->size() <id && (*cache)[id] ) {
163  delete (*cache)[id];
164  (*cache)[id]=0;
165  }
166  if (last ) {
167  delete cache;
168  cache = 0;
169  }
170  }
171 }
172 
173 template<class V>
174 V& G4CacheReference<V>::GetCache(unsigned int id) const
175 {
176  return *(cache->operator[](id));
177 }
178 
179 template<class V>
180 G4ThreadLocal typename G4CacheReference<V>::cache_container * G4CacheReference<V>::cache = 0;
181 
182 //======= Implementation: G4CacheReference<V*>
183 template<class V>
184 void G4CacheReference<V*>::Initialize( unsigned int id )
185 {
186 #ifdef g4cdebug
187  if ( cache == 0 )
188  cout<<"Pointer template"<<endl;
189 #endif
190  if ( cache == 0 )
191  cache = new cache_container;
192  if ( cache->size() <= id )
193  cache->resize(id+1,static_cast<V*>(0));
194  //No need to create pointee
195  //if ( (*cache)[id] == 0 ) (*cache)[id]=new VALTYPE;
196 }
197 
198 template<class V>
199 inline void G4CacheReference<V*>::Destroy( unsigned int id , G4bool last)
200 {
201  if ( cache ) {
202 #ifdef g4cdebug
203  cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
204 #endif
205  if ( cache->size() <id && (*cache)[id] ) {
206  //Ownership is for client
207  //delete (*cache)[id];
208  (*cache)[id]=0;
209  }
210  if (last ) {
211  delete cache;
212  cache = 0;
213  }
214  }
215 }
216 
217 template<class V>
218 V*& G4CacheReference<V*>::GetCache(unsigned int id) const
219 {
220  return (cache->operator[](id));
221 }
222 
223 template<class V>
224 G4ThreadLocal typename G4CacheReference<V*>::cache_container * G4CacheReference<V*>::cache=0;
225 
226 //======= Implementation: G4CacheReference<double>
228 {
229 #ifdef g4cdebug
230  cout<<"Specialized template for G4double"<<endl;
231 #endif
232  if ( cache == 0 )
233  cache = new cache_container;
234  if ( cache->size() <= id )
235  cache->resize(id+1,static_cast<G4double>(0));
236 }
237 
238 void G4CacheReference<G4double>::Destroy( unsigned int /*id*/ , G4bool last) {
239  if ( cache && last ) {
240 #ifdef g4cdebug
241  cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
242 #endif
243  delete cache;
244  cache = 0;
245  }
246 }
247 
249  return cache->operator[](id);
250 }
251 
252 //G4ThreadLocal std::vector<G4double>* G4CacheReference<G4double>::cache = 0;
253 
void Initialize(unsigned int id)
void Initialize()
Definition: errprop.cc:96
#define G4ThreadLocal
Definition: tls.hh:52
bool G4bool
Definition: G4Types.hh:79
VALTYPE & GetCache(unsigned int id) const
double G4double
Definition: G4Types.hh:76
void Destroy(unsigned int id, G4bool last)