Geant4  10.03
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 #include "globals.hh"
67 
68 #ifdef g4cdebug
69 #include <iostream>
70 #include <sstream>
71 using std::cout;
72 using std::endl;
73 #endif
74 
75 // A TLS storage for a cache of type VALTYPE
76 template<class VALTYPE>
78 public:
79  inline void Initialize( unsigned int id );
80  // Initliaze TLS storage
81 
82  inline void Destroy( unsigned int id , G4bool last);
83  // Cleanup TLS storage for instance id. If last==true
84  // destroy and cleanup object container
85 
86  inline VALTYPE& GetCache(unsigned int id) const;
87  // Returns cached value for instance id
88 private:
89  typedef std::vector<VALTYPE*> cache_container;
90  // Implementation detail: the cached object is stored as a
91  // pointer. See Note (1)
92  static G4ThreadLocal cache_container *cache;
93 };
94 
95 // Note (1) : Object is stored as a pointer to avoid too large
96 // std::vector in case of stored objects and allow use
97 // of spcialized allocators
98 
99 // Template specialization for pointers
100 // Note that objects are not owned by cache, for this
101 // version of the cache the explicit new/delete of the
102 // cached object
103 template<class VALTYPE>
104 class G4CacheReference<VALTYPE*> {
105 public:
106  inline void Initialize( unsigned int id );
107 
108  inline void Destroy( unsigned int id , G4bool last);
109 
110  inline VALTYPE*& GetCache(unsigned int id) const;
111 
112 private:
113  typedef std::vector<VALTYPE*> cache_container;
114  static G4ThreadLocal cache_container *cache;
115 };
116 
117 // Template specialization for probably the most used case: double
118 // Be more efficient avoiding unnecessary "new/delete"
119 template<>
121 public:
122  inline void Initialize( unsigned int id );
123 
124  inline void Destroy( unsigned int id , G4bool last);
125 
126  inline G4double& GetCache(unsigned int id) const;
127 
128 private:
129  typedef std::vector<G4double> cache_container;
130  G4GLOB_DLL static G4ThreadLocal std::vector<G4double> *cache;
131 };
132 
133 
134 //================================
135 // Implementation details follow
136 //================================
137 
138 //======= Implementation: G4CacheReference<V>
139 
140 template<class V>
141 void G4CacheReference<V>::Initialize(unsigned int id)
142 {
143 #ifdef g4cdebug
144  if ( cache == 0 )
145  cout<<"Generic template"<<endl;
146 #endif
147  //Create cache container
148  if ( cache == 0 )
149  cache = new cache_container;
150  if ( cache->size() <= id )
151  cache->resize(id+1,static_cast<V*>(0));
152  if ( (*cache)[id] == 0 ) (*cache)[id]=new V;
153 
154 }
155 
156 template<class V>
157 void G4CacheReference<V>::Destroy( unsigned int id, G4bool last )
158 {
159  if ( cache ) {
160 #ifdef g4cdebug
161  cout<<"Destroying element"<<id<<" is last?"<<last<<endl;
162 #endif
163  if ( cache->size() < id ) {
165  msg<<"Internal fatal error. Invalid G4Cache size (requested id: "<<id<<" but cache has size: "<<cache->size();
166  msg<<" Possibly client created G4Cache object in a thread and tried to delete it from another thread!";
167  G4Exception("G4CacheReference<V>::Destroy","Cache001",FatalException,msg);
168  return;
169  }
170  if ( cache->size() > id && (*cache)[id] ) {
171  delete (*cache)[id];
172  (*cache)[id]=0;
173  }
174  if (last ) {
175  delete cache;
176  cache = 0;
177  }
178  }
179 }
180 
181 template<class V>
182 V& G4CacheReference<V>::GetCache(unsigned int id) const
183 {
184  return *(cache->operator[](id));
185 }
186 
187 template<class V>
189 
190 //======= Implementation: G4CacheReference<V*>
191 template<class V>
192 void G4CacheReference<V*>::Initialize( unsigned int id )
193 {
194 #ifdef g4cdebug
195  if ( cache == 0 )
196  cout<<"Pointer template"<<endl;
197 #endif
198  if ( cache == 0 )
199  cache = new cache_container;
200  if ( cache->size() <= id )
201  cache->resize(id+1,static_cast<V*>(0));
202  //No need to create pointee
203  //if ( (*cache)[id] == 0 ) (*cache)[id]=new VALTYPE;
204 }
205 
206 template<class V>
207 inline void G4CacheReference<V*>::Destroy( unsigned int id , G4bool last)
208 {
209  if ( cache ) {
210 #ifdef g4cdebug
211  cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
212 #endif
213  if ( cache->size() < id ) {
215  msg<<"Internal fatal error. Invalid G4Cache size (requested id: "<<id<<" but cache has size: "<<cache->size();
216  msg<<" Possibly client created G4Cache object in a thread and tried to delete it from another thread!";
217  G4Exception("G4CacheReference<V*>::Destroy","Cache001",FatalException,msg);
218  return;
219  }
220  if ( cache->size() > id && (*cache)[id] ) {
221  //Ownership is for client
222  //delete (*cache)[id];
223  (*cache)[id]=0;
224  }
225  if (last ) {
226  delete cache;
227  cache = 0;
228  }
229  }
230 }
231 
232 template<class V>
233 V*& G4CacheReference<V*>::GetCache(unsigned int id) const
234 {
235  return (cache->operator[](id));
236 }
237 
238 template<class V>
240 
241 //======= Implementation: G4CacheReference<double>
243 {
244 #ifdef g4cdebug
245  cout<<"Specialized template for G4double"<<endl;
246 #endif
247  if ( cache == 0 )
248  cache = new cache_container;
249  if ( cache->size() <= id )
250  cache->resize(id+1,static_cast<G4double>(0));
251 }
252 
253 #ifdef g4cdebug
254 void G4CacheReference<G4double>::Destroy( unsigned int id , G4bool last) {
255 #else
256 void G4CacheReference<G4double>::Destroy( unsigned int /*id*/ , G4bool last) {
257 #endif
258  if ( cache && last ) {
259 #ifdef g4cdebug
260  cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
261 #endif
262  delete cache;
263  cache = 0;
264  }
265 }
266 
268  return cache->operator[](id);
269 }
270 
271 //G4ThreadLocal std::vector<G4double>* G4CacheReference<G4double>::cache = 0;
272 
std::vector< VALTYPE * > cache_container
void Initialize(unsigned int id)
static G4ThreadLocal cache_container * cache
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void Initialize()
Definition: errprop.cc:101
std::vector< VALTYPE * > cache_container
static G4ThreadLocal cache_container * cache
#define G4ThreadLocal
Definition: tls.hh:89
#define G4GLOB_DLL
Definition: G4Types.hh:64
std::vector< G4double > cache_container
bool G4bool
Definition: G4Types.hh:79
static G4GLOB_DLL G4ThreadLocal std::vector< G4double > * cache
VALTYPE & GetCache(unsigned int id) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
double G4double
Definition: G4Types.hh:76
void Destroy(unsigned int id, G4bool last)