Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4VUPLSplitter.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 //
27 // $Id: $
28 //
29 //
30 // ------------------------------------------------------------
31 //
32 // GEANT 4 class header file
33 //
34 // ---------------- G4UPLSplitter ----------------
35 //
36 // Utility template class for splitting RW data for thread-safety from
37 // classes: G4UserPhysicsList, G4VPhysicsConstructor and G4CModularPhsyicsList
38 //
39 // ------------------------------------------------------------
40 // History:
41 // 01.25.2009 Xin Dong: First implementation from automatic MT conversion.
42 // ------------------------------------------------------------
43 #ifndef G4VUPLSPLITTER_HH
44 #define G4VUPLSPLITTER_HH
45 
46 #include <stdlib.h>
47 
48 #include "globals.hh"
49 #include "rundefs.hh"
50 #include "G4AutoLock.hh"
51 //
52 // This class implements the split-mechanism for shared objects.
53 // Let's see how it works.
54 // In the split-class we have an instance of this class and an G4int instanceID
55 // Every time, in the master thread a new instance of the split-class
56 // is created the constructor calls:
57 // instanceID = g4vuplsplitter.CreateInstance();
58 // This creates in memory an "array", pointed by "sharedOffset" of capacity "totalspace"
59 // The array contains "totalobj" (<=totalspace) instances (i.e. the array has
60 // un-initialized spaces)
61 // Note that also the TLS variables "offset" and "workertotalspace" have also the same stuff
62 // When a worker thread is started we can call g4vuplsplitter.NewSubInstances()
63 // This will simply allocate enough space in the TLS space "offset" and call
64 // T::initialize() onto the new created methods.
65 // Alternatively one can call, when the worker thread start, g4vuplsplitter.workerCopySubInstanceArray()
66 // That will copy the content of master thread "array" into the TLS one
67 
68 // To see this stuff in action see:
69 // G4VUserPhysicsList class and G4WorkerThread classes.
70 
71 template <class T> // T is the private data from the object to be split
73 {
74  public:
75 
76  G4VUPLSplitter() : totalobj(0),totalspace(0),sharedOffset(0)
77  {
79  }
80 
82  // Invoked by the master thread to create a new subinstance
83  // whenever a new split class instance is created.
84  // This is called by constructor of shared classes, thus only master thread
85  // calls this
86  {
87  G4AutoLock l(&mutex);
88  //One more instance
89  totalobj++;
90  //If the number of objects is larger than the available spaces,
91  //a re-allocation is needed
92  if (totalobj > workertotalspace)
93  {
94  l.unlock();
96  l.lock();
97  }
98  //Since this is called by Master thread, we can remember this
99  totalspace = workertotalspace;
100  sharedOffset = offset;
101  return (totalobj - 1);
102  }
103 
105  // Invoked by each worker thread to grow the subinstance array and
106  // initialize each new subinstance using a particular method defined
107  // by the subclass.
108  {
109  G4AutoLock l(&mutex);
110  if (workertotalspace >= totalobj) { return; }
111  //Remember current large size
112  G4int originaltotalspace = workertotalspace;
113  //Increase its size by some value (purely arbitrary)
114  workertotalspace = totalobj + 512;
115  //Now re-allocate new space
116  offset = (T *) realloc(offset, workertotalspace * sizeof(T));
117  if (offset == 0)
118  {
119  G4Exception("G4VUPLSplitter::NewSubInstances()",
120  "OutOfMemory", FatalException, "Cannot malloc space!");
121  return;
122  }
123  //The newly created objects need to be initialized
124  for (G4int i = originaltotalspace; i < workertotalspace; i++)
125  {
126  offset[i].initialize();
127  }
128  }
129 
130  void FreeWorker()
131  // Invoked by all threads to free the subinstance array.
132  {
133  if (!offset) { return; }
134  free( offset);
135  offset = 0;
136  }
137 
138  T* GetOffset() { return offset; }
139 
140  void UseWorkArea( T* newOffset )
141  {
142  // Use recycled work area - which was created previously
143  if( offset && offset!=newOffset )
144  {
145  G4Exception("G4VUPLSplitter::UseWorkspace()",
146  "TwoWorkspaces", FatalException,
147  "Thread already has workspace - cannot use another.");
148  }
149  offset= newOffset;
150  // totalobj= numObjects;
151  // totalspace= numSpace;
152  }
153 
154  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
155  {
156  // Detach this thread from this Location
157  // The object which calls this method is responsible for it.
158  //
159  T* offsetRet= offset;
160 
161  offset= 0;
162 
163  return offsetRet;
164  }
165 
167  //Invoked by each worker thread to copy all subinstances array from
168  //the master thread
169  {
170  if ( offset ) return;
171  //Since this is called by worker threds, totalspace is some valid number > 0
172  //Remember totalspace is the number of availabel slots from master.
173  //We are sure that it has valid data
174  G4AutoLock l(&mutex);
175  offset = (T *)realloc(offset,totalspace * sizeof(T));
176  if (offset == 0)
177  {
178  G4Exception("G4VUPLSplitter::WorkerCopySubInstanceArray()",
179  "OutOfMemory", FatalException, "Cannot malloc space!");
180  return;
181  }
182  //Now just copy from master thread (sharedOffset)
183  memcpy(offset,sharedOffset,totalspace*sizeof(T));
184  }
185  public:
186 
187  G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace; //Per-thread available number of slots
188  G4RUN_DLL G4ThreadLocalStatic T* offset; //Pointer to first instance of an array
189 
190  private:
191  G4int totalobj; //Total number of instances from master thread
192  G4int totalspace; // Available number of "slots"
193  T* sharedOffset;
194  G4Mutex mutex;
195 };
196 
198 template<typename T> G4ThreadLocal T* G4VUPLSplitter<T>::offset=0;
199 
200 #endif
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
#define G4RUN_DLL
Definition: rundefs.hh:48
#define G4ThreadLocal
Definition: tls.hh:89
#define G4ThreadLocalStatic
Definition: tls.hh:88
int G4int
Definition: G4Types.hh:78
G4RUN_DLL G4ThreadLocalStatic T * offset
G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
G4int CreateSubInstance()
void WorkerCopySubInstanceArray()
void UseWorkArea(T *newOffset)