Geant4  10.00.p03
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 
57  }
58 
60  // Invoked by the master or work thread to create a new subinstance
61  // whenever a new split class instance is created.
62  {
63  G4AutoLock l(&mutex);
64  totalobj++;
65  if (totalobj > totalspace)
66  {
67  totalspace=totalspace + 512;
68  offset = (T *) realloc(offset, totalspace * sizeof(T));
69  if (offset == 0)
70  {
71  G4Exception("G4GeomSPlitter::CreateSubInstance()",
72  "OutOfMemory", FatalException, "Cannot malloc space!");
73  }
75  }
76  return (totalobj - 1);
77  }
78 
80  {
81  G4AutoLock l(&mutex);
82  memcpy(offset, sharedOffset, totalspace * sizeof(T));
83  }
84 
86  // Invoked by each worker thread to copy all the subinstance array
87  // from the master thread.
88  {
89  G4AutoLock l(&mutex);
90  if (offset) { return; }
91  offset = (T *) realloc(offset, totalspace * sizeof(T));
92  if (offset == 0)
93  {
94  G4Exception("G4GeomSplitter::SlaveCopySubInstanceArray()",
95  "OutOfMemory", FatalException, "Cannot malloc space!");
96  }
97  l.unlock();
99  }
100 
102  // Invoked by each worker thread to create the subinstance array and
103  // initialize each subinstance using a particular method defined by
104  // the subclass.
105  {
106  G4AutoLock l(&mutex);
107  if (offset) { return; }
108  offset = (T *) realloc(offset, totalspace * sizeof(T));
109 
110  if (offset == 0)
111  {
112  G4Exception("G4GeomSplitter::SlaveInitializeSubInstance()",
113  "OutOfMemory", FatalException, "Cannot malloc space!");
114  }
115 
116  for (G4int i = 0 ; i < totalspace ; i++)
117  {
118  offset[i].initialize();
119  }
120  }
121 
123  // Invoked by each worker thread at start of a run (2nd or later)
124  // to copy again all the subinstance array from the master thread.
125  // To cope with user's changes in Geometry - e.g. change of material in a volume
126  {
127  G4AutoLock l(&mutex);
128  if (!offset) {
130  G4Exception("G4GeomSPlitter::SlaveReCopySubInstance()",
131  "MissingInitialisation", JustWarning,
132  "Must be called after Initialisation or first Copy.");
133  }
134  l.unlock();
136  }
137 
138  void FreeSlave()
139  // Invoked by all threads to free the subinstance array.
140  {
141  if (!offset) { return; }
142  free( offset );
143  offset = 0;
144  }
145 
146  //
147  // Extension - to allow sharing of workspaces - John Apostolakis 28 May 2013
148 
149  T* GetOffset() { return offset; }
150 
151  void UseWorkArea( T* newOffset ) // , G4int numObjects, G4int numSpace)
152  {
153  // Use recycled work area - which was created previously
154  if( offset && offset!=newOffset )
155  {
156  if( newOffset != offset )
157  {
158  G4Exception("G4GeomSplitter::UseWorkspace()",
159  "TwoWorkspaces", FatalException,
160  "Thread already has workspace - cannot use another.");
161  }
162  else
163  {
164  G4Exception("G4GeomSplitter::UseWorkspace()",
165  "TwoWorkspaces", JustWarning,
166  "Thread already has a workspace - trying to set the same again.");
167  }
168  }
169  offset= newOffset;
170  // totalobj= numObjects;
171  // totalspace= numSpace;
172  }
173 
174  T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
175  {
176  // Detach this thread from this Location
177  // The object which calls this method is responsible for it.
178  //
179  T* offsetRet= offset;
180 
181  offset= 0;
182 
183  return offsetRet;
184  }
185 
186  void Destroy()
187  {
188 
189  }
190 
191  public:
192 
194 
195  private:
196 
201 };
202 
203 #endif
void CopyMasterContents()
void free(void *__ptr)
Definition: hjmalloc.cc:140
#define G4MUTEXINIT(mutex)
Definition: G4Threading.hh:160
static G4GEOM_DLL G4ThreadLocal T * offset
#define G4ThreadLocal
Definition: tls.hh:52
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:156
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
void UseWorkArea(T *newOffset)
#define G4GEOM_DLL
Definition: geomwdefs.hh:48
void SlaveInitializeSubInstance()
void SlaveReCopySubInstanceArray()