Import Geant4 10.7.0.beta source tree
This commit is contained in:
@@ -25,10 +25,10 @@
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// ------------------------------------------------------------
|
||||
//
|
||||
// GEANT 4 class header file
|
||||
// GEANT 4 class header file
|
||||
//
|
||||
// ---------------- G4UPLSplitter ----------------
|
||||
//
|
||||
@@ -44,9 +44,9 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "G4AutoLock.hh"
|
||||
#include "globals.hh"
|
||||
#include "rundefs.hh"
|
||||
#include "G4AutoLock.hh"
|
||||
//
|
||||
// This class implements the split-mechanism for shared objects.
|
||||
// Let's see how it works.
|
||||
@@ -54,15 +54,15 @@
|
||||
// Every time, in the master thread a new instance of the split-class
|
||||
// is created the constructor calls:
|
||||
// instanceID = g4vuplsplitter.CreateInstance();
|
||||
// This creates in memory an "array", pointed by "sharedOffset" of capacity "totalspace"
|
||||
// The array contains "totalobj" (<=totalspace) instances (i.e. the array has
|
||||
// un-initialized spaces)
|
||||
// Note that also the TLS variables "offset" and "workertotalspace" have also the same stuff
|
||||
// When a worker thread is started we can call g4vuplsplitter.NewSubInstances()
|
||||
// This will simply allocate enough space in the TLS space "offset" and call
|
||||
// T::initialize() onto the new created methods.
|
||||
// Alternatively one can call, when the worker thread start, g4vuplsplitter.workerCopySubInstanceArray()
|
||||
// That will copy the content of master thread "array" into the TLS one
|
||||
// This creates in memory an "array", pointed by "sharedOffset" of capacity
|
||||
// "totalspace" The array contains "totalobj" (<=totalspace) instances (i.e. the
|
||||
// array has un-initialized spaces) Note that also the TLS variables "offset"
|
||||
// and "workertotalspace" have also the same stuff When a worker thread is
|
||||
// started we can call g4vuplsplitter.NewSubInstances() This will simply
|
||||
// allocate enough space in the TLS space "offset" and call T::initialize() onto
|
||||
// the new created methods. Alternatively one can call, when the worker thread
|
||||
// start, g4vuplsplitter.workerCopySubInstanceArray() That will copy the content
|
||||
// of master thread "array" into the TLS one
|
||||
|
||||
// To see this stuff in action see:
|
||||
// G4VUserPhysicsList class and G4WorkerThread classes.
|
||||
@@ -70,130 +70,143 @@
|
||||
template <class T> // T is the private data from the object to be split
|
||||
class G4VUPLSplitter
|
||||
{
|
||||
public:
|
||||
public:
|
||||
G4VUPLSplitter()
|
||||
: totalobj(0)
|
||||
, totalspace(0)
|
||||
, sharedOffset(0)
|
||||
{
|
||||
G4MUTEXINIT(mutex);
|
||||
}
|
||||
|
||||
G4VUPLSplitter() : totalobj(0),totalspace(0),sharedOffset(0)
|
||||
G4int CreateSubInstance()
|
||||
// Invoked by the master thread to create a new subinstance
|
||||
// whenever a new split class instance is created.
|
||||
// This is called by constructor of shared classes, thus only master thread
|
||||
// calls this
|
||||
{
|
||||
G4AutoLock l(&mutex);
|
||||
// One more instance
|
||||
totalobj++;
|
||||
// If the number of objects is larger than the available spaces,
|
||||
// a re-allocation is needed
|
||||
if(totalobj > workertotalspace)
|
||||
{
|
||||
G4MUTEXINIT(mutex);
|
||||
l.unlock();
|
||||
NewSubInstances();
|
||||
l.lock();
|
||||
}
|
||||
// Since this is called by Master thread, we can remember this
|
||||
totalspace = workertotalspace;
|
||||
sharedOffset = offset;
|
||||
return (totalobj - 1);
|
||||
}
|
||||
|
||||
G4int CreateSubInstance()
|
||||
// Invoked by the master thread to create a new subinstance
|
||||
// whenever a new split class instance is created.
|
||||
// This is called by constructor of shared classes, thus only master thread
|
||||
// calls this
|
||||
void NewSubInstances()
|
||||
// Invoked by each worker thread to grow the subinstance array and
|
||||
// initialize each new subinstance using a particular method defined
|
||||
// by the subclass.
|
||||
{
|
||||
G4AutoLock l(&mutex);
|
||||
if(workertotalspace >= totalobj)
|
||||
{
|
||||
G4AutoLock l(&mutex);
|
||||
//One more instance
|
||||
totalobj++;
|
||||
//If the number of objects is larger than the available spaces,
|
||||
//a re-allocation is needed
|
||||
if (totalobj > workertotalspace)
|
||||
{
|
||||
l.unlock();
|
||||
NewSubInstances();
|
||||
l.lock();
|
||||
}
|
||||
//Since this is called by Master thread, we can remember this
|
||||
totalspace = workertotalspace;
|
||||
sharedOffset = offset;
|
||||
return (totalobj - 1);
|
||||
return;
|
||||
}
|
||||
// Remember current large size
|
||||
G4int originaltotalspace = workertotalspace;
|
||||
// Increase its size by some value (purely arbitrary)
|
||||
workertotalspace = totalobj + 512;
|
||||
// Now re-allocate new space
|
||||
offset = (T*) realloc(offset, workertotalspace * sizeof(T));
|
||||
if(offset == 0)
|
||||
{
|
||||
G4Exception("G4VUPLSplitter::NewSubInstances()", "OutOfMemory",
|
||||
FatalException, "Cannot malloc space!");
|
||||
return;
|
||||
}
|
||||
// The newly created objects need to be initialized
|
||||
for(G4int i = originaltotalspace; i < workertotalspace; i++)
|
||||
{
|
||||
offset[i].initialize();
|
||||
}
|
||||
}
|
||||
|
||||
void NewSubInstances()
|
||||
// Invoked by each worker thread to grow the subinstance array and
|
||||
// initialize each new subinstance using a particular method defined
|
||||
// by the subclass.
|
||||
void FreeWorker()
|
||||
// Invoked by all threads to free the subinstance array.
|
||||
{
|
||||
if(!offset)
|
||||
{
|
||||
G4AutoLock l(&mutex);
|
||||
if (workertotalspace >= totalobj) { return; }
|
||||
//Remember current large size
|
||||
G4int originaltotalspace = workertotalspace;
|
||||
//Increase its size by some value (purely arbitrary)
|
||||
workertotalspace = totalobj + 512;
|
||||
//Now re-allocate new space
|
||||
offset = (T *) realloc(offset, workertotalspace * sizeof(T));
|
||||
if (offset == 0)
|
||||
{
|
||||
G4Exception("G4VUPLSplitter::NewSubInstances()",
|
||||
"OutOfMemory", FatalException, "Cannot malloc space!");
|
||||
return;
|
||||
}
|
||||
//The newly created objects need to be initialized
|
||||
for (G4int i = originaltotalspace; i < workertotalspace; i++)
|
||||
{
|
||||
offset[i].initialize();
|
||||
}
|
||||
return;
|
||||
}
|
||||
free(offset);
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
void FreeWorker()
|
||||
// Invoked by all threads to free the subinstance array.
|
||||
{
|
||||
if (!offset) { return; }
|
||||
free( offset);
|
||||
offset = 0;
|
||||
}
|
||||
T* GetOffset() { return offset; }
|
||||
|
||||
T* GetOffset() { return offset; }
|
||||
|
||||
void UseWorkArea( T* newOffset )
|
||||
void UseWorkArea(T* newOffset)
|
||||
{
|
||||
// Use recycled work area - which was created previously
|
||||
if(offset && offset != newOffset)
|
||||
{
|
||||
// Use recycled work area - which was created previously
|
||||
if( offset && offset!=newOffset )
|
||||
{
|
||||
G4Exception("G4VUPLSplitter::UseWorkspace()",
|
||||
"TwoWorkspaces", FatalException,
|
||||
"Thread already has workspace - cannot use another.");
|
||||
}
|
||||
offset= newOffset;
|
||||
// totalobj= numObjects;
|
||||
// totalspace= numSpace;
|
||||
G4Exception("G4VUPLSplitter::UseWorkspace()", "TwoWorkspaces",
|
||||
FatalException,
|
||||
"Thread already has workspace - cannot use another.");
|
||||
}
|
||||
|
||||
T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
|
||||
{
|
||||
// Detach this thread from this Location
|
||||
// The object which calls this method is responsible for it.
|
||||
//
|
||||
T* offsetRet= offset;
|
||||
|
||||
offset= 0;
|
||||
|
||||
return offsetRet;
|
||||
}
|
||||
|
||||
void WorkerCopySubInstanceArray()
|
||||
//Invoked by each worker thread to copy all subinstances array from
|
||||
//the master thread
|
||||
{
|
||||
if ( offset ) return;
|
||||
//Since this is called by worker threds, totalspace is some valid number > 0
|
||||
//Remember totalspace is the number of availabel slots from master.
|
||||
//We are sure that it has valid data
|
||||
G4AutoLock l(&mutex);
|
||||
offset = (T *)realloc(offset,totalspace * sizeof(T));
|
||||
if (offset == 0)
|
||||
{
|
||||
G4Exception("G4VUPLSplitter::WorkerCopySubInstanceArray()",
|
||||
"OutOfMemory", FatalException, "Cannot malloc space!");
|
||||
return;
|
||||
}
|
||||
//Now just copy from master thread (sharedOffset)
|
||||
memcpy(offset,sharedOffset,totalspace*sizeof(T));
|
||||
}
|
||||
public:
|
||||
offset = newOffset;
|
||||
// totalobj= numObjects;
|
||||
// totalspace= numSpace;
|
||||
}
|
||||
|
||||
G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace; //Per-thread available number of slots
|
||||
G4RUN_DLL G4ThreadLocalStatic T* offset; //Pointer to first instance of an array
|
||||
T* FreeWorkArea() // G4int* numObjects, G4int* numSpace)
|
||||
{
|
||||
// Detach this thread from this Location
|
||||
// The object which calls this method is responsible for it.
|
||||
//
|
||||
T* offsetRet = offset;
|
||||
|
||||
private:
|
||||
G4int totalobj; //Total number of instances from master thread
|
||||
G4int totalspace; // Available number of "slots"
|
||||
T* sharedOffset;
|
||||
G4Mutex mutex;
|
||||
offset = 0;
|
||||
|
||||
return offsetRet;
|
||||
}
|
||||
|
||||
void WorkerCopySubInstanceArray()
|
||||
// Invoked by each worker thread to copy all subinstances array from
|
||||
// the master thread
|
||||
{
|
||||
if(offset)
|
||||
return;
|
||||
// Since this is called by worker threds, totalspace is some valid number >
|
||||
// 0 Remember totalspace is the number of availabel slots from master. We
|
||||
// are sure that it has valid data
|
||||
G4AutoLock l(&mutex);
|
||||
offset = (T*) realloc(offset, totalspace * sizeof(T));
|
||||
if(offset == 0)
|
||||
{
|
||||
G4Exception("G4VUPLSplitter::WorkerCopySubInstanceArray()", "OutOfMemory",
|
||||
FatalException, "Cannot malloc space!");
|
||||
return;
|
||||
}
|
||||
// Now just copy from master thread (sharedOffset)
|
||||
memcpy(offset, sharedOffset, totalspace * sizeof(T));
|
||||
}
|
||||
|
||||
public:
|
||||
G4RUN_DLL G4ThreadLocalStatic G4int workertotalspace; // Per-thread available
|
||||
// number of slots
|
||||
G4RUN_DLL G4ThreadLocalStatic
|
||||
T* offset; // Pointer to first instance of an array
|
||||
|
||||
private:
|
||||
G4int totalobj; // Total number of instances from master thread
|
||||
G4int totalspace; // Available number of "slots"
|
||||
T* sharedOffset;
|
||||
G4Mutex mutex;
|
||||
};
|
||||
|
||||
template<typename T> G4ThreadLocal G4int G4VUPLSplitter<T>::workertotalspace=0;
|
||||
template<typename T> G4ThreadLocal T* G4VUPLSplitter<T>::offset=0;
|
||||
template <typename T>
|
||||
G4ThreadLocal G4int G4VUPLSplitter<T>::workertotalspace = 0;
|
||||
template <typename T>
|
||||
G4ThreadLocal T* G4VUPLSplitter<T>::offset = 0;
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user