Import Geant4 10.4.1 source tree

This commit is contained in:
Gabriele Cosmo
2018-03-02 09:34:30 +01:00
parent fc6af9e721
commit fe04dcb406
341 changed files with 18389 additions and 24522 deletions
+138 -104
View File
@@ -29,16 +29,16 @@
// GEANT 4 class header file
//
// Class Description:
//
// The classes contained in this header files are used by
// G4Cache to store a TLS instance of the cached object.
// G4Cache to store a TLS instance of the cached object.
// These classes should not be used by client code, but
// are used by one of the G4Cache classes.
//
// This class is container of the cached value
// G4Cache is a container of the cached value.
// Not memory efficient, but CPU efficient (constant time access)
// A different version with a map instead of a vector should be
// memory efficient
// and less CPU efficient (log-time access).
// memory efficient and less CPU efficient (log-time access).
// If really a lot of these objects are used
// we may want to consider the map version to save some memory
//
@@ -53,29 +53,35 @@
// objects can be stored in the cache and this limitation is removed
// but explicit handling of memory (new/delete) of cached object becomes
// client responsibility.
//
// History:
// 21 Oct 2013: A. Dotti - First implementation
//
// Todos: - Understand if map based class can be more efficent than
// vector one
// - Evaluate use of specialized allocator for TLS "new"
// ------------------------------------------------------------
#ifndef G4CacheDetails_hh
#define G4CacheDetails_hh
#include <vector>
#include "G4Threading.hh"
#include "globals.hh"
#ifdef g4cdebug
#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
#include <iostream>
#include <sstream>
using std::cout;
using std::endl;
#endif
// A TLS storage for a cache of type VALTYPE
template<class VALTYPE>
class G4CacheReference {
public:
//
template<class VALTYPE> class G4CacheReference
{
public:
inline void Initialize( unsigned int id );
// Initliaze TLS storage
@@ -85,47 +91,51 @@ public:
inline VALTYPE& GetCache(unsigned int id) const;
// Returns cached value for instance id
private:
private:
typedef std::vector<VALTYPE*> cache_container;
// Implementation detail: the cached object is stored as a
// pointer. See Note (1)
// pointer. Object is stored as a pointer to avoid too large
// std::vector in case of stored objects and allow use of
// specialized allocators
static G4ThreadLocal cache_container *cache;
};
// Note (1) : Object is stored as a pointer to avoid too large
// std::vector in case of stored objects and allow use
// of spcialized allocators
// Template specialization for pointers
// Note that objects are not owned by cache, for this
// version of the cache the explicit new/delete of the
// cached object
template<class VALTYPE>
class G4CacheReference<VALTYPE*> {
public:
// Note: Objects are not owned by cache, for this version of the cache
// the explicit new/delete of the cached object
//
template<class VALTYPE> class G4CacheReference<VALTYPE*>
{
public:
inline void Initialize( unsigned int id );
inline void Destroy( unsigned int id , G4bool last);
inline VALTYPE*& GetCache(unsigned int id) const;
private:
private:
typedef std::vector<VALTYPE*> cache_container;
static G4ThreadLocal cache_container *cache;
};
// Template specialization for probably the most used case: double
// Be more efficient avoiding unnecessary "new/delete"
template<>
class G4CacheReference<G4double> {
public:
//
template<> class G4CacheReference<G4double>
{
public:
inline void Initialize( unsigned int id );
inline void Destroy( unsigned int id , G4bool last);
inline G4double& GetCache(unsigned int id) const;
private:
private:
typedef std::vector<G4double> cache_container;
G4GLOB_DLL static G4ThreadLocal std::vector<G4double> *cache;
};
@@ -136,137 +146,161 @@ private:
//================================
//======= Implementation: G4CacheReference<V>
//===========================================
template<class V>
void G4CacheReference<V>::Initialize(unsigned int id)
void G4CacheReference<V>::Initialize( unsigned int id )
{
#ifdef g4cdebug
if ( cache == 0 )
cout<<"Generic template"<<endl;
if ( cache == 0 )
cout<<"Generic template"<<endl;
#endif
//Create cache container
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<V*>(0));
if ( (*cache)[id] == 0 ) (*cache)[id]=new V;
// Create cache container
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<V*>(0));
if ( (*cache)[id] == 0 )
(*cache)[id]=new V;
}
template<class V>
void G4CacheReference<V>::Destroy( unsigned int id, G4bool last )
{
if ( cache ) {
if ( cache )
{
#ifdef g4cdebug
cout<<"Destroying element"<<id<<" is last?"<<last<<endl;
cout<<"Destroying element"<<id<<" is last?"<<last<<endl;
#endif
if ( cache->size() < id ) {
G4ExceptionDescription msg;
msg<<"Internal fatal error. Invalid G4Cache size (requested id: "<<id<<" but cache has size: "<<cache->size();
msg<<" Possibly client created G4Cache object in a thread and tried to delete it from another thread!";
G4Exception("G4CacheReference<V>::Destroy","Cache001",FatalException,msg);
return;
}
if ( cache->size() > id && (*cache)[id] ) {
delete (*cache)[id];
(*cache)[id]=0;
}
if (last ) {
delete cache;
cache = 0;
}
if ( cache->size() < id )
{
G4ExceptionDescription msg;
msg << "Internal fatal error. Invalid G4Cache size (requested id: "
<< id << " but cache has size: "<<cache->size();
msg << " Possibly client created G4Cache object in a thread and"
<< " tried to delete it from another thread!";
G4Exception("G4CacheReference<V>::Destroy", "Cache001",
FatalException, msg);
return;
}
if ( cache->size() > id && (*cache)[id] )
{
delete (*cache)[id];
(*cache)[id]=0;
}
if (last)
{
delete cache;
cache = 0;
}
}
}
template<class V>
V& G4CacheReference<V>::GetCache(unsigned int id) const
V& G4CacheReference<V>::GetCache( unsigned int id ) const
{
return *(cache->operator[](id));
return *(cache->operator[](id));
}
template<class V>
G4ThreadLocal typename G4CacheReference<V>::cache_container * G4CacheReference<V>::cache = 0;
G4ThreadLocal typename
G4CacheReference<V>::cache_container * G4CacheReference<V>::cache = 0;
//======= Implementation: G4CacheReference<V*>
//============================================
template<class V>
void G4CacheReference<V*>::Initialize( unsigned int id )
{
#ifdef g4cdebug
if ( cache == 0 )
cout<<"Pointer template"<<endl;
if ( cache == 0 )
cout<<"Pointer template"<<endl;
#endif
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<V*>(0));
//No need to create pointee
//if ( (*cache)[id] == 0 ) (*cache)[id]=new VALTYPE;
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<V*>(0));
}
template<class V>
inline void G4CacheReference<V*>::Destroy( unsigned int id , G4bool last)
inline void G4CacheReference<V*>::Destroy( unsigned int id , G4bool last )
{
if ( cache ) {
if ( cache )
{
#ifdef g4cdebug
cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
cout << "Destroying element" << id << " is last?" << last
<< "-Pointer template specialization-" << endl;
#endif
if ( cache->size() < id ) {
G4ExceptionDescription msg;
msg<<"Internal fatal error. Invalid G4Cache size (requested id: "<<id<<" but cache has size: "<<cache->size();
msg<<" Possibly client created G4Cache object in a thread and tried to delete it from another thread!";
G4Exception("G4CacheReference<V*>::Destroy","Cache001",FatalException,msg);
return;
}
if ( cache->size() > id && (*cache)[id] ) {
//Ownership is for client
//delete (*cache)[id];
(*cache)[id]=0;
}
if (last ) {
delete cache;
cache = 0;
}
if ( cache->size() < id )
{
G4ExceptionDescription msg;
msg << "Internal fatal error. Invalid G4Cache size (requested id: "
<< id << " but cache has size: " << cache->size();
msg << " Possibly client created G4Cache object in a thread and"
<< " tried to delete it from another thread!";
G4Exception("G4CacheReference<V*>::Destroy", "Cache001",
FatalException, msg);
return;
}
if ( cache->size() > id && (*cache)[id] )
{
// Ownership is for client
// delete (*cache)[id];
(*cache)[id]=0;
}
if (last )
{
delete cache;
cache = 0;
}
}
}
template<class V>
V*& G4CacheReference<V*>::GetCache(unsigned int id) const
{
return (cache->operator[](id));
return (cache->operator[](id));
}
template<class V>
G4ThreadLocal typename G4CacheReference<V*>::cache_container * G4CacheReference<V*>::cache=0;
G4ThreadLocal typename
G4CacheReference<V*>::cache_container * G4CacheReference<V*>::cache = 0;
//======= Implementation: G4CacheReference<double>
//============================================
void G4CacheReference<G4double>::Initialize( unsigned int id )
{
#ifdef g4cdebug
cout<<"Specialized template for G4double"<<endl;
cout<<"Specialized template for G4double"<<endl;
#endif
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<G4double>(0));
if ( cache == 0 )
cache = new cache_container;
if ( cache->size() <= id )
cache->resize(id+1,static_cast<G4double>(0));
}
#ifdef g4cdebug
void G4CacheReference<G4double>::Destroy( unsigned int id , G4bool last) {
void G4CacheReference<G4double>::Destroy( unsigned int id , G4bool last)
#else
void G4CacheReference<G4double>::Destroy( unsigned int /*id*/ , G4bool last) {
void G4CacheReference<G4double>::Destroy( unsigned int /*id*/ , G4bool last)
#endif
if ( cache && last ) {
{
if ( cache && last )
{
#ifdef g4cdebug
cout<<"Destroying element"<<id<<" is last?"<<last<<"-Pointer template specialization-"<<endl;
cout << "Destroying element" << id << " is last?" << last
<< "-Pointer template specialization-" << endl;
#endif
delete cache;
cache = 0;
}
delete cache;
cache = 0;
}
}
G4double& G4CacheReference<G4double>::GetCache(unsigned int id) const {
return cache->operator[](id);
G4double& G4CacheReference<G4double>::GetCache(unsigned int id) const
{
return cache->operator[](id);
}
//G4ThreadLocal std::vector<G4double>* G4CacheReference<G4double>::cache = 0;
#endif