Geant4
10.00.p02
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
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 "slavetotalspace" 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.SlaveCopySubInstanceArray()
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
72
class
G4VUPLSplitter
73
{
74
public
:
75
76
G4VUPLSplitter
() :
totalobj
(0),
totalspace
(0),
sharedOffset
(0) {}
77
78
G4int
CreateSubInstance
()
79
// Invoked by the master thread to create a new subinstance
80
// whenever a new split class instance is created.
81
// This is called by constructor of shared classes, thus only master thread
82
// calls this
83
{
84
//One more instance
85
totalobj
++;
86
//If the number of objects is larger than the available spaces,
87
//a re-allocation is needed
88
if
(
totalobj
>
slavetotalspace
) {
NewSubInstances
(); }
89
//Since this is called by Master thread, we can remember this
90
totalspace
=
slavetotalspace
;
91
sharedOffset
=
offset
;
92
return
(
totalobj
- 1);
93
}
94
95
void
NewSubInstances
()
96
// Invoked by each worker thread to grow the subinstance array and
97
// initialize each new subinstance using a particular method defined
98
// by the subclass.
99
{
100
if
(
slavetotalspace
>=
totalobj
) {
return
; }
101
//Remember current large size
102
G4int
originaltotalspace =
slavetotalspace
;
103
//Increase its size by some value (purely arbitrary)
104
slavetotalspace
=
totalobj
+ 512;
105
//Now re-allocate new space
106
offset
= (T *)
realloc
(
offset
,
slavetotalspace
*
sizeof
(T));
107
if
(
offset
== 0)
108
{
109
G4Exception
(
"G4VUPLSplitter::NewSubInstances()"
,
110
"OutOfMemory"
,
FatalException
,
"Cannot malloc space!"
);
111
return
;
112
}
113
//The newly created objects need to be initialized
114
for
(
G4int
i = originaltotalspace; i <
slavetotalspace
; i++)
115
{
116
offset
[i].initialize();
117
}
118
}
119
120
void
FreeSlave
()
121
// Invoked by all threads to free the subinstance array.
122
{
123
if
(!
offset
) {
return
; }
124
free
(
offset
);
125
offset
= 0;
126
}
127
128
void
SlaveCopySubInstanceArray
()
129
//Invoked by each worker thread to copy all subinstances array from
130
//the master thread
131
{
132
if
(
offset
)
return
;
133
//Since this is called by worker threds, totalspace is some valid number > 0
134
//Remember totalspace is the number of availabel slots from master.
135
//We are sure that it has valid data
136
offset
= (T *)
realloc
(
offset
,
totalspace
*
sizeof
(T));
137
if
(
offset
== 0)
138
{
139
G4Exception
(
"G4VUPLSplitter::SlaveCopySubInstanceArray()"
,
140
"OutOfMemory"
,
FatalException
,
"Cannot malloc space!"
);
141
return
;
142
}
143
//Now just copy from master thread (sharedOffset)
144
memcpy(
offset
,
sharedOffset
,
totalspace
*
sizeof
(T));
145
}
146
public
:
147
148
G4RUN_DLL
static
G4ThreadLocal
G4int
slavetotalspace
;
//Per-thread available number of slots
149
G4RUN_DLL
static
G4ThreadLocal
T*
offset
;
//Pointer to first instance of an array
150
151
private
:
152
153
G4int
totalobj
;
//Total number of instances from master thread
154
G4int
totalspace
;
// Available number of "slots"
155
T*
sharedOffset
;
156
};
157
158
#endif
G4VUPLSplitter
Definition:
G4VUPLSplitter.hh:72
G4VUPLSplitter::totalspace
G4int totalspace
Definition:
G4VUPLSplitter.hh:154
free
void free(void *__ptr)
Definition:
hjmalloc.cc:140
G4RUN_DLL
#define G4RUN_DLL
Definition:
rundefs.hh:48
G4VUPLSplitter::FreeSlave
void FreeSlave()
Definition:
G4VUPLSplitter.hh:120
G4ThreadLocal
#define G4ThreadLocal
Definition:
tls.hh:52
G4int
int G4int
Definition:
G4Types.hh:78
rundefs.hh
globals.hh
G4VUPLSplitter::NewSubInstances
void NewSubInstances()
Definition:
G4VUPLSplitter.hh:95
G4VUPLSplitter::sharedOffset
T * sharedOffset
Definition:
G4VUPLSplitter.hh:155
G4Exception
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition:
G4Exception.cc:41
G4VUPLSplitter::offset
static G4RUN_DLL G4ThreadLocal T * offset
Definition:
G4VUPLSplitter.hh:149
FatalException
Definition:
G4ExceptionSeverity.hh:60
G4VUPLSplitter::totalobj
G4int totalobj
Definition:
G4VUPLSplitter.hh:153
G4VUPLSplitter::SlaveCopySubInstanceArray
void SlaveCopySubInstanceArray()
Definition:
G4VUPLSplitter.hh:128
G4VUPLSplitter::CreateSubInstance
G4int CreateSubInstance()
Definition:
G4VUPLSplitter.hh:78
realloc
void * realloc(void *__ptr, size_t __size)
Definition:
hjmalloc.cc:103
G4VUPLSplitter::slavetotalspace
static G4RUN_DLL G4ThreadLocal G4int slavetotalspace
Definition:
G4VUPLSplitter.hh:148
G4VUPLSplitter::G4VUPLSplitter
G4VUPLSplitter()
Definition:
G4VUPLSplitter.hh:76
geant4.10.00.p02
source
run
include
G4VUPLSplitter.hh
Generated on Thu Dec 31 2015 10:41:19 for Geant4 by
1.8.8