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