Import Geant4 10.7.0.beta source tree
This commit is contained in:
@@ -23,135 +23,132 @@
|
||||
// * acceptance of all terms of the Geant4 Software license. *
|
||||
// ********************************************************************
|
||||
//
|
||||
// G4TWorkspacePool
|
||||
//
|
||||
//
|
||||
// ------------------------------------------------------------
|
||||
// GEANT 4 class header file
|
||||
//
|
||||
// Class Description:
|
||||
// Class description:
|
||||
//
|
||||
// Create and hold a pointer to Workspace.
|
||||
// This class holds a thread-private static instance
|
||||
// of the template parameter workspace.
|
||||
// This class holds a thread-private static instance of the template
|
||||
// parameter workspace.
|
||||
//
|
||||
// The concrete implementation of workspace objects
|
||||
// are responsible for instantiating a singleton instance
|
||||
// of this pool.
|
||||
// The concrete implementation of workspace objects are responsible for
|
||||
// instantiating a singleton instance of this pool.
|
||||
//
|
||||
// Recycling of this pool can enable reuse among different
|
||||
// threads in task-based - or 'on-demand' - simulation.
|
||||
// Recycling of this pool can enable reuse among different threads in
|
||||
// task-based - or 'on-demand' - simulation.
|
||||
|
||||
// Authors: J.Apostolakis, A.Dotti - 24 October 2014
|
||||
// Revisions: G.Cosmo - 21 Obctober 2016, revised pool initialisation
|
||||
// ------------------------------------------------------------
|
||||
|
||||
#ifndef G4TWORKSPACEPOOL_HH
|
||||
#define G4TWORKSPACEPOOL_HH
|
||||
#define G4TWORKSPACEPOOL_HH 1
|
||||
|
||||
#include "tls.hh"
|
||||
#include "globals.hh"
|
||||
#include "tls.hh"
|
||||
|
||||
template<class T>
|
||||
template <class T>
|
||||
class G4TWorkspacePool
|
||||
{
|
||||
public:
|
||||
public:
|
||||
inline T* CreateWorkspace();
|
||||
// For use with simple MT mode - each thread gets a workspace
|
||||
// and uses it until end
|
||||
|
||||
inline T* CreateWorkspace();
|
||||
// For use with simple MT mode - each thread gets a workspace
|
||||
// and uses it until end
|
||||
|
||||
inline void CreateAndUseWorkspace();
|
||||
// Create it (as above) and use it
|
||||
|
||||
inline T* FindOrCreateWorkspace();
|
||||
// For use with 'dynamic' model of threading - workspaces can be recycled
|
||||
// Reuse an existing workspace - or create a new one if needed.
|
||||
// This will never fail, except if system is out of resources
|
||||
|
||||
inline T* GetWorkspace() { return fMyWorkspace; }
|
||||
// Give back the existing, active workspace for my thread / task
|
||||
|
||||
inline void Recycle( T * myWrkSpace );
|
||||
// Keep the unused Workspace - for recycling
|
||||
|
||||
inline void CleanUpAndDestroyAllWorkspaces();
|
||||
// To be called once at the end of the job
|
||||
inline void CreateAndUseWorkspace();
|
||||
// Create it (as above) and use it
|
||||
|
||||
public:
|
||||
inline T* FindOrCreateWorkspace();
|
||||
// For use with 'dynamic' model of threading - workspaces can be recycled
|
||||
// Reuse an existing workspace - or create a new one if needed.
|
||||
// This will never fail, except if system is out of resources
|
||||
|
||||
G4TWorkspacePool() {}
|
||||
~G4TWorkspacePool() {}
|
||||
|
||||
private:
|
||||
inline T* GetWorkspace() { return fMyWorkspace; }
|
||||
// Give back the existing, active workspace for my thread / task
|
||||
|
||||
static G4ThreadLocal T* fMyWorkspace;
|
||||
// The thread's workspace - if assigned
|
||||
inline void Recycle(T* myWrkSpace);
|
||||
// Keep the unused Workspace - for recycling
|
||||
|
||||
inline void CleanUpAndDestroyAllWorkspaces();
|
||||
// To be called once at the end of the job
|
||||
|
||||
G4TWorkspacePool() {}
|
||||
~G4TWorkspacePool() {}
|
||||
|
||||
private:
|
||||
static G4ThreadLocal T* fMyWorkspace;
|
||||
// The thread's workspace - if assigned
|
||||
};
|
||||
|
||||
template<typename T> G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace=0;
|
||||
template <typename T>
|
||||
G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace = nullptr;
|
||||
|
||||
template<class T>
|
||||
// -----------------------------
|
||||
// Inline methods implementation
|
||||
// -----------------------------
|
||||
|
||||
template <class T>
|
||||
T* G4TWorkspacePool<T>::CreateWorkspace()
|
||||
{
|
||||
T* wrk = 0;
|
||||
if ( !fMyWorkspace )
|
||||
T* wrk = nullptr;
|
||||
if(fMyWorkspace == nullptr)
|
||||
{
|
||||
wrk = new T;
|
||||
if(wrk == nullptr)
|
||||
{
|
||||
wrk = new T;
|
||||
if ( !wrk )
|
||||
{
|
||||
G4Exception("G4TWorspacePool<someType>::CreateWorkspace",
|
||||
"MemoryError", FatalException,
|
||||
"Failed to create workspace.");
|
||||
}
|
||||
else
|
||||
{
|
||||
fMyWorkspace = wrk;
|
||||
}
|
||||
G4Exception("G4TWorspacePool<someType>::CreateWorkspace()", "MemoryError",
|
||||
FatalException, "Failed to create workspace.");
|
||||
}
|
||||
else
|
||||
{
|
||||
G4Exception("ParticlesWorspacePool::CreateWorkspace",
|
||||
"InvalidCondition", FatalException,
|
||||
"Cannot create workspace twice for the same thread.");
|
||||
wrk = fMyWorkspace;
|
||||
fMyWorkspace = wrk;
|
||||
}
|
||||
return wrk;
|
||||
}
|
||||
else
|
||||
{
|
||||
G4Exception("ParticlesWorspacePool::CreateWorkspace()", "InvalidCondition",
|
||||
FatalException,
|
||||
"Cannot create workspace twice for the same thread.");
|
||||
wrk = fMyWorkspace;
|
||||
}
|
||||
return wrk;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template <class T>
|
||||
void G4TWorkspacePool<T>::CreateAndUseWorkspace()
|
||||
{
|
||||
(this->CreateWorkspace())->UseWorkspace();
|
||||
(this->CreateWorkspace())->UseWorkspace();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template <class T>
|
||||
T* G4TWorkspacePool<T>::FindOrCreateWorkspace()
|
||||
{
|
||||
T* wrk= fMyWorkspace;
|
||||
if( !wrk )
|
||||
{
|
||||
wrk= this->CreateWorkspace();
|
||||
}
|
||||
wrk->UseWorkspace();
|
||||
|
||||
fMyWorkspace= wrk; // assign it for use by this thread.
|
||||
return wrk;
|
||||
T* wrk = fMyWorkspace;
|
||||
if(wrk == nullptr)
|
||||
{
|
||||
wrk = this->CreateWorkspace();
|
||||
}
|
||||
wrk->UseWorkspace();
|
||||
|
||||
fMyWorkspace = wrk; // assign it for use by this thread.
|
||||
return wrk;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void G4TWorkspacePool<T>::Recycle( T * myWrkSpace )
|
||||
template <class T>
|
||||
void G4TWorkspacePool<T>::Recycle(T* myWrkSpace)
|
||||
{
|
||||
myWrkSpace->ReleaseWorkspace();
|
||||
delete myWrkSpace;
|
||||
myWrkSpace->ReleaseWorkspace();
|
||||
delete myWrkSpace;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template <class T>
|
||||
void G4TWorkspacePool<T>::CleanUpAndDestroyAllWorkspaces()
|
||||
{
|
||||
if (fMyWorkspace)
|
||||
{
|
||||
fMyWorkspace->DestroyWorkspace();
|
||||
delete fMyWorkspace;
|
||||
fMyWorkspace=0;
|
||||
}
|
||||
if(fMyWorkspace != nullptr)
|
||||
{
|
||||
fMyWorkspace->DestroyWorkspace();
|
||||
delete fMyWorkspace;
|
||||
fMyWorkspace = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // G4TWORKSPACEPOOL_HH
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user