Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GeomSplitter.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 // class G4LogicalVolume
31 //
32 // Class description:
33 //
34 // Utility template class for splitting of RW data for thread-safety from
35 // classes: G4LogicalVolume, G4Region, G4VPhysicalVolume, G4PolyconeSide
36 // G4PolyhedraSide, G4PVReplica.
37 
38 // Author:
39 // 01.25.09 X.Dong: Initial version from automatic MT conversion.
40 // ------------------------------------------------------------------------
41 #ifndef G4GEOMSPLITTER_HH
42 #define G4GEOMSPLITTER_HH
43 
44 #include <stdlib.h>
45 
46 #include "globals.hh"
47 #include "geomwdefs.hh"
48 #include "G4AutoLock.hh"
49 
50 template <class T> // T is the private data from the object to be split
52 {
53  public:
54 
56  : totalobj(0), totalspace(0), sharedOffset(0)
57  {
59  }
60 
62  // Invoked by the master or work thread to create a new subinstance
63  // whenever a new split class instance is created.
64  {
65  G4AutoLock l(&mutex);
66  totalobj++;
67  if (totalobj > totalspace)
68  {
69  totalspace=totalspace + 512;
70  offset = (T *) realloc(offset, totalspace * sizeof(T));
71  if (offset == 0)
72  {
73  G4Exception("G4GeomSPlitter::CreateSubInstance()",
74  "OutOfMemory", FatalException, "Cannot malloc space!");
75  }
76  sharedOffset = offset;
77  }
78  return (totalobj - 1);
79  }
80 
82  {
83  G4AutoLock l(&mutex);
84  memcpy(offset, sharedOffset, totalspace * sizeof(T));
85  }
86 
88  // Invoked by each worker thread to copy all the subinstance array
89  // from the master thread.
90  {
91  G4AutoLock l(&mutex);
92  if (offset) { return; }
93  offset = (T *) realloc(offset, totalspace * sizeof(T));
94  if (offset == 0)
95  {
96  G4Exception("G4GeomSplitter::SlaveCopySubInstanceArray()",
97  "OutOfMemory", FatalException, "Cannot malloc space!");
98  }
99  l.unlock();
101  }
102 
104  // Invoked by each worker thread to create the subinstance array and
105  // initialize each subinstance using a particular method defined by
106  // the subclass.
107  {
108  G4AutoLock l(&mutex);
109  if (offset) { return; }
110  offset = (T *) realloc(offset, totalspace * sizeof(T));
111 
112  if (offset == 0)
113  {
114  G4Exception("G4GeomSplitter::SlaveInitializeSubInstance()",
115  "OutOfMemory", FatalException, "Cannot malloc space!");
116  }
117 
118  for (G4int i = 0 ; i < totalspace ; i++)
119  {
120  offset[i].initialize();
121  }
122  }
123 
125  // Invoked by each worker thread at start of a run (2nd or later)
126  // to copy again all the subinstance array from the master thread.
127  // To cope with user's changes in Geometry - e.g. change of material in a volume
128  {
129  G4AutoLock l(&mutex);
130  if (!offset)
131  {
133  G4Exception("G4GeomSPlitter::SlaveReCopySubInstance()",
134  "MissingInitialisation", JustWarning,
135  "Must be called after Initialisation or first Copy.");
136  }
137  l.unlock();
139  }
140 
141  void FreeSlave()
142  // Invoked by all threads to free the subinstance array.
143  {
144  if (!offset) { return; }
145  free( offset );
146  offset = 0;
147  }
148 
149  //
150  // Extension - to allow sharing of workspaces - John Apostolakis 28 May 2013
151 
152  T* GetOffset() { return offset; }
153 
154  void UseWorkArea( T* newOffset ) // , G4int numObjects, G4int numSpace)
155  {
156  // Use recycled work area - which was created previously
157  if( offset && offset!=newOffset )
158  {
159  G4Exception("G4GeomSplitter::UseWorkspace()",
160  "TwoWorkspaces", FatalException,
161  "Thread already has workspace - cannot use another.");
162  }
163  offset= newOffset;
164  // totalobj= numObjects;
165  // totalspace= numSpace;
166  }
167 
168  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
169  {
170  // Detach this thread from this Location
171  // The object which calls this method is responsible for it.
172  //
173  T* offsetRet= offset;
174 
175  offset= 0;
176 
177  return offsetRet;
178  }
179 
180  void Destroy()
181  {
182 
183  }
184 
185  public:
186 
188 
189  private:
190 
191  G4int totalobj;
192  G4int totalspace;
193  T* sharedOffset;
194  G4Mutex mutex;
195 };
196 
197 template <typename T> G4ThreadLocal T* G4GeomSplitter<T>::offset = 0;
198 
199 #endif
void CopyMasterContents()
static G4GEOM_DLL G4ThreadLocal T * offset
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:177
#define G4ThreadLocal
Definition: tls.hh:89
G4int CreateSubInstance()
int G4int
Definition: G4Types.hh:78
void SlaveCopySubInstanceArray()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
void UseWorkArea(T *newOffset)
#define G4GEOM_DLL
Definition: geomwdefs.hh:48
void SlaveInitializeSubInstance()
void SlaveReCopySubInstanceArray()