Geant4  10.01.p02
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 
78  }
79 
81  // Invoked by the master thread to create a new subinstance
82  // whenever a new split class instance is created.
83  // This is called by constructor of shared classes, thus only master thread
84  // calls this
85  {
86  G4AutoLock l(&mutex);
87  //One more instance
88  totalobj++;
89  //If the number of objects is larger than the available spaces,
90  //a re-allocation is needed
91  if (totalobj > workertotalspace) {
92  l.unlock();
94  l.lock();
95  }
96  //Since this is called by Master thread, we can remember this
99  return (totalobj - 1);
100  }
101 
103  // Invoked by each worker thread to grow the subinstance array and
104  // initialize each new subinstance using a particular method defined
105  // by the subclass.
106  {
107  G4AutoLock l(&mutex);
108  if (workertotalspace >= totalobj) { return; }
109  //Remember current large size
110  G4int originaltotalspace = workertotalspace;
111  //Increase its size by some value (purely arbitrary)
112  workertotalspace = totalobj + 512;
113  //Now re-allocate new space
114  offset = (T *) realloc(offset, workertotalspace * sizeof(T));
115  if (offset == 0)
116  {
117  G4Exception("G4VUPLSplitter::NewSubInstances()",
118  "OutOfMemory", FatalException, "Cannot malloc space!");
119  return;
120  }
121  //The newly created objects need to be initialized
122  for (G4int i = originaltotalspace; i < workertotalspace; i++)
123  {
124  offset[i].initialize();
125  }
126  }
127 
128  void FreeWorker()
129  // Invoked by all threads to free the subinstance array.
130  {
131  if (!offset) { return; }
132  free( offset);
133  offset = 0;
134  }
135 
136  T* GetOffset() { return offset; }
137 
138  void UseWorkArea( T* newOffset )
139  {
140  // Use recycled work area - which was created previously
141  if( offset && offset!=newOffset )
142  {
143  if( newOffset != offset )
144  {
145  G4Exception("G4VUPLSplitter::UseWorkspace()",
146  "TwoWorkspaces", FatalException,
147  "Thread already has workspace - cannot use another.");
148  }
149  else
150  {
151  G4Exception("G4VUPLSplitter::UseWorkspace()",
152  "TwoWorkspaces", JustWarning,
153  "Thread already has a workspace - trying to set the same again.");
154  }
155  }
156  offset= newOffset;
157  // totalobj= numObjects;
158  // totalspace= numSpace;
159  }
160 
161  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
162  {
163  // Detach this thread from this Location
164  // The object which calls this method is responsible for it.
165  //
166  T* offsetRet= offset;
167 
168  offset= 0;
169 
170  return offsetRet;
171  }
172 
174  //Invoked by each worker thread to copy all subinstances array from
175  //the master thread
176  {
177  if ( offset ) return;
178  //Since this is called by worker threds, totalspace is some valid number > 0
179  //Remember totalspace is the number of availabel slots from master.
180  //We are sure that it has valid data
181  G4AutoLock l(&mutex);
182  offset = (T *)realloc(offset,totalspace * sizeof(T));
183  if (offset == 0)
184  {
185  G4Exception("G4VUPLSplitter::WorkerCopySubInstanceArray()",
186  "OutOfMemory", FatalException, "Cannot malloc space!");
187  return;
188  }
189  //Now just copy from master thread (sharedOffset)
190  memcpy(offset,sharedOffset,totalspace*sizeof(T));
191  }
192  public:
193 
194  G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace; //Per-thread available number of slots
195  G4RUN_DLL G4ThreadLocalStatic T* offset; //Pointer to first instance of an array
196 
197  private:
198  G4int totalobj; //Total number of instances from master thread
199  G4int totalspace; // Available number of "slots"
202 };
203 
204 #endif
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
#define G4RUN_DLL
Definition: rundefs.hh:48
#define G4ThreadLocalStatic
Definition: tls.hh:88
int G4int
Definition: G4Types.hh:78
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
G4RUN_DLL G4ThreadLocalStatic T * offset
G4int CreateSubInstance()
void WorkerCopySubInstanceArray()
G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace
void UseWorkArea(T *newOffset)