Import Geant4 11.3.0.beta source tree

This commit is contained in:
Gabriele Cosmo
2024-06-28 13:08:51 +02:00
parent f7b23877ed
commit e58e650b32
5232 changed files with 239416 additions and 244360 deletions
@@ -21,10 +21,6 @@
errors in thread-local EnergyDeposit are present, they can be viewed
in "mfd_diff.out" at the end of the simulation
This example also provides a demonstration of the timemory (a performance
instrumentation toolkit) package provided in Geant4 -- for documentation of timemory
see https://github.com/NERSC/timemory and https://timemory.readthedocs.io.
\section ThreadsafeScorers_s1 ATOMICS and the ATOMIC SCORERS
atomics can ONLY handle plain-old data (POD) types, e.g. int, double, etc.
@@ -186,17 +182,5 @@
% ./ts_scorers run.mac
% ./ts_scorers run.mac > run.out
\section ThreadsafeScorers_s8 TIMEMORY USAGE
This example demonstrates profiling analysis with timemory
(https://github.com/NERSC/timemory).
- Compile Geant4 with timemory (-DGEANT4_USE_TIMEMORY=ON)
- timemory provide timing within the Geant4 source code
and within the example (TSRun::RecordEvent)
- Analysis is echoed to stdout and generates several output
files in a folder based on the name of the executable. In
general that folder will be "timemory-{name of executable}-output"
*/
@@ -6,16 +6,6 @@ project(ThreadsafeScorers C CXX)
find_package(Geant4 REQUIRED ui_all vis_all)
include(${Geant4_USE_FILE})
option(USE_TIMEMORY "Enable timemory if not built at Geant4 level" OFF)
set(timemory_COMPONENTS "headers;caliper;papi;gotcha;gperftools-cpu;vector"
CACHE STRING "timemory components")
set(timemory_FIND_COMPONENTS_INTERFACE ${PROJECT_NAME}-timemory)
if(USE_TIMEMORY AND NOT TARGET geant4-timemory)
find_package(timemory 3.0 REQUIRED COMPONENTS ${timemory_COMPONENTS})
set(timemory_LIBRARIES ${PROJECT_NAME}-timemory)
endif()
include_directories(${PROJECT_SOURCE_DIR}/include
${Geant4_INCLUDE_DIR})
@@ -29,7 +19,7 @@ add_executable(${name} ${name}.cc ${headers}
${sources}
${macros})
target_link_libraries(${name} ${Geant4_LIBRARIES} ${timemory_LIBRARIES})
target_link_libraries(${name} ${Geant4_LIBRARIES})
# For IDEs - specifically Xcode
source_group("macros" FILES ${macros})
@@ -4,6 +4,8 @@ See `CONTRIBUTING.rst` for details of **required** info/format for each entry,
which **must** added in reverse chronological order (newest at the top). It must **not**
be used as a substitute for writing good git commit messages!
## 2024-05-03 Ben Morgan (ThreadsafeScorers-V11-02-00)
- Remove use of no longer supported TiMemory
## 2022-11-09 I. Hrivnacova (ThreadsafeScorers-V11-00-01)
- Fixed Doxygen documentation
@@ -23,10 +23,7 @@
sum of these scorers that were updated via mutex locking. If round-off
errors in thread-local EnergyDeposit are present, they can be viewed
in "mfd_diff.out" at the end of the simulation
This example also provides a demonstration of the timemory (a performance
instrumentation toolkit) package provided in Geant4 -- for documentation of timemory
see https://github.com/NERSC/timemory and https://timemory.readthedocs.io.
1- ATOMICS and the ATOMIC SCORERS
atomics can ONLY handle plain-old data (POD) types, e.g. int, double, etc.
@@ -174,15 +171,3 @@
(without visualization)
% ./ts_scorers run.mac
% ./ts_scorers run.mac > run.out
8- TIMEMORY USAGE
This example demonstrates profiling analysis with timemory
(https://github.com/NERSC/timemory).
- Compile Geant4 with timemory (-DGEANT4_USE_TIMEMORY=ON)
- timemory provide timing within the Geant4 source code
and within the example (TSRun::RecordEvent)
- Analysis is echoed to stdout and generates several output
files in a folder based on the name of the executable. In
general that folder will be "timemory-{name of executable}-output"
@@ -45,12 +45,12 @@
#ifndef G4TAtomicHitsCollection_h
#define G4TAtomicHitsCollection_h 1
#include "G4VHitsCollection.hh"
#include "G4Allocator.hh"
#include "globals.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
#include "G4Threading.hh"
#include "G4VHitsCollection.hh"
#include "G4atomic.hh"
#include "globals.hh"
#include <deque>
#include <type_traits>
@@ -77,114 +77,110 @@
void* theCollection;
};*/
template <class T>
template<class T>
class G4TAtomicHitsCollection : public G4VHitsCollection
{
protected:
static_assert(std::is_fundamental<T>::value,
"G4TAtomicHitsCollection must use fundamental type");
protected:
static_assert(std::is_fundamental<T>::value,
"G4TAtomicHitsCollection must use fundamental type");
public:
typedef T base_type;
typedef G4atomic<T> value_type;
typedef typename std::deque<value_type*> container_type;
public:
typedef T base_type;
typedef G4atomic<T> value_type;
typedef typename std::deque<value_type*> container_type;
public:
G4TAtomicHitsCollection();
public:
G4TAtomicHitsCollection();
public:
// with description
G4TAtomicHitsCollection(G4String detName, G4String colNam);
public:
// with description
G4TAtomicHitsCollection(G4String detName, G4String colNam);
// constructor.
public:
virtual ~G4TAtomicHitsCollection();
G4bool operator==(const G4TAtomicHitsCollection<T>& right) const;
// constructor.
public:
virtual ~G4TAtomicHitsCollection();
G4bool operator==(const G4TAtomicHitsCollection<T>& right) const;
// inline void *operator new(size_t);
// inline void operator delete(void* anHC);
// inline void *operator new(size_t);
// inline void operator delete(void* anHC);
public: // with description
virtual void DrawAllHits();
virtual void PrintAllHits();
// These two methods invokes Draw() and Print() methods of all of
// hit objects stored in this collection, respectively.
public: // with description
virtual void DrawAllHits();
virtual void PrintAllHits();
// These two methods invokes Draw() and Print() methods of all of
// hit objects stored in this collection, respectively.
public: // with description
inline value_type* operator[](size_t i) const { return (*theCollection)[i]; }
// Returns a pointer to a concrete hit object.
inline container_type* GetVector() const { return theCollection; }
// Returns a collection vector.
inline G4int insert(T* aHit)
{
G4AutoLock l(&fMutex);
theCollection->push_back(aHit);
return theCollection->size();
}
// Insert a hit object. Total number of hit objects stored in this
// collection is returned.
inline G4int entries() const
{
G4AutoLock l(&fMutex);
return theCollection->size();
}
// Returns the number of hit objects stored in this collection
public: // with description
inline value_type* operator[](size_t i) const { return (*theCollection)[i]; }
// Returns a pointer to a concrete hit object.
inline container_type* GetVector() const { return theCollection; }
// Returns a collection vector.
inline G4int insert(T* aHit)
{
G4AutoLock l(&fMutex);
theCollection->push_back(aHit);
return theCollection->size();
}
// Insert a hit object. Total number of hit objects stored in this
// collection is returned.
inline G4int entries() const
{
G4AutoLock l(&fMutex);
return theCollection->size();
}
// Returns the number of hit objects stored in this collection
public:
virtual G4VHit* GetHit(size_t i) const { return (*theCollection)[i]; }
virtual size_t GetSize() const
{
G4AutoLock l(&fMutex);
return theCollection->size();
}
public:
virtual G4VHit* GetHit(size_t i) const { return (*theCollection)[i]; }
virtual size_t GetSize() const
{
G4AutoLock l(&fMutex);
return theCollection->size();
}
protected:
container_type* theCollection;
G4Mutex fMutex;
protected:
container_type* theCollection;
G4Mutex fMutex;
};
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
G4TAtomicHitsCollection<T>::G4TAtomicHitsCollection()
: theCollection(new container_type)
template<class T>
G4TAtomicHitsCollection<T>::G4TAtomicHitsCollection() : theCollection(new container_type)
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
G4TAtomicHitsCollection<T>::G4TAtomicHitsCollection(G4String detName,
G4String colNam)
: G4VHitsCollection(detName, colNam)
, theCollection(new container_type)
template<class T>
G4TAtomicHitsCollection<T>::G4TAtomicHitsCollection(G4String detName, G4String colNam)
: G4VHitsCollection(detName, colNam), theCollection(new container_type)
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
template<class T>
G4TAtomicHitsCollection<T>::~G4TAtomicHitsCollection()
{
for(size_t i = 0; i < theCollection->size(); i++)
delete(*theCollection)[i];
for (size_t i = 0; i < theCollection->size(); i++)
delete (*theCollection)[i];
theCollection->clear();
delete theCollection;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
G4bool G4TAtomicHitsCollection<T>::operator==(
const G4TAtomicHitsCollection<T>& right) const
template<class T>
G4bool G4TAtomicHitsCollection<T>::operator==(const G4TAtomicHitsCollection<T>& right) const
{
return (collectionName == right.collectionName);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
template<class T>
void G4TAtomicHitsCollection<T>::DrawAllHits()
{
G4AutoLock l(&fMutex);
for(size_t i = 0; i < theCollection->size(); i++)
for (size_t i = 0; i < theCollection->size(); i++)
(*theCollection)[i]->Draw();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <class T>
template<class T>
void G4TAtomicHitsCollection<T>::PrintAllHits()
{
G4AutoLock l(&fMutex);
for(size_t i = 0; i < theCollection->size(); i++)
for (size_t i = 0; i < theCollection->size(); i++)
(*theCollection)[i]->Print();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -45,12 +45,12 @@
#ifndef G4TAtomicHitsMap_h
#define G4TAtomicHitsMap_h 1
#include "G4AutoLock.hh"
#include "G4THitsCollection.hh"
#include "G4THitsMap.hh"
#include "globals.hh"
#include "G4atomic.hh"
#include "G4Threading.hh"
#include "G4AutoLock.hh"
#include "G4atomic.hh"
#include "globals.hh"
#include <map>
#include <type_traits>
@@ -65,141 +65,132 @@
// cannot be instansiated with a template class. Thus G4HitsMap
// class MUST NOT be directly used by the user.
template <typename T>
template<typename T>
class G4TAtomicHitsMap : public G4VHitsCollection
{
protected:
static_assert(std::is_fundamental<T>::value,
"G4TAtomicHitsMap must use fundamental type");
protected:
static_assert(std::is_fundamental<T>::value, "G4TAtomicHitsMap must use fundamental type");
public:
typedef G4atomic<T> value_type;
typedef value_type* mapped_type;
typedef typename std::map<G4int, mapped_type> container_type;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;
public:
typedef G4atomic<T> value_type;
typedef value_type* mapped_type;
typedef typename std::map<G4int, mapped_type> container_type;
typedef typename container_type::iterator iterator;
typedef typename container_type::const_iterator const_iterator;
public:
G4TAtomicHitsMap();
public:
G4TAtomicHitsMap();
public: // with description
G4TAtomicHitsMap(G4String detName, G4String colNam);
// constructor.
public: // with description
G4TAtomicHitsMap(G4String detName, G4String colNam);
// constructor.
public:
virtual ~G4TAtomicHitsMap();
G4bool operator==(const G4TAtomicHitsMap<T>& right) const;
G4TAtomicHitsMap<T>& operator+=(const G4TAtomicHitsMap<T>& right) const;
G4TAtomicHitsMap<T>& operator+=(const G4THitsMap<T>& right) const;
public:
virtual ~G4TAtomicHitsMap();
G4bool operator==(const G4TAtomicHitsMap<T>& right) const;
G4TAtomicHitsMap<T>& operator+=(const G4TAtomicHitsMap<T>& right) const;
G4TAtomicHitsMap<T>& operator+=(const G4THitsMap<T>& right) const;
public: // with description
virtual void DrawAllHits();
virtual void PrintAllHits();
// These two methods invokes Draw() and Print() methods of all of
// hit objects stored in this map, respectively.
public: // with description
virtual void DrawAllHits();
virtual void PrintAllHits();
// These two methods invokes Draw() and Print() methods of all of
// hit objects stored in this map, respectively.
public: // with description
inline value_type* operator[](G4int key) const;
public: // with description
inline value_type* operator[](G4int key) const;
// Returns a pointer to a concrete hit object.
inline container_type* GetMap() const { return theCollection; }
// Returns a collection map.
inline G4int add(const G4int& key, value_type*& aHit) const;
inline G4int add(const G4int& key, T& aHit) const;
// Insert a hit object. Total number of hit objects stored in this
// map is returned.
inline G4int set(const G4int& key, value_type*& aHit) const;
inline G4int set(const G4int& key, T& aHit) const;
// Overwrite a hit object. Total number of hit objects stored in this
// map is returned.
inline G4int entries() const { return theCollection->size(); }
// Returns the number of hit objects stored in this map
inline void clear();
// Returns a pointer to a concrete hit object.
inline container_type* GetMap() const { return theCollection; }
// Returns a collection map.
inline G4int add(const G4int& key, value_type*& aHit) const;
inline G4int add(const G4int& key, T& aHit) const;
// Insert a hit object. Total number of hit objects stored in this
// map is returned.
inline G4int set(const G4int& key, value_type*& aHit) const;
inline G4int set(const G4int& key, T& aHit) const;
// Overwrite a hit object. Total number of hit objects stored in this
// map is returned.
inline G4int entries() const { return theCollection->size(); }
// Returns the number of hit objects stored in this map
inline void clear();
public:
virtual G4VHit* GetHit(size_t) const { return 0; }
virtual size_t GetSize() const { return theCollection->size(); }
public:
virtual G4VHit* GetHit(size_t) const { return 0; }
virtual size_t GetSize() const { return theCollection->size(); }
virtual size_t size() const { return theCollection->size(); }
virtual size_t size() const { return theCollection->size(); }
public:
iterator begin() { return theCollection->begin(); }
iterator end() { return theCollection->end(); }
public:
iterator begin() { return theCollection->begin(); }
iterator end() { return theCollection->end(); }
const_iterator begin() const { return theCollection->begin(); }
const_iterator end() const { return theCollection->end(); }
const_iterator begin() const { return theCollection->begin(); }
const_iterator end() const { return theCollection->end(); }
const_iterator cbegin() const { return theCollection->cbegin(); }
const_iterator cend() const { return theCollection->cend(); }
const_iterator cbegin() const { return theCollection->cbegin(); }
const_iterator cend() const { return theCollection->cend(); }
iterator find(G4int p) { return theCollection->find(p); }
const_iterator find(G4int p) const { return theCollection->find(p); }
iterator find(G4int p) { return theCollection->find(p); }
const_iterator find(G4int p) const { return theCollection->find(p); }
private:
container_type* theCollection;
mutable G4Mutex fMutex;
private:
container_type* theCollection;
mutable G4Mutex fMutex;
};
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
G4TAtomicHitsMap<T>::G4TAtomicHitsMap()
: theCollection(new container_type)
template<typename T>
G4TAtomicHitsMap<T>::G4TAtomicHitsMap() : theCollection(new container_type)
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
G4TAtomicHitsMap<T>::G4TAtomicHitsMap(G4String detName, G4String colNam)
: G4VHitsCollection(detName, colNam)
, theCollection(new container_type)
: G4VHitsCollection(detName, colNam), theCollection(new container_type)
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
G4TAtomicHitsMap<T>::~G4TAtomicHitsMap()
{
for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
for (auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
delete itr->second;
delete theCollection;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
G4bool G4TAtomicHitsMap<T>::operator==(const G4TAtomicHitsMap<T>& right) const
{
return (collectionName == right.collectionName);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(
const G4TAtomicHitsMap<T>& rhs) const
template<typename T>
G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(const G4TAtomicHitsMap<T>& rhs) const
{
for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
for (auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
add(itr->first, *(itr->second));
return (G4TAtomicHitsMap<T>&) (*this);
return (G4TAtomicHitsMap<T>&)(*this);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(
const G4THitsMap<T>& rhs) const
template<typename T>
G4TAtomicHitsMap<T>& G4TAtomicHitsMap<T>::operator+=(const G4THitsMap<T>& rhs) const
{
for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
for (auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
add(itr->first, *(itr->second));
return (G4TAtomicHitsMap<T>&) (*this);
return (G4TAtomicHitsMap<T>&)(*this);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
inline
typename G4TAtomicHitsMap<T>::value_type* G4TAtomicHitsMap<T>::operator[](
G4int key) const
template<typename T>
inline typename G4TAtomicHitsMap<T>::value_type* G4TAtomicHitsMap<T>::operator[](G4int key) const
{
if(theCollection->find(key) != theCollection->end())
if (theCollection->find(key) != theCollection->end())
return theCollection->find(key)->second;
else
{
else {
G4AutoLock l(&fMutex);
if(theCollection->find(key) == theCollection->end())
{
value_type* ptr = new value_type;
if (theCollection->find(key) == theCollection->end()) {
value_type* ptr = new value_type;
(*theCollection)[key] = ptr;
return ptr;
}
@@ -208,13 +199,12 @@ inline
}
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
{
if(theCollection->find(key) != theCollection->end())
if (theCollection->find(key) != theCollection->end())
*(*theCollection)[key] += *aHit;
else
{
else {
G4AutoLock l(&fMutex);
(*theCollection)[key] = aHit;
}
@@ -222,15 +212,14 @@ inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
return theCollection->size();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
{
if(theCollection->find(key) != theCollection->end())
if (theCollection->find(key) != theCollection->end())
*(*theCollection)[key] += aHit;
else
{
else {
value_type* hit = new value_type;
*hit = aHit;
*hit = aHit;
G4AutoLock l(&fMutex);
(*theCollection)[key] = hit;
}
@@ -238,49 +227,47 @@ inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
return theCollection->size();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, value_type*& aHit) const
{
if(theCollection->find(key) != theCollection->end())
delete(*theCollection)[key]->second;
if (theCollection->find(key) != theCollection->end()) delete (*theCollection)[key]->second;
(*theCollection)[key] = aHit;
G4AutoLock l(&fMutex);
return theCollection->size();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, T& aHit) const
{
if(theCollection->find(key) != theCollection->end())
if (theCollection->find(key) != theCollection->end())
*(*theCollection)[key] = aHit;
else
{
value_type* hit = new value_type;
*hit = aHit;
else {
value_type* hit = new value_type;
*hit = aHit;
(*theCollection)[key] = hit;
}
G4AutoLock l(&fMutex);
return theCollection->size();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
void G4TAtomicHitsMap<T>::DrawAllHits()
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
void G4TAtomicHitsMap<T>::PrintAllHits()
{
G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- "
<< entries() << " entries" << G4endl;
G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- " << entries()
<< " entries" << G4endl;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename T>
template<typename T>
void G4TAtomicHitsMap<T>::clear()
{
G4AutoLock l(&fMutex);
for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
for (auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
delete itr->second;
theCollection->clear();
@@ -75,293 +75,276 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
template <typename _Tp>
template<typename _Tp>
class G4atomic
{
public:
typedef typename std::atomic<_Tp> base_type;
typedef _Tp value_type;
public:
typedef typename std::atomic<_Tp> base_type;
typedef _Tp value_type;
private:
using mem_ord = std::memory_order;
private:
using mem_ord = std::memory_order;
public:
// constructors
explicit G4atomic(mem_ord mo = std::memory_order_acq_rel)
: fMemOrder(mo)
{
atomics::set(&fvalue, value_type());
}
public:
// constructors
explicit G4atomic(mem_ord mo = std::memory_order_acq_rel) : fMemOrder(mo)
{
atomics::set(&fvalue, value_type());
}
explicit G4atomic(const value_type& _init,
mem_ord mo = std::memory_order_acq_rel)
: fMemOrder(mo)
{
atomics::set(&fvalue, _init);
}
explicit G4atomic(const value_type& _init, mem_ord mo = std::memory_order_acq_rel)
: fMemOrder(mo)
{
atomics::set(&fvalue, _init);
}
// copy-constructor from pure C++11 atomic
explicit G4atomic(const base_type& rhs,
mem_ord mo = std::memory_order_acq_rel)
: fMemOrder(mo)
{
atomics::set(&fvalue, rhs);
}
// copy-constructor from pure C++11 atomic
explicit G4atomic(const base_type& rhs, mem_ord mo = std::memory_order_acq_rel) : fMemOrder(mo)
{
atomics::set(&fvalue, rhs);
}
// copy-constructor
explicit G4atomic(const G4atomic& rhs)
: fMemOrder(rhs.fMemOrder)
{
atomics::set(&fvalue, rhs.base());
}
// copy-constructor
explicit G4atomic(const G4atomic& rhs) : fMemOrder(rhs.fMemOrder)
{
atomics::set(&fvalue, rhs.base());
}
// assignment operators
G4atomic& operator=(const G4atomic& rhs)
{
if(this != &rhs)
atomics::set(&fvalue, rhs.fvalue);
return *this;
}
// assignment operators
G4atomic& operator=(const G4atomic& rhs)
{
if (this != &rhs) atomics::set(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator=(const value_type& rhs)
{
atomics::set(&fvalue, rhs);
return *this;
}
G4atomic& operator=(const value_type& rhs)
{
atomics::set(&fvalue, rhs);
return *this;
}
G4atomic& operator=(const base_type& rhs)
{
atomics::set(&fvalue, rhs);
return *this;
}
G4atomic& operator=(const base_type& rhs)
{
atomics::set(&fvalue, rhs);
return *this;
}
// destructor
~G4atomic() { fvalue.~base_type(); }
// destructor
~G4atomic() { fvalue.~base_type(); }
// base version
base_type& base() { return fvalue; }
const base_type& base() const { return fvalue; }
base_type& base() volatile { return fvalue; }
const base_type& base() const volatile { return fvalue; }
// base version
base_type& base() { return fvalue; }
const base_type& base() const { return fvalue; }
base_type& base() volatile { return fvalue; }
const base_type& base() const volatile { return fvalue; }
// check if atomic is lock-free
bool is_lock_free() const { return fvalue.is_lock_free(); }
bool is_lock_free() const volatile { return fvalue.is_lock_free(); }
// check if atomic is lock-free
bool is_lock_free() const { return fvalue.is_lock_free(); }
bool is_lock_free() const volatile { return fvalue.is_lock_free(); }
// store functions
void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst)
{
atomics::set(fvalue, _desired, mo);
}
void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst) volatile
{
atomics::set(fvalue, _desired, mo);
}
// store functions
void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst)
{
atomics::set(fvalue, _desired, mo);
}
void store(_Tp _desired, mem_ord mo = std::memory_order_seq_cst) volatile
{
atomics::set(fvalue, _desired, mo);
}
// load functions
_Tp load(mem_ord mo = std::memory_order_seq_cst) const
{
return atomics::get(fvalue, mo);
}
_Tp load(mem_ord mo = std::memory_order_seq_cst) const volatile
{
return atomics::get(fvalue, mo);
}
// load functions
_Tp load(mem_ord mo = std::memory_order_seq_cst) const { return atomics::get(fvalue, mo); }
_Tp load(mem_ord mo = std::memory_order_seq_cst) const volatile
{
return atomics::get(fvalue, mo);
}
// implicit conversion functions
operator _Tp() const { return this->load(); }
operator _Tp() const volatile { return this->load(); }
// implicit conversion functions
operator _Tp() const { return this->load(); }
operator _Tp() const volatile { return this->load(); }
operator base_type&() const { return fvalue; }
operator base_type&() const { return fvalue; }
// compare-and-swap functions
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _success,
mem_ord _failure)
{
return fvalue.compare_exchange_weak(_expected, _desired, _success,
_failure);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _success,
mem_ord _failure) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _success,
_failure);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _order)
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired,
mem_ord _order) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _success,
mem_ord _failure)
{
return fvalue.compare_exchange_weak(_expected, _desired, _success,
_failure);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _success,
// compare-and-swap functions
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _success, mem_ord _failure)
{
return fvalue.compare_exchange_weak(_expected, _desired, _success, _failure);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _success,
mem_ord _failure) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _success,
_failure);
}
{
return fvalue.compare_exchange_weak(_expected, _desired, _success, _failure);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _order)
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired,
mem_ord _order) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _order)
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_weak(_Tp& _expected, _Tp _desired, mem_ord _order) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
// value_type operators
G4atomic& operator+=(const value_type& rhs)
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const value_type& rhs)
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const value_type& rhs)
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const value_type& rhs)
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _success, mem_ord _failure)
{
return fvalue.compare_exchange_weak(_expected, _desired, _success, _failure);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _success,
mem_ord _failure) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _success, _failure);
}
// atomic operators
G4atomic& operator+=(const G4atomic& rhs)
{
atomics::increment(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator-=(const G4atomic& rhs)
{
atomics::decrement(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator*=(const G4atomic& rhs)
{
atomics::multiply(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator/=(const G4atomic& rhs)
{
atomics::divide(&fvalue, rhs.fvalue);
return *this;
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _order)
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
bool compare_exchange_strong(_Tp& _expected, _Tp _desired, mem_ord _order) volatile
{
return fvalue.compare_exchange_weak(_expected, _desired, _order);
}
G4atomic& operator+=(const G4atomic& rhs) volatile
{
atomics::increment(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator-=(const G4atomic& rhs) volatile
{
atomics::decrement(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator*=(const G4atomic& rhs) volatile
{
atomics::multiply(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator/=(const G4atomic& rhs) volatile
{
atomics::divide(&fvalue, rhs.fvalue);
return *this;
}
// value_type operators
G4atomic& operator+=(const value_type& rhs)
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const value_type& rhs)
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const value_type& rhs)
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const value_type& rhs)
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
// STL atomic operators
G4atomic& operator+=(const std::atomic<_Tp>& rhs)
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const std::atomic<_Tp>& rhs)
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const std::atomic<_Tp>& rhs)
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const std::atomic<_Tp>& rhs)
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
// atomic operators
G4atomic& operator+=(const G4atomic& rhs)
{
atomics::increment(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator-=(const G4atomic& rhs)
{
atomics::decrement(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator*=(const G4atomic& rhs)
{
atomics::multiply(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator/=(const G4atomic& rhs)
{
atomics::divide(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator+=(const std::atomic<_Tp>& rhs) volatile
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const std::atomic<_Tp>& rhs) volatile
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const std::atomic<_Tp>& rhs) volatile
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const std::atomic<_Tp>& rhs) volatile
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator+=(const G4atomic& rhs) volatile
{
atomics::increment(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator-=(const G4atomic& rhs) volatile
{
atomics::decrement(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator*=(const G4atomic& rhs) volatile
{
atomics::multiply(&fvalue, rhs.fvalue);
return *this;
}
G4atomic& operator/=(const G4atomic& rhs) volatile
{
atomics::divide(&fvalue, rhs.fvalue);
return *this;
}
// increment operators
value_type operator++()
{
value_type _tmp = ++fvalue;
return _tmp;
}
value_type operator++(int)
{
value_type _tmp = fvalue++;
return _tmp;
}
// STL atomic operators
G4atomic& operator+=(const std::atomic<_Tp>& rhs)
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const std::atomic<_Tp>& rhs)
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const std::atomic<_Tp>& rhs)
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const std::atomic<_Tp>& rhs)
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
value_type operator--()
{
value_type _tmp = --fvalue;
return _tmp;
}
value_type operator--(int)
{
value_type _tmp = fvalue--;
return _tmp;
}
G4atomic& operator+=(const std::atomic<_Tp>& rhs) volatile
{
atomics::increment(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator-=(const std::atomic<_Tp>& rhs) volatile
{
atomics::decrement(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator*=(const std::atomic<_Tp>& rhs) volatile
{
atomics::multiply(&fvalue, rhs, fMemOrder);
return *this;
}
G4atomic& operator/=(const std::atomic<_Tp>& rhs) volatile
{
atomics::divide(&fvalue, rhs, fMemOrder);
return *this;
}
protected:
base_type fvalue;
mem_ord fMemOrder;
// increment operators
value_type operator++()
{
value_type _tmp = ++fvalue;
return _tmp;
}
value_type operator++(int)
{
value_type _tmp = fvalue++;
return _tmp;
}
value_type operator--()
{
value_type _tmp = --fvalue;
return _tmp;
}
value_type operator--(int)
{
value_type _tmp = fvalue--;
return _tmp;
}
protected:
base_type fvalue;
mem_ord fMemOrder;
};
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#else // ! G4MULTITHREADED
template <typename _Tp>
template<typename _Tp>
using G4atomic = _Tp;
#endif // G4MULTITHREADED
@@ -46,358 +46,324 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
# include <functional>
# include <atomic>
# include <functional>
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
namespace atomics
{
//------------------------------------------------------------------------//
namespace details
{
//--------------------------------------------------------------------//
template <typename _Tp>
using OpFunction = std::function<_Tp(const _Tp&, const _Tp&)>;
//--------------------------------------------------------------------//
template <typename _Tp>
inline void do_fetch_and_store(std::atomic<_Tp>* _atomic, const _Tp& _value,
std::memory_order mem_odr)
{
_atomic->store(_value, mem_odr);
}
//--------------------------------------------------------------------//
template <typename _Tp>
inline void do_fetch_and_store(std::atomic<_Tp>* _atomic,
const std::atomic<_Tp>& _value,
std::memory_order mem_odr)
{
_atomic->store(_value.load(), mem_odr);
}
//--------------------------------------------------------------------//
template <typename _Tp>
inline void do_compare_and_swap(std::atomic<_Tp>* _atomic,
const _Tp& _value,
const OpFunction<_Tp>& _operator,
std::memory_order mem_odr)
{
_Tp _expected = _Tp();
do
{
_expected = _atomic->load();
} while(!(_atomic->compare_exchange_weak(
_expected, _operator(_expected, _value), mem_odr)));
}
//--------------------------------------------------------------------//
template <typename _Tp>
inline void do_compare_and_swap(std::atomic<_Tp>* _atomic,
const std::atomic<_Tp>& _atomic_value,
const OpFunction<_Tp>& _operator,
std::memory_order mem_odr)
{
_Tp _expected = _Tp();
do
{
_expected = _atomic->load();
} while(!(_atomic->compare_exchange_weak(
_expected, _operator(_expected, _atomic_value.load()), mem_odr)));
}
//--------------------------------------------------------------------//
} // namespace details
//------------------------------------------------------------------------//
// WITH ATOMIC TEMPLATE BASE TYPE AS SECOND PARAMETER
//------------------------------------------------------------------------//
template <typename T>
inline void set(std::atomic<T>* _atomic, const T& _desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
details::do_compare_and_swap(
_atomic, _desired,
details::OpFunction<T>([](const T&, const T& y) { return y; }), mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void set(std::atomic<T>& _atomic, const T& _desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
set(&_atomic, _desired, mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void increment(std::atomic<T>* _atomic, const T& _increment,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _increment,
details::OpFunction<T>([](const T& x, const T& y) { return x + y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void decrement(std::atomic<T>* _atomic, const T& _decrement,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _decrement,
details::OpFunction<T>([](const T& x, const T& y) { return x - y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void multiply(std::atomic<T>* _atomic, const T& _factor,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _factor,
details::OpFunction<T>([](const T& x, const T& y) { return x * y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void divide(std::atomic<T>* _atomic, const T& _factor,
//------------------------------------------------------------------------//
namespace details
{
//--------------------------------------------------------------------//
template<typename _Tp>
using OpFunction = std::function<_Tp(const _Tp&, const _Tp&)>;
//--------------------------------------------------------------------//
template<typename _Tp>
inline void do_fetch_and_store(std::atomic<_Tp>* _atomic, const _Tp& _value,
std::memory_order mem_odr)
{
_atomic->store(_value, mem_odr);
}
//--------------------------------------------------------------------//
template<typename _Tp>
inline void do_fetch_and_store(std::atomic<_Tp>* _atomic, const std::atomic<_Tp>& _value,
std::memory_order mem_odr)
{
_atomic->store(_value.load(), mem_odr);
}
//--------------------------------------------------------------------//
template<typename _Tp>
inline void do_compare_and_swap(std::atomic<_Tp>* _atomic, const _Tp& _value,
const OpFunction<_Tp>& _operator, std::memory_order mem_odr)
{
_Tp _expected = _Tp();
do {
_expected = _atomic->load();
} while (!(_atomic->compare_exchange_weak(_expected, _operator(_expected, _value), mem_odr)));
}
//--------------------------------------------------------------------//
template<typename _Tp>
inline void do_compare_and_swap(std::atomic<_Tp>* _atomic, const std::atomic<_Tp>& _atomic_value,
const OpFunction<_Tp>& _operator, std::memory_order mem_odr)
{
_Tp _expected = _Tp();
do {
_expected = _atomic->load();
} while (!(_atomic->compare_exchange_weak(_expected, _operator(_expected, _atomic_value.load()),
mem_odr)));
}
//--------------------------------------------------------------------//
} // namespace details
//------------------------------------------------------------------------//
// WITH ATOMIC TEMPLATE BASE TYPE AS SECOND PARAMETER
//------------------------------------------------------------------------//
template<typename T>
inline void set(std::atomic<T>* _atomic, const T& _desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
details::do_compare_and_swap(
_atomic, _desired, details::OpFunction<T>([](const T&, const T& y) { return y; }), mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void set(std::atomic<T>& _atomic, const T& _desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
set(&_atomic, _desired, mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void increment(std::atomic<T>* _atomic, const T& _increment, std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _increment,
details::OpFunction<T>([](const T& x, const T& y) { return x + y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void decrement(std::atomic<T>* _atomic, const T& _decrement, std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _decrement,
details::OpFunction<T>([](const T& x, const T& y) { return x - y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void multiply(std::atomic<T>* _atomic, const T& _factor, std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _factor,
details::OpFunction<T>([](const T& x, const T& y) { return x * y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void divide(std::atomic<T>* _atomic, const T& _factor, std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _factor,
details::OpFunction<T>([](const T& x, const T& y) { return x / y; }),
mem_odr);
}
//------------------------------------------------------------------------//
// WITH ATOMICS AS SECOND PARAMETER
//------------------------------------------------------------------------//
template<typename T>
inline void set(std::atomic<T>* _atomic, const std::atomic<T>& _atomic_desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
// details::do_fetch_and_store(_atomic, _desired);
details::do_compare_and_swap(_atomic, _atomic_desired,
details::OpFunction<T>([](const T&, const T& y) { return y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void set(std::atomic<T>& _atomic, const std::atomic<T>& _atomic_desired,
std::memory_order mem_odr)
{
set(&_atomic, _atomic_desired, mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void increment(std::atomic<T>* _atomic, const std::atomic<T>& _atomic_increment,
std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _atomic_increment,
details::OpFunction<T>([](const T& x, const T& y) { return x + y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void decrement(std::atomic<T>* _atomic, const std::atomic<T>& _atomic_decrement,
std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _atomic_decrement,
details::OpFunction<T>([](const T& x, const T& y) { return x - y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void multiply(std::atomic<T>* _atomic, const std::atomic<T>& _atomic_factor,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _factor,
details::OpFunction<T>([](const T& x, const T& y) { return x / y; }),
mem_odr);
}
//------------------------------------------------------------------------//
// WITH ATOMICS AS SECOND PARAMETER
//------------------------------------------------------------------------//
template <typename T>
inline void set(std::atomic<T>* _atomic,
const std::atomic<T>& _atomic_desired,
std::memory_order mem_odr = std::memory_order_seq_cst)
{
// details::do_fetch_and_store(_atomic, _desired);
details::do_compare_and_swap(
_atomic, _atomic_desired,
details::OpFunction<T>([](const T&, const T& y) { return y; }), mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void set(std::atomic<T>& _atomic,
const std::atomic<T>& _atomic_desired,
std::memory_order mem_odr)
{
set(&_atomic, _atomic_desired, mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void increment(std::atomic<T>* _atomic,
const std::atomic<T>& _atomic_increment,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _atomic_increment,
details::OpFunction<T>([](const T& x, const T& y) { return x + y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void decrement(std::atomic<T>* _atomic,
const std::atomic<T>& _atomic_decrement,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _atomic_decrement,
details::OpFunction<T>([](const T& x, const T& y) { return x - y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void multiply(std::atomic<T>* _atomic,
const std::atomic<T>& _atomic_factor,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _atomic_factor,
details::OpFunction<T>([](const T& x, const T& y) { return x * y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template <typename T>
inline void divide(std::atomic<T>* _atomic,
const std::atomic<T>& _atomic_factor,
std::memory_order mem_odr)
{
details::do_compare_and_swap(
_atomic, _atomic_factor,
details::OpFunction<T>([](const T& x, const T& y) { return x / y; }),
mem_odr);
}
{
details::do_compare_and_swap(_atomic, _atomic_factor,
details::OpFunction<T>([](const T& x, const T& y) { return x * y; }),
mem_odr);
}
//------------------------------------------------------------------------//
template<typename T>
inline void divide(std::atomic<T>* _atomic, const std::atomic<T>& _atomic_factor,
std::memory_order mem_odr)
{
details::do_compare_and_swap(_atomic, _atomic_factor,
details::OpFunction<T>([](const T& x, const T& y) { return x / y; }),
mem_odr);
}
//------------------------------------------------------------------------//
// STANDARD OVERLOAD //
//------------------------------------------------------------------------//
template <typename T>
inline void set(T* _non_atomic, const T& _desired)
{
*_non_atomic = _desired;
}
//------------------------------------------------------------------------//
template <typename T>
inline void set(T& _non_atomic, const T& _desired)
{
set(&_non_atomic, _desired);
}
//------------------------------------------------------------------------//
// STL PAIR OVERLOAD //
//------------------------------------------------------------------------//
//
//------------------------------------------------------------------------//
// WITH ATOMIC TEMPLATE TYPE AS SECOND PARAMETER
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _desired)
{
set(&_atomic->first, _desired.first);
set(&_atomic->second, _desired.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>& _atomic,
const std::pair<T, U>& _desired)
{
set(&_atomic, _desired);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void increment(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _increment)
{
increment(&_atomic->first, _increment.first);
increment(&_atomic->second, _increment.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void decrement(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _decrement)
{
decrement(&_atomic->first, _decrement.first);
decrement(&_atomic->second, _decrement.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void multiply(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _factor)
{
multiply(&_atomic->first, _factor.first);
multiply(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void divide(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
//------------------------------------------------------------------------//
// STANDARD OVERLOAD //
//------------------------------------------------------------------------//
template<typename T>
inline void set(T* _non_atomic, const T& _desired)
{
*_non_atomic = _desired;
}
//------------------------------------------------------------------------//
template<typename T>
inline void set(T& _non_atomic, const T& _desired)
{
set(&_non_atomic, _desired);
}
//------------------------------------------------------------------------//
// STL PAIR OVERLOAD //
//------------------------------------------------------------------------//
//
//------------------------------------------------------------------------//
// WITH ATOMIC TEMPLATE TYPE AS SECOND PARAMETER
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>* _atomic, const std::pair<T, U>& _desired)
{
set(&_atomic->first, _desired.first);
set(&_atomic->second, _desired.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>& _atomic, const std::pair<T, U>& _desired)
{
set(&_atomic, _desired);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void increment(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _increment)
{
increment(&_atomic->first, _increment.first);
increment(&_atomic->second, _increment.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void decrement(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _decrement)
{
decrement(&_atomic->first, _decrement.first);
decrement(&_atomic->second, _decrement.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void multiply(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _factor)
{
divide(&_atomic->first, _factor.first);
divide(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
// WITH ATOMICS AS SECOND PARAMETER
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _desired)
{
set(&_atomic->first, _desired.first);
set(&_atomic->second, _desired.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>& _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _desired)
{
set(&_atomic, _desired);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void increment(
std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _increment)
{
increment(&_atomic->first, _increment.first);
increment(&_atomic->second, _increment.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void decrement(
std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _decrement)
{
decrement(&_atomic->first, _decrement.first);
decrement(&_atomic->second, _decrement.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void multiply(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _factor)
{
multiply(&_atomic->first, _factor.first);
multiply(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline void divide(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
{
multiply(&_atomic->first, _factor.first);
multiply(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void divide(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<T, U>& _factor)
{
divide(&_atomic->first, _factor.first);
divide(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
// WITH ATOMICS AS SECOND PARAMETER
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _desired)
{
set(&_atomic->first, _desired.first);
set(&_atomic->second, _desired.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void set(std::pair<std::atomic<T>, std::atomic<U>>& _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _desired)
{
set(&_atomic, _desired);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void increment(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _increment)
{
increment(&_atomic->first, _increment.first);
increment(&_atomic->second, _increment.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void decrement(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _decrement)
{
decrement(&_atomic->first, _decrement.first);
decrement(&_atomic->second, _decrement.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void multiply(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _factor)
{
divide(&_atomic->first, _factor.first);
divide(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
{
multiply(&_atomic->first, _factor.first);
multiply(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline void divide(std::pair<std::atomic<T>, std::atomic<U>>* _atomic,
const std::pair<std::atomic<T>, std::atomic<U>>& _factor)
{
divide(&_atomic->first, _factor.first);
divide(&_atomic->second, _factor.second);
}
//------------------------------------------------------------------------//
//------------------------------------------------------------------------//
template <typename T>
inline T get(const T& _non_atomic)
{
return _non_atomic;
}
//------------------------------------------------------------------------//
template <typename T>
inline T get(const T& _non_atomic, std::memory_order)
{
return _non_atomic;
}
//------------------------------------------------------------------------//
template <typename T>
inline T get(const std::atomic<T>& _atomic)
{
return _atomic.load();
}
//------------------------------------------------------------------------//
template <typename T>
inline T get(const std::atomic<T>& _atomic, std::memory_order mem_odr)
{
return _atomic.load(mem_odr);
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline std::pair<T, U> get(
const std::pair<std::atomic<T>, std::atomic<U>>& _atomic)
{
return std::pair<T, U>(get(_atomic.first), get(_atomic.second));
}
//------------------------------------------------------------------------//
template <typename T, typename U>
inline std::pair<T, U> get(
const std::pair<std::atomic<T>, std::atomic<U>>& _atomic,
std::memory_order mem_odr)
{
return std::pair<T, U>(get(_atomic.first, mem_odr),
get(_atomic.second, mem_odr));
}
//------------------------------------------------------------------------//
//------------------------------------------------------------------------//
template<typename T>
inline T get(const T& _non_atomic)
{
return _non_atomic;
}
//------------------------------------------------------------------------//
template<typename T>
inline T get(const T& _non_atomic, std::memory_order)
{
return _non_atomic;
}
//------------------------------------------------------------------------//
template<typename T>
inline T get(const std::atomic<T>& _atomic)
{
return _atomic.load();
}
//------------------------------------------------------------------------//
template<typename T>
inline T get(const std::atomic<T>& _atomic, std::memory_order mem_odr)
{
return _atomic.load(mem_odr);
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline std::pair<T, U> get(const std::pair<std::atomic<T>, std::atomic<U>>& _atomic)
{
return std::pair<T, U>(get(_atomic.first), get(_atomic.second));
}
//------------------------------------------------------------------------//
template<typename T, typename U>
inline std::pair<T, U> get(const std::pair<std::atomic<T>, std::atomic<U>>& _atomic,
std::memory_order mem_odr)
{
return std::pair<T, U>(get(_atomic.first, mem_odr), get(_atomic.second, mem_odr));
}
//------------------------------------------------------------------------//
//------------------------------------------------------------------------//
// for plain old data (POD) and pairs (e.g. std::pair<atomic<T>, atomic<U>>)
template <typename _Tp_base, typename _Tp_atom>
inline _Tp_base base(const _Tp_atom& _atomic)
{
return get(_atomic);
}
//------------------------------------------------------------------------//
//------------------------------------------------------------------------//
// for plain old data (POD) and pairs (e.g. std::pair<atomic<T>, atomic<U>>)
template<typename _Tp_base, typename _Tp_atom>
inline _Tp_base base(const _Tp_atom& _atomic)
{
return get(_atomic);
}
//------------------------------------------------------------------------//
} // namespace atomics
@@ -40,28 +40,27 @@
#ifndef tsactioninitialization_hh_
#define tsactioninitialization_hh_
#include "globals.hh"
#include "G4VUserActionInitialization.hh"
#include "G4AutoLock.hh"
#include "G4Threading.hh"
#include "G4VUserActionInitialization.hh"
#include "globals.hh"
class TSActionInitialization : public G4VUserActionInitialization
{
public:
// Constructor and Destructors
TSActionInitialization();
virtual ~TSActionInitialization();
public:
// Constructor and Destructors
TSActionInitialization();
virtual ~TSActionInitialization();
static TSActionInitialization* Instance();
static TSActionInitialization* Instance();
public:
virtual void BuildForMaster() const;
virtual void Build() const;
public:
virtual void BuildForMaster() const;
virtual void Build() const;
private:
// Private functions
static TSActionInitialization* fgInstance;
private:
// Private functions
static TSActionInitialization* fgInstance;
};
#endif
@@ -54,9 +54,9 @@
#ifndef tsdetectorconstruction_hh
#define tsdetectorconstruction_hh 1
#include "globals.hh"
#include "G4VUserDetectorConstruction.hh"
#include "G4ThreeVector.hh"
#include "G4VUserDetectorConstruction.hh"
#include "globals.hh"
#include <map>
#include <set>
@@ -70,45 +70,42 @@ class G4Material;
class TSDetectorConstruction : public G4VUserDetectorConstruction
{
public:
typedef std::map<G4String, G4Material*> MaterialCollection_t;
typedef std::set<G4LogicalVolume*> ScoringVolumes_t;
public:
typedef std::map<G4String, G4Material*> MaterialCollection_t;
typedef std::set<G4LogicalVolume*> ScoringVolumes_t;
public:
TSDetectorConstruction();
virtual ~TSDetectorConstruction();
public:
TSDetectorConstruction();
virtual ~TSDetectorConstruction();
static TSDetectorConstruction* Instance();
static TSDetectorConstruction* Instance();
public:
G4VPhysicalVolume* Construct();
inline const G4ThreeVector& GetWorldDimensions() const { return fWorldDim; }
inline const ScoringVolumes_t& GetScoringVolumes() const
{
return fScoringVolumes;
}
inline const G4String& GetMFDName() const { return fMfdName; }
inline G4int GetTotalTargets() const
{
return fTargetSections.x() * fTargetSections.y() * fTargetSections.z();
}
public:
G4VPhysicalVolume* Construct();
inline const G4ThreeVector& GetWorldDimensions() const { return fWorldDim; }
inline const ScoringVolumes_t& GetScoringVolumes() const { return fScoringVolumes; }
inline const G4String& GetMFDName() const { return fMfdName; }
inline G4int GetTotalTargets() const
{
return fTargetSections.x() * fTargetSections.y() * fTargetSections.z();
}
protected:
virtual MaterialCollection_t ConstructMaterials();
virtual G4VPhysicalVolume* ConstructWorld(const MaterialCollection_t&);
virtual void ConstructSDandField();
protected:
virtual MaterialCollection_t ConstructMaterials();
virtual G4VPhysicalVolume* ConstructWorld(const MaterialCollection_t&);
virtual void ConstructSDandField();
private:
static TSDetectorConstruction* fgInstance;
G4VPhysicalVolume* fWorldPhys;
ScoringVolumes_t fScoringVolumes;
G4String fWorldMaterialName;
G4String fTargetMaterialName;
G4String fCasingMaterialName;
G4ThreeVector fWorldDim;
G4ThreeVector fTargetDim;
G4ThreeVector fTargetSections;
G4String fMfdName;
private:
static TSDetectorConstruction* fgInstance;
G4VPhysicalVolume* fWorldPhys;
ScoringVolumes_t fScoringVolumes;
G4String fWorldMaterialName;
G4String fTargetMaterialName;
G4String fCasingMaterialName;
G4ThreeVector fWorldDim;
G4ThreeVector fTargetDim;
G4ThreeVector fTargetSections;
G4String fMfdName;
};
#endif
@@ -42,26 +42,26 @@
#ifndef tsphysicslist_hh
#define tsphysicslist_hh 1
#include "globals.hh"
#include "G4VUserPhysicsList.hh"
#include "G4VPhysicsConstructor.hh"
#include "G4VUserPhysicsList.hh"
#include "globals.hh"
#include <deque>
class TSPhysicsList : public G4VUserPhysicsList
{
public:
typedef std::deque<G4VPhysicsConstructor*> PhysicsSet_t;
public:
typedef std::deque<G4VPhysicsConstructor*> PhysicsSet_t;
public:
TSPhysicsList();
virtual ~TSPhysicsList();
public:
TSPhysicsList();
virtual ~TSPhysicsList();
public:
void ConstructParticle() override;
void ConstructProcess() override;
public:
void ConstructParticle() override;
void ConstructProcess() override;
private:
PhysicsSet_t fConstructors;
private:
PhysicsSet_t fConstructors;
};
#endif
@@ -39,23 +39,23 @@
#ifndef tsprimarygeneratoraction_hh
#define tsprimarygeneratoraction_hh 1
#include "globals.hh"
#include "G4VUserPrimaryGeneratorAction.hh"
#include "globals.hh"
class G4Event;
class G4ParticleGun;
class TSPrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
{
public:
TSPrimaryGeneratorAction();
virtual ~TSPrimaryGeneratorAction();
public:
TSPrimaryGeneratorAction();
virtual ~TSPrimaryGeneratorAction();
public:
void GeneratePrimaries(G4Event*);
public:
void GeneratePrimaries(G4Event*);
private:
G4ParticleGun* fGun;
private:
G4ParticleGun* fGun;
};
#endif
@@ -54,15 +54,14 @@
#ifndef tsrun_h
#define tsrun_h 1
#include "globals.hh"
#include "G4Run.hh"
#include "G4Event.hh"
#include "G4THitsMap.hh"
#include "G4TAtomicHitsMap.hh"
#include "G4THitsVector.hh"
#include "G4StatAnalysis.hh"
#include "G4ConvergenceTester.hh"
#include "G4Event.hh"
#include "G4Run.hh"
#include "G4StatAnalysis.hh"
#include "G4TAtomicHitsMap.hh"
#include "G4THitsMap.hh"
#include "G4THitsVector.hh"
#include "globals.hh"
#include <vector>
@@ -70,42 +69,42 @@ class G4Event;
// template <typename _Tp> using G4StatContainer = G4THitsMap<_Tp>;
// template <typename _Tp> using G4StatContainer = G4THitsVector<_Tp>;
template <typename _Tp>
template<typename _Tp>
using G4StatContainer = G4THitsDeque<_Tp>;
class TSRun : public G4Run
{
public:
typedef std::map<G4int, G4double> MutexHitsMap_t;
public:
typedef std::map<G4int, G4double> MutexHitsMap_t;
public:
TSRun(const G4String&);
virtual ~TSRun();
public:
TSRun(const G4String&);
virtual ~TSRun();
// virtual method from G4Run.
// The method is overriden in this class for scoring.
virtual void RecordEvent(const G4Event*);
// virtual method from G4Run.
// The method is overriden in this class for scoring.
virtual void RecordEvent(const G4Event*);
// Access methods for scoring information.
// - Get HitsMap of this RUN.
G4THitsMap<G4double>* GetHitsMap(const G4String& collname) const;
G4TAtomicHitsMap<G4double>* GetAtomicHitsMap(const G4String&) const;
MutexHitsMap_t* GetMutexHitsMap(const G4String&) const;
G4StatContainer<G4StatAnalysis>* GetStatMap(const G4String& collname) const;
G4StatContainer<G4ConvergenceTester>* GetConvMap(const G4String&) const;
// Access methods for scoring information.
// - Get HitsMap of this RUN.
G4THitsMap<G4double>* GetHitsMap(const G4String& collname) const;
G4TAtomicHitsMap<G4double>* GetAtomicHitsMap(const G4String&) const;
MutexHitsMap_t* GetMutexHitsMap(const G4String&) const;
G4StatContainer<G4StatAnalysis>* GetStatMap(const G4String& collname) const;
G4StatContainer<G4ConvergenceTester>* GetConvMap(const G4String&) const;
void ConstructMFD(const G4String&);
void ConstructMFD(const G4String&);
virtual void Merge(const G4Run*);
virtual void Merge(const G4Run*);
private:
std::vector<G4String> fCollNames;
std::vector<G4int> fCollIDs;
std::vector<G4THitsMap<G4double>*> fRunMaps;
std::vector<G4StatContainer<G4StatAnalysis>*> fStatMaps;
static std::vector<G4TAtomicHitsMap<G4double>*> fAtomicRunMaps;
static std::map<G4String, MutexHitsMap_t> fMutexRunMaps;
static std::vector<G4StatContainer<G4ConvergenceTester>*> fConvMaps;
private:
std::vector<G4String> fCollNames;
std::vector<G4int> fCollIDs;
std::vector<G4THitsMap<G4double>*> fRunMaps;
std::vector<G4StatContainer<G4StatAnalysis>*> fStatMaps;
static std::vector<G4TAtomicHitsMap<G4double>*> fAtomicRunMaps;
static std::map<G4String, MutexHitsMap_t> fMutexRunMaps;
static std::vector<G4StatContainer<G4ConvergenceTester>*> fConvMaps;
};
#endif
@@ -37,11 +37,12 @@
#ifndef tsrunaction_hh
#define tsrunaction_hh 1
#include "globals.hh"
#include "G4UserRunAction.hh"
#include <vector>
#include "globals.hh"
#include <map>
#include <tuple>
#include <vector>
class G4Run;
class G4Timer;
@@ -49,24 +50,24 @@ class TSDetectorConstruction;
class TSRunAction : public G4UserRunAction
{
public:
typedef std::tuple<G4double, G4double, G4double> Compare_t;
typedef std::map<G4int, Compare_t> IDcompare_t;
typedef std::map<G4String, IDcompare_t> TypeCompare_t;
public:
typedef std::tuple<G4double, G4double, G4double> Compare_t;
typedef std::map<G4int, Compare_t> IDcompare_t;
typedef std::map<G4String, IDcompare_t> TypeCompare_t;
public:
TSRunAction();
virtual ~TSRunAction();
public:
TSRunAction();
virtual ~TSRunAction();
public:
virtual void BeginOfRunAction(const G4Run*);
virtual void EndOfRunAction(const G4Run*);
virtual G4Run* GenerateRun();
public:
virtual void BeginOfRunAction(const G4Run*);
virtual void EndOfRunAction(const G4Run*);
virtual G4Run* GenerateRun();
private:
TSDetectorConstruction* fDetector;
G4String fName;
TypeCompare_t fTypeCompare;
private:
TSDetectorConstruction* fDetector;
G4String fName;
TypeCompare_t fTypeCompare;
};
#endif
@@ -38,6 +38,7 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#include "TSActionInitialization.hh"
#include "TSDetectorConstruction.hh"
#include "TSPrimaryGeneratorAction.hh"
#include "TSRunAction.hh"
@@ -55,11 +56,17 @@ TSActionInitialization* TSActionInitialization::Instance()
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSActionInitialization::TSActionInitialization() { fgInstance = this; }
TSActionInitialization::TSActionInitialization()
{
fgInstance = this;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSActionInitialization::~TSActionInitialization() { fgInstance = 0; }
TSActionInitialization::~TSActionInitialization()
{
fgInstance = 0;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -53,22 +53,21 @@
#include "TSDetectorConstruction.hh"
#include "G4RunManager.hh"
#include "G4Box.hh"
#include "G4LogicalVolume.hh"
#include "G4VPhysicalVolume.hh"
#include "G4Material.hh"
#include "G4NistManager.hh"
#include "G4PVPlacement.hh"
#include "G4VisAttributes.hh"
#include "G4Colour.hh"
#include "G4UnitsTable.hh"
#include "G4UserLimits.hh"
#include "G4SDManager.hh"
#include "G4LogicalVolume.hh"
#include "G4Material.hh"
#include "G4MultiFunctionalDetector.hh"
#include "G4NistManager.hh"
#include "G4PSEnergyDeposit.hh"
#include "G4PSNofStep.hh"
#include "G4PVPlacement.hh"
#include "G4RunManager.hh"
#include "G4SDManager.hh"
#include "G4UnitsTable.hh"
#include "G4UserLimits.hh"
#include "G4VPhysicalVolume.hh"
#include "G4VisAttributes.hh"
using namespace CLHEP;
@@ -86,21 +85,24 @@ TSDetectorConstruction* TSDetectorConstruction::Instance()
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSDetectorConstruction::TSDetectorConstruction()
: fWorldPhys(0)
, fWorldMaterialName("G4_Galactic")
, fTargetMaterialName("G4_B")
, fCasingMaterialName("G4_WATER")
, fWorldDim(G4ThreeVector(0.5 * m, 0.5 * m, 0.5 * m))
, fTargetDim(G4ThreeVector(0.5 * m, 0.5 * m, 0.5 * m))
, fTargetSections(G4ThreeVector(5, 5, 5))
, fMfdName("Target_MFD")
: fWorldPhys(0),
fWorldMaterialName("G4_Galactic"),
fTargetMaterialName("G4_B"),
fCasingMaterialName("G4_WATER"),
fWorldDim(G4ThreeVector(0.5 * m, 0.5 * m, 0.5 * m)),
fTargetDim(G4ThreeVector(0.5 * m, 0.5 * m, 0.5 * m)),
fTargetSections(G4ThreeVector(5, 5, 5)),
fMfdName("Target_MFD")
{
fgInstance = this;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSDetectorConstruction::~TSDetectorConstruction() { fgInstance = 0; }
TSDetectorConstruction::~TSDetectorConstruction()
{
fgInstance = 0;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -111,13 +113,12 @@ G4VPhysicalVolume* TSDetectorConstruction::Construct()
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSDetectorConstruction::MaterialCollection_t
TSDetectorConstruction::ConstructMaterials()
TSDetectorConstruction::MaterialCollection_t TSDetectorConstruction::ConstructMaterials()
{
MaterialCollection_t materials;
G4NistManager* nist = G4NistManager::Instance();
materials["World"] = nist->FindOrBuildMaterial(fWorldMaterialName);
materials["World"] = nist->FindOrBuildMaterial(fWorldMaterialName);
materials["Target"] = nist->FindOrBuildMaterial(fTargetMaterialName);
materials["Casing"] = nist->FindOrBuildMaterial(fCasingMaterialName);
@@ -126,19 +127,17 @@ TSDetectorConstruction::ConstructMaterials()
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(
const MaterialCollection_t& materials)
G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(const MaterialCollection_t& materials)
{
G4UserLimits* steplimit =
new G4UserLimits(0.1 * (fTargetDim.z() / fTargetSections.z()));
G4UserLimits* steplimit = new G4UserLimits(0.1 * (fTargetDim.z() / fTargetSections.z()));
G4bool check_overlap = false;
G4Box* world_solid = new G4Box("World", 0.5 * fWorldDim.x(),
0.5 * fWorldDim.y(), 0.5 * fWorldDim.z());
G4Box* world_solid =
new G4Box("World", 0.5 * fWorldDim.x(), 0.5 * fWorldDim.y(), 0.5 * fWorldDim.z());
G4LogicalVolume* world_log =
new G4LogicalVolume(world_solid, materials.find("World")->second, "World");
fWorldPhys = new G4PVPlacement(0, G4ThreeVector(0.), "World", world_log, 0,
false, 0, check_overlap);
fWorldPhys =
new G4PVPlacement(0, G4ThreeVector(0.), "World", world_log, 0, false, 0, check_overlap);
G4int nz = fTargetSections.z();
G4int ny = fTargetSections.y();
@@ -155,9 +154,9 @@ G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(
//------------------------------------------------------------------------//
// Set Visual Attributes
//------------------------------------------------------------------------//
G4VisAttributes* red = new G4VisAttributes(G4Color(1., 0., 0., 1.0));
G4VisAttributes* red = new G4VisAttributes(G4Color(1., 0., 0., 1.0));
G4VisAttributes* green = new G4VisAttributes(G4Color(0., 1., 0., 0.25));
G4VisAttributes* blue = new G4VisAttributes(G4Color(0., 0., 1., 0.1));
G4VisAttributes* blue = new G4VisAttributes(G4Color(0., 0., 1., 0.1));
G4VisAttributes* white = new G4VisAttributes(G4Color(1., 1., 1., 1.));
white->SetVisibility(true);
@@ -172,35 +171,28 @@ G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(
world_log->SetVisAttributes(white);
for(G4int k = 0; k < nz; ++k)
for(G4int j = 0; j < ny; ++j)
for(G4int i = 0; i < nx; ++i)
{
for (G4int k = 0; k < nz; ++k)
for (G4int j = 0; j < ny; ++j)
for (G4int i = 0; i < nx; ++i) {
// displacement of section
G4double dx =
0.5 * sx + static_cast<G4double>(i) * sx - 0.5 * fWorldDim.x();
G4double dy =
0.5 * sy + static_cast<G4double>(j) * sy - 0.5 * fWorldDim.y();
G4double dz =
0.5 * sz + static_cast<G4double>(k) * sz - 0.5 * fWorldDim.z();
G4double dx = 0.5 * sx + static_cast<G4double>(i) * sx - 0.5 * fWorldDim.x();
G4double dy = 0.5 * sy + static_cast<G4double>(j) * sy - 0.5 * fWorldDim.y();
G4double dz = 0.5 * sz + static_cast<G4double>(k) * sz - 0.5 * fWorldDim.z();
G4ThreeVector td = G4ThreeVector(dx, dy, -dz);
// make unique name
std::stringstream ss_name;
ss_name << "Target_" << i << "_" << j << "_" << k;
G4Box* target_solid =
new G4Box(ss_name.str(), 0.5 * sx, 0.5 * sy, 0.5 * sz);
G4Box* target_solid = new G4Box(ss_name.str(), 0.5 * sx, 0.5 * sy, 0.5 * sz);
G4Material* target_material = 0;
G4bool is_casing = true;
G4bool is_casing = true;
if(j == 0 || j + 1 == ny || i == 0 || i + 1 == nx ||
(nz > 1 && (k == 0 || k + 1 == nz)))
if (j == 0 || j + 1 == ny || i == 0 || i + 1 == nx || (nz > 1 && (k == 0 || k + 1 == nz)))
target_material = materials.find("Casing")->second;
else
{
else {
target_material = materials.find("Target")->second;
is_casing = false;
is_casing = false;
}
G4LogicalVolume* target_log =
@@ -213,10 +205,9 @@ G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(
fScoringVolumes.insert(target_log);
if(is_casing)
if (is_casing)
target_log->SetVisAttributes(blue);
else
{
else {
// making a checkerboard for kicks...
G4bool even_z = (k % 2 == 0) ? true : false;
G4bool even_y = (j % 2 == 0) ? true : false;
@@ -224,16 +215,15 @@ G4VPhysicalVolume* TSDetectorConstruction::ConstructWorld(
G4VisAttributes* theColor = nullptr;
if((even_z))
{
if((even_y && even_x) || (!even_y && !even_x))
if ((even_z)) {
if ((even_y && even_x) || (!even_y && !even_x))
theColor = red;
else
theColor = green;
}
else // ! even_z
{
if((!even_y && even_x) || (even_y && !even_x))
if ((!even_y && even_x) || (even_y && !even_x))
theColor = red;
else
theColor = green;
@@ -262,8 +252,7 @@ void TSDetectorConstruction::ConstructSDandField()
MFDet->RegisterPrimitive(nstep);
// add scoring volumes
for(auto ite : fScoringVolumes)
{
for (auto ite : fScoringVolumes) {
SetSensitiveDetector(ite, MFDet);
}
}
@@ -41,10 +41,10 @@
#include "TSPhysicsList.hh"
#include "G4RunManager.hh"
#include "G4ParticleDefinition.hh"
#include "G4ProcessManager.hh"
#include "G4ParticleTypes.hh"
#include "G4ProcessManager.hh"
#include "G4RunManager.hh"
// Hadrons
#include "G4BaryonConstructor.hh"
@@ -65,20 +65,19 @@
// Physics List Helper
#include "G4PhysicsListHelper.hh"
#include "G4StepLimiter.hh"
// Constructors
#include "G4DecayPhysics.hh"
#include "G4EmStandardPhysics_option3.hh"
#include "G4EmStandardPhysics_option4.hh"
#include "G4RadioactiveDecayPhysics.hh"
#include "G4HadronPhysicsQGSP_BERT.hh"
#include "G4HadronPhysicsQGSP_BERT_HP.hh"
#include "G4HadronElasticPhysics.hh"
#include "G4HadronElasticPhysicsHP.hh"
#include "G4IonElasticPhysics.hh"
#include "G4HadronPhysicsQGSP_BERT.hh"
#include "G4HadronPhysicsQGSP_BERT_HP.hh"
#include "G4IonBinaryCascadePhysics.hh"
#include "G4DecayPhysics.hh"
#include "G4IonElasticPhysics.hh"
#include "G4RadioactiveDecayPhysics.hh"
#include <set>
@@ -101,7 +100,7 @@ TSPhysicsList::TSPhysicsList()
TSPhysicsList::~TSPhysicsList()
{
for(auto ite : fConstructors)
for (auto ite : fConstructors)
delete ite;
}
@@ -109,8 +108,7 @@ TSPhysicsList::~TSPhysicsList()
void TSPhysicsList::ConstructParticle()
{
for(auto c : fConstructors)
{
for (auto c : fConstructors) {
c->ConstructParticle();
}
}
@@ -123,8 +121,7 @@ void TSPhysicsList::ConstructProcess()
//
AddTransportation();
for(auto c : fConstructors)
{
for (auto c : fConstructors) {
c->ConstructProcess();
}
@@ -148,13 +145,11 @@ void TSPhysicsList::ConstructProcess()
G4PhysicsListHelper* ph = G4PhysicsListHelper::GetPhysicsListHelper();
while((*particleIterator)())
{
while ((*particleIterator)()) {
G4ParticleDefinition* particle = particleIterator->value();
G4String pname = particle->GetParticleName();
G4String pname = particle->GetParticleName();
if(step_limit_particles.find(pname) != step_limit_particles.end() ||
particle->GetPDGCharge())
if (step_limit_particles.find(pname) != step_limit_particles.end() || particle->GetPDGCharge())
{
ph->RegisterProcess(new G4StepLimiter, particle);
}
@@ -37,15 +37,14 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#include "TSPrimaryGeneratorAction.hh"
#include "TSDetectorConstruction.hh"
#include "G4RunManager.hh"
#include "G4Event.hh"
#include "G4ParticleDefinition.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "G4Profiler.hh"
#include "G4TiMemory.hh"
#include "G4RunManager.hh"
using namespace CLHEP;
@@ -57,8 +56,7 @@ TSPrimaryGeneratorAction::TSPrimaryGeneratorAction()
G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
G4String particleName;
G4ParticleDefinition* particle =
particleTable->FindParticle(particleName = "neutron");
G4ParticleDefinition* particle = particleTable->FindParticle(particleName = "neutron");
fGun->SetParticleDefinition(particle);
fGun->SetParticleEnergy(1. * MeV);
@@ -66,20 +64,21 @@ TSPrimaryGeneratorAction::TSPrimaryGeneratorAction()
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSPrimaryGeneratorAction::~TSPrimaryGeneratorAction() { delete fGun; }
TSPrimaryGeneratorAction::~TSPrimaryGeneratorAction()
{
delete fGun;
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void TSPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
G4USER_SCOPED_PROFILE(__FUNCTION__);
static TSDetectorConstruction* detector = TSDetectorConstruction::Instance();
G4ThreeVector dir(0., 0., 1.);
G4ThreeVector pos(
detector->GetWorldDimensions().x() * (G4UniformRand() - 0.5),
detector->GetWorldDimensions().y() * (G4UniformRand() - 0.5),
-0.5 * detector->GetWorldDimensions().z());
G4ThreeVector pos(detector->GetWorldDimensions().x() * (G4UniformRand() - 0.5),
detector->GetWorldDimensions().y() * (G4UniformRand() - 0.5),
-0.5 * detector->GetWorldDimensions().z());
dir /= dir.mag();
fGun->SetParticleMomentumDirection(dir);
@@ -70,12 +70,12 @@
#include "TSRun.hh"
#include "G4SDManager.hh"
#include "G4MultiFunctionalDetector.hh"
#include "G4VPrimitiveScorer.hh"
#include "G4TiMemory.hh"
#include "TSDetectorConstruction.hh"
#include "G4MultiFunctionalDetector.hh"
#include "G4SDManager.hh"
#include "G4VPrimitiveScorer.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
std::vector<G4TAtomicHitsMap<G4double>*> TSRun::fAtomicRunMaps;
@@ -86,8 +86,7 @@ std::vector<G4StatContainer<G4ConvergenceTester>*> TSRun::fConvMaps;
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSRun::TSRun(const G4String& mfd_name)
: G4Run()
TSRun::TSRun(const G4String& mfd_name) : G4Run()
{
ConstructMFD(mfd_name);
}
@@ -97,15 +96,14 @@ TSRun::TSRun(const G4String& mfd_name)
TSRun::~TSRun()
{
//--- Clear HitsMap for RUN
for(unsigned i = 0; i < fRunMaps.size(); ++i)
for (unsigned i = 0; i < fRunMaps.size(); ++i)
delete fRunMaps[i];
if(!G4Threading::IsWorkerThread())
{
for(unsigned i = 0; i < fAtomicRunMaps.size(); ++i)
if (!G4Threading::IsWorkerThread()) {
for (unsigned i = 0; i < fAtomicRunMaps.size(); ++i)
delete fAtomicRunMaps[i];
for(auto& itr : fConvMaps)
for (auto& itr : fConvMaps)
delete itr;
fAtomicRunMaps.clear();
@@ -125,13 +123,11 @@ void TSRun::ConstructMFD(const G4String& mfdName)
// Get CollectionIDs for HitCollections.
//=================================================
G4MultiFunctionalDetector* mfd =
(G4MultiFunctionalDetector*) (SDman->FindSensitiveDetector(mfdName));
(G4MultiFunctionalDetector*)(SDman->FindSensitiveDetector(mfdName));
//
if(mfd)
{
if (mfd) {
//--- Loop over the registered primitive scorers.
for(G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++)
{
for (G4int icol = 0; icol < mfd->GetNumberOfPrimitives(); icol++) {
// Get Primitive Scorer object.
G4VPrimitiveScorer* scorer = mfd->GetPrimitive(icol);
// collection name and collectionID for HitsCollection,
@@ -139,36 +135,28 @@ void TSRun::ConstructMFD(const G4String& mfdName)
// of primitive scorer.
// The collection name is given by <MFD name>/<Primitive
// Scorer name>.
G4String collectionName = scorer->GetName();
G4String collectionName = scorer->GetName();
G4String fullCollectionName = mfdName + "/" + collectionName;
G4int collectionID = SDman->GetCollectionID(fullCollectionName);
G4int collectionID = SDman->GetCollectionID(fullCollectionName);
//
if(collectionID >= 0)
{
G4cout << "++ " << fullCollectionName << " id " << collectionID
<< G4endl;
if (collectionID >= 0) {
G4cout << "++ " << fullCollectionName << " id " << collectionID << G4endl;
// Store obtained HitsCollection information into data members.
// And, creates new G4THitsMap for accumulating quantities during RUN.
fCollNames.push_back(fullCollectionName);
fCollIDs.push_back(collectionID);
fRunMaps.push_back(new G4THitsMap<G4double>(mfdName, collectionName));
fStatMaps.push_back(new G4StatContainer<G4StatAnalysis>(
mfdName, collectionName,
TSDetectorConstruction::Instance()->GetTotalTargets()));
if(!G4Threading::IsWorkerThread())
{
fAtomicRunMaps.push_back(
new G4TAtomicHitsMap<G4double>(mfdName, collectionName));
mfdName, collectionName, TSDetectorConstruction::Instance()->GetTotalTargets()));
if (!G4Threading::IsWorkerThread()) {
fAtomicRunMaps.push_back(new G4TAtomicHitsMap<G4double>(mfdName, collectionName));
fMutexRunMaps[fCollNames[collectionID]].clear();
fConvMaps.push_back(new G4StatContainer<G4ConvergenceTester>(
mfdName, collectionName,
TSDetectorConstruction::Instance()->GetTotalTargets()));
mfdName, collectionName, TSDetectorConstruction::Instance()->GetTotalTargets()));
}
}
else
{
G4cout << "** collection " << fullCollectionName << " not found. "
<< G4endl;
else {
G4cout << "** collection " << fullCollectionName << " not found. " << G4endl;
}
}
}
@@ -187,52 +175,43 @@ void TSRun::RecordEvent(const G4Event* aEvent)
// HitsCollection of This Event
//============================
G4HCofThisEvent* HCE = aEvent->GetHCofThisEvent();
if(!HCE)
return;
if (!HCE) return;
for(unsigned i = 0; i < fCollIDs.size(); ++i)
{
for (unsigned i = 0; i < fCollIDs.size(); ++i) {
G4int fCollID = fCollIDs.at(i);
//=======================================================
// Sum up HitsMap of this Event into HitsMap of this RUN
//=======================================================
G4THitsMap<G4double>* EvtMap = 0;
if(fCollID >= 0) // Collection is attached to HCE
if (fCollID >= 0) // Collection is attached to HCE
EvtMap = static_cast<G4THitsMap<G4double>*>(HCE->GetHC(fCollID));
else
G4cout << " Error EvtMap Not Found " << G4endl;
G4USER_SCOPED_PROFILE(fCollNames.at(i));
if(EvtMap)
{
if (EvtMap) {
//=== Sum up HitsMap of this event to HitsMap of RUN.===
{
G4USER_SCOPED_PROFILE("ThreadLocal");
*fRunMaps[fCollID] += *EvtMap;
}
//=== Sum up HitsMap of this event to StatMap of RUN.===
{
G4USER_SCOPED_PROFILE("ThreadLocal/G4StatAnalysis");
// G4StatAnalysis map
*fStatMaps[fCollID] += *EvtMap;
}
//=== Sum up HitsMap of this event to atomic HitsMap of RUN.===
{
G4USER_SCOPED_PROFILE("Global/Atomic");
*fAtomicRunMaps[fCollID] += *EvtMap;
}
//=== Sum up HitsMap of this event to MutexMap of RUN.===
{
G4USER_SCOPED_PROFILE("Global/Mutex");
// mutex run map
static G4Mutex mtx = G4MUTEX_INITIALIZER;
G4AutoLock lock(&mtx);
for(const auto& itr : *EvtMap)
for (const auto& itr : *EvtMap)
fMutexRunMaps[fCollNames[fCollID]][itr.first] += *itr.second;
}
//=== Sum up HitsMap of this event to MutexMap of RUN.===
{
G4USER_SCOPED_PROFILE("Global/Mutex/G4ConvergenceTester");
// G4ConvergenceTester run map
static G4Mutex mtx = G4MUTEX_INITIALIZER;
G4AutoLock lock(&mtx);
@@ -249,8 +228,7 @@ void TSRun::Merge(const G4Run* aTSRun)
{
const TSRun* localTSRun = static_cast<const TSRun*>(aTSRun);
for(unsigned i = 0; i < fRunMaps.size(); ++i)
{
for (unsigned i = 0; i < fRunMaps.size(); ++i) {
*fRunMaps[i] += *localTSRun->fRunMaps[i];
*fStatMaps[i] += *localTSRun->fStatMaps[i];
}
@@ -265,10 +243,8 @@ void TSRun::Merge(const G4Run* aTSRun)
// <MultiFunctional Detector Name>/<Primitive Scorer Name>
G4THitsMap<G4double>* TSRun::GetHitsMap(const G4String& collName) const
{
for(unsigned i = 0; i < fCollNames.size(); ++i)
{
if(collName == fCollNames[i])
return fRunMaps[i];
for (unsigned i = 0; i < fCollNames.size(); ++i) {
if (collName == fCollNames[i]) return fRunMaps[i];
}
G4Exception("TSRun", collName.c_str(), JustWarning,
@@ -281,13 +257,10 @@ G4THitsMap<G4double>* TSRun::GetHitsMap(const G4String& collName) const
// Access AtomicsHitsMap.
// by full description of collection name, that is
// <MultiFunctional Detector Name>/<Primitive Scorer Name>
G4TAtomicHitsMap<G4double>* TSRun::GetAtomicHitsMap(
const G4String& collName) const
G4TAtomicHitsMap<G4double>* TSRun::GetAtomicHitsMap(const G4String& collName) const
{
for(unsigned i = 0; i < fCollNames.size(); ++i)
{
if(collName == fCollNames[i])
return fAtomicRunMaps[i];
for (unsigned i = 0; i < fCollNames.size(); ++i) {
if (collName == fCollNames[i]) return fAtomicRunMaps[i];
}
G4Exception("TSRun", collName.c_str(), JustWarning,
@@ -302,8 +275,7 @@ G4TAtomicHitsMap<G4double>* TSRun::GetAtomicHitsMap(
// <MultiFunctional Detector Name>/<Primitive Scorer Name>
TSRun::MutexHitsMap_t* TSRun::GetMutexHitsMap(const G4String& collName) const
{
if(fMutexRunMaps.find(collName) != fMutexRunMaps.end())
return &fMutexRunMaps[collName];
if (fMutexRunMaps.find(collName) != fMutexRunMaps.end()) return &fMutexRunMaps[collName];
G4Exception("TSRun", collName.c_str(), JustWarning,
"GetHitsMap failed to locate the requested MutexHitsMap");
@@ -315,13 +287,10 @@ TSRun::MutexHitsMap_t* TSRun::GetMutexHitsMap(const G4String& collName) const
// Access StatMap.
// by full description of collection name, that is
// <MultiFunctional Detector Name>/<Primitive Scorer Name>
G4StatContainer<G4StatAnalysis>* TSRun::GetStatMap(
const G4String& collName) const
G4StatContainer<G4StatAnalysis>* TSRun::GetStatMap(const G4String& collName) const
{
for(unsigned i = 0; i < fCollNames.size(); ++i)
{
if(collName == fCollNames[i])
return fStatMaps[i];
for (unsigned i = 0; i < fCollNames.size(); ++i) {
if (collName == fCollNames[i]) return fStatMaps[i];
}
G4Exception("TSRun", collName.c_str(), JustWarning,
@@ -331,13 +300,10 @@ G4StatContainer<G4StatAnalysis>* TSRun::GetStatMap(
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4StatContainer<G4ConvergenceTester>* TSRun::GetConvMap(
const G4String& collName) const
G4StatContainer<G4ConvergenceTester>* TSRun::GetConvMap(const G4String& collName) const
{
for(unsigned i = 0; i < fCollNames.size(); ++i)
{
if(collName == fCollNames[i])
return fConvMaps[i];
for (unsigned i = 0; i < fCollNames.size(); ++i) {
if (collName == fCollNames[i]) return fConvMaps[i];
}
G4Exception("TSRun", collName.c_str(), JustWarning,
@@ -35,22 +35,21 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#include "TSRunAction.hh"
#include "TSActionInitialization.hh"
#include "G4RunManager.hh"
#include "G4Run.hh"
#include "G4Timer.hh"
#include "G4TaskRunManager.hh"
#include "TSRun.hh"
#include "TSDetectorConstruction.hh"
#include "TSRun.hh"
#include "G4Run.hh"
#include "G4RunManager.hh"
#include "G4StatAnalysis.hh"
#include "G4TaskRunManager.hh"
#include "G4Timer.hh"
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
TSRunAction::TSRunAction()
: fDetector(TSDetectorConstruction::Instance())
, fName(fDetector->GetMFDName())
: fDetector(TSDetectorConstruction::Instance()), fName(fDetector->GetMFDName())
{}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -59,7 +58,10 @@ TSRunAction::~TSRunAction() {}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
G4Run* TSRunAction::GenerateRun() { return new TSRun(fName); }
G4Run* TSRunAction::GenerateRun()
{
return new TSRun(fName);
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
@@ -68,16 +70,14 @@ void TSRunAction::BeginOfRunAction(const G4Run* aRun)
// G4int evts_to_process = aRun->GetNumberOfEventToBeProcessed();
// G4RunManager::GetRunManager()->SetPrintProgress(
// (evts_to_process > 1000) ? evts_to_process / 1000 : 1);
if(IsMaster() && aRun != nullptr)
G4PrintEnv();
if (IsMaster() && aRun != nullptr) G4PrintEnv();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
void TSRunAction::EndOfRunAction(const G4Run* aRun)
{
if(IsMaster())
{
if (IsMaster()) {
G4cout << " ###### EndOfTSRunAction ###### " << G4endl;
aRun->GetNumberOfEvent();
@@ -97,51 +97,45 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
//---------------------------------------------
// Dump accumulated quantities for this RUN.
//---------------------------------------------
std::vector<G4String> primScorerNames{ "EnergyDeposit", "NumberOfSteps" };
std::vector<G4String> fnames{ "mfd_tl", "mfd_tg" };
std::vector<G4double> units{ CLHEP::eV, CLHEP::keV, 1, 1 };
std::vector<G4String> unitstr{ "keV", "steps" };
std::vector<G4String> primScorerNames{"EnergyDeposit", "NumberOfSteps"};
std::vector<G4String> fnames{"mfd_tl", "mfd_tg"};
std::vector<G4double> units{CLHEP::eV, CLHEP::keV, 1, 1};
std::vector<G4String> unitstr{"keV", "steps"};
//----------------------------------------------------------------------//
// lambda to print double value
auto print = [](std::ostream& fout, G4int first, G4double second,
G4double unit1, G4double unit2, G4String unit2str) {
if(fout)
fout << first << " " << second / unit1 << G4endl;
auto print = [](std::ostream& fout, G4int first, G4double second, G4double unit1,
G4double unit2, G4String unit2str) {
if (fout) fout << first << " " << second / unit1 << G4endl;
G4cout << " " << std::setw(10) << first << " " << std::setw(15)
<< std::setprecision(6) << std::fixed << second / unit2 << " "
<< unit2str << G4endl;
G4cout << " " << std::setw(10) << first << " " << std::setw(15) << std::setprecision(6)
<< std::fixed << second / unit2 << " " << unit2str << G4endl;
G4cout.unsetf(std::ios::fixed);
};
//----------------------------------------------------------------------//
// lambda to print statistics
auto stat_print = [](std::ostream& fout, G4int first, G4StatAnalysis* stat,
G4ConvergenceTester* conv, G4double unit1,
G4double unit2, G4String unit2str) {
if(!stat || !conv)
return;
G4ConvergenceTester* conv, G4double unit1, G4double unit2,
G4String unit2str) {
if (!stat || !conv) return;
auto fsecond = (*stat);
auto psecond = (*stat);
fsecond /= unit1;
psecond /= unit2;
if(fout)
{
if (fout) {
fout << first << " " << fsecond << G4endl;
conv->ShowResult(fout);
}
std::stringstream ss;
ss << " " << std::setw(10) << first << " " << std::setw(15)
<< std::setprecision(6) << std::fixed << psecond << " " << unit2str;
ss << " " << std::setw(10) << first << " " << std::setw(15) << std::setprecision(6)
<< std::fixed << psecond << " " << unit2str;
// skip print of ConvergenceTester to stdout
G4cout << ss.str() << G4endl;
};
//----------------------------------------------------------------------//
for(unsigned i = 0; i < primScorerNames.size(); ++i)
{
for(unsigned j = 0; j < fnames.size(); ++j)
{
for (unsigned i = 0; i < primScorerNames.size(); ++i) {
for (unsigned j = 0; j < fnames.size(); ++j) {
fname = fnames.at(j) + "_" + primScorerNames.at(i) + ".out";
fileout.open(fname);
G4cout << separator.str() << G4endl;
@@ -149,97 +143,77 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
G4cout << separator.str() << G4endl;
G4bool valid = true;
if(j == 0)
{
G4THitsMap<G4double>* hitmap =
tsRun->GetHitsMap(fName + "/" + primScorerNames.at(i));
if (j == 0) {
G4THitsMap<G4double>* hitmap = tsRun->GetHitsMap(fName + "/" + primScorerNames.at(i));
G4StatContainer<G4StatAnalysis>* statmap =
tsRun->GetStatMap(fName + "/" + primScorerNames.at(i));
G4StatContainer<G4ConvergenceTester>* convmap =
tsRun->GetConvMap(fName + "/" + primScorerNames.at(i));
if(hitmap && hitmap->size() != 0)
{
for(auto itr = hitmap->begin(); itr != hitmap->end(); itr++)
{
if(!hitmap->GetObject(itr))
continue;
if (hitmap && hitmap->size() != 0) {
for (auto itr = hitmap->begin(); itr != hitmap->end(); itr++) {
if (!hitmap->GetObject(itr)) continue;
IDs.insert(itr->first);
std::get<0>(fTypeCompare[primScorerNames.at(i)][itr->first]) =
*itr->second / units.at(i);
print(fileout, itr->first, *itr->second, units.at(i),
units.at(i + 1), unitstr.at(i));
print(fileout, itr->first, *itr->second, units.at(i), units.at(i + 1), unitstr.at(i));
}
}
else
{
else {
valid = false;
}
if(statmap && statmap->size() != 0 && convmap && convmap->size() != 0)
{
if (statmap && statmap->size() != 0 && convmap && convmap->size() != 0) {
auto stat_fname = "stat_" + fname;
std::ofstream statout;
statout.open(stat_fname);
for(auto itr = statmap->begin(); itr != statmap->end(); itr++)
{
G4int _f = statmap->GetIndex(itr);
G4StatAnalysis* _s = statmap->GetObject(itr);
for (auto itr = statmap->begin(); itr != statmap->end(); itr++) {
G4int _f = statmap->GetIndex(itr);
G4StatAnalysis* _s = statmap->GetObject(itr);
G4ConvergenceTester* _c = convmap->GetObject(_f);
stat_print(statout, _f, _s, _c, units.at(i), units.at(i + 1),
unitstr.at(i));
stat_print(statout, _f, _s, _c, units.at(i), units.at(i + 1), unitstr.at(i));
}
statout.close();
}
else
{
else {
std::stringstream ss;
ss << " StatMap/ConvMap is either not "
<< "created or the StatMap/ConvMap was empty";
if(statmap)
ss << " (StatMap size == " << statmap->size() << ")";
if(convmap)
ss << " (ConvMap size == " << convmap->size() << ")";
if (statmap) ss << " (StatMap size == " << statmap->size() << ")";
if (convmap) ss << " (ConvMap size == " << convmap->size() << ")";
G4Exception("TSRunAction", "002", JustWarning,
G4String(primScorerNames.at(i) + ss.str()).c_str());
}
if(!valid)
{
if (!valid) {
G4Exception("TSRunAction", "000", JustWarning,
G4String(primScorerNames.at(i) +
" HitsMap is either not "
"created or the HitsMap was empty")
G4String(primScorerNames.at(i)
+ " HitsMap is either not "
"created or the HitsMap was empty")
.c_str());
}
}
else
{
else {
G4TAtomicHitsMap<G4double>* hitmap =
tsRun->GetAtomicHitsMap(fName + "/" + primScorerNames.at(i));
if(hitmap && hitmap->size() != 0)
{
for(auto itr = hitmap->begin(); itr != hitmap->end(); itr++)
{
if (hitmap && hitmap->size() != 0) {
for (auto itr = hitmap->begin(); itr != hitmap->end(); itr++) {
IDs.insert(itr->first);
std::get<1>(fTypeCompare[primScorerNames.at(i)][itr->first]) =
*itr->second / units.at(i);
print(fileout, itr->first, *itr->second, units.at(i),
units.at(i + 1), unitstr.at(i));
print(fileout, itr->first, *itr->second, units.at(i), units.at(i + 1), unitstr.at(i));
}
}
else
{
else {
valid = false;
}
if(!valid)
{
if (!valid) {
G4Exception("TSRunAction", "001", JustWarning,
G4String(primScorerNames.at(i) +
" HitsMap is either not "
"created or the HitsMap was empty")
G4String(primScorerNames.at(i)
+ " HitsMap is either not "
"created or the HitsMap was empty")
.c_str());
}
}
@@ -249,15 +223,11 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
G4cout << " closed file " << fname << " for output" << G4endl;
}
// add the mutex data
TSRun::MutexHitsMap_t* hitmap =
tsRun->GetMutexHitsMap(fName + "/" + primScorerNames.at(i));
if(hitmap && hitmap->size() != 0)
{
for(auto itr = hitmap->begin(); itr != hitmap->end(); itr++)
{
TSRun::MutexHitsMap_t* hitmap = tsRun->GetMutexHitsMap(fName + "/" + primScorerNames.at(i));
if (hitmap && hitmap->size() != 0) {
for (auto itr = hitmap->begin(); itr != hitmap->end(); itr++) {
IDs.insert(itr->first);
std::get<2>(fTypeCompare[primScorerNames.at(i)][itr->first]) =
itr->second / units.at(i);
std::get<2>(fTypeCompare[primScorerNames.at(i)][itr->first]) = itr->second / units.at(i);
}
}
}
@@ -275,17 +245,14 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
G4cout << separator.str() << G4endl;
fileout << " " << std::setw(10) << "ID"
<< " " << std::setw(30) << std::setprecision(12) << std::fixed
<< "MFD value"
<< " " << std::setw(30) << std::setprecision(12) << std::fixed << "MFD value"
<< " " << std::setw(30) << std::setprecision(12) << std::fixed
<< "Atomic Hits Map value"
<< " " << std::setw(30) << std::setprecision(8)
<< std::scientific << "Difference"
<< " " << std::setw(30) << std::setprecision(8)
<< std::scientific << "Diff (MFD - MUTEXED)"
<< " " << std::setw(30) << std::setprecision(8)
<< std::scientific << "Diff (ATOM_HIT_MAP - MUTEXED)" << G4endl
<< G4endl;
<< " " << std::setw(30) << std::setprecision(8) << std::scientific << "Difference"
<< " " << std::setw(30) << std::setprecision(8) << std::scientific
<< "Diff (MFD - MUTEXED)"
<< " " << std::setw(30) << std::setprecision(8) << std::scientific
<< "Diff (ATOM_HIT_MAP - MUTEXED)" << G4endl << G4endl;
//----------------------------------------------------------------------//
//
@@ -309,29 +276,26 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
};
// this is the outer-loop of tasks
auto report_type_comparison = [=](const G4String& id,
const IDcompare_t& comp) {
auto report_type_comparison = [=](const G4String& id, const IDcompare_t& comp) {
// the 'report_type_comparison' generates more tasks
auto report_subtype_comparison = [](const G4int& idx,
const Compare_t& value) {
auto report_subtype_comparison = [](const G4int& idx, const Compare_t& value) {
std::stringstream streamout;
G4double d01 = std::fabs(std::get<0>(value) - std::get<1>(value));
G4double d02 = std::fabs(std::get<0>(value) - std::get<2>(value));
G4double d03 = std::fabs(std::get<1>(value) - std::get<2>(value));
auto _print_diff = [&](const G4double& _dval) {
if(_dval > 0.0)
streamout << std::setprecision(8) << std::scientific
<< std::setw(30) << _dval << " ";
if (_dval > 0.0)
streamout << std::setprecision(8) << std::scientific << std::setw(30) << _dval
<< " ";
else
streamout << std::setprecision(1) << std::fixed << std::setw(30)
<< _dval << " ";
streamout << std::setprecision(1) << std::fixed << std::setw(30) << _dval << " ";
};
streamout << " " << std::setw(10) << idx << " " << std::setw(30)
<< std::setprecision(12) << std::fixed << std::get<0>(value)
<< " " << std::setw(30) << std::setprecision(12)
<< std::fixed << std::get<1>(value) << " ";
<< std::setprecision(12) << std::fixed << std::get<0>(value) << " "
<< std::setw(30) << std::setprecision(12) << std::fixed << std::get<1>(value)
<< " ";
_print_diff(d01);
_print_diff(d02);
@@ -343,23 +307,21 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
std::stringstream streamout;
streamout << "\n\nType = " << id << "\n" << G4endl;
if(tp)
{
if (tp) {
// create a task group (nested inside the 'report_type_comparison' task)
G4TaskGroup<std::string> tg(join_output, tp);
// create the tasks in the task-group
for(auto titr = comp.begin(); titr != comp.end(); ++titr)
for (auto titr = comp.begin(); titr != comp.end(); ++titr)
tg.exec(report_subtype_comparison, titr->first, titr->second);
// wait on the tasks to finish and execute the join function
// this will block the outer task from completing until all the inner
// tasks have been completed
streamout << tg.join();
}
else
{
else {
// if there isn't a tasking thread-pool then we make traditional
// function call on this thread
for(auto titr = comp.begin(); titr != comp.end(); ++titr)
for (auto titr = comp.begin(); titr != comp.end(); ++titr)
streamout << report_subtype_comparison(titr->first, titr->second);
}
// this is the completion of the outer tasks
@@ -367,12 +329,11 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
};
G4String tasking_result = "";
if(tp)
{
if (tp) {
G4cout << "\n\nGenerating diff output via tasking... ";
// create a task group to
G4TaskGroup<std::string> tg(join_output, tp);
for(auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
for (auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
tg.exec(report_type_comparison, itr->first, itr->second);
// wait on the tasks to finish and execute the join function
tasking_result = tg.join();
@@ -380,31 +341,27 @@ void TSRunAction::EndOfRunAction(const G4Run* aRun)
// if thread-pool was available, lets validate that tasking did what was
// expected
if(tp)
{
if (tp) {
// generate the output serially
G4String serial_result = "";
for(auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
for (auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
serial_result += report_type_comparison(itr->first, itr->second);
// write the tasking result even if it was bad so that it can viewed
fileout << tasking_result;
// compare the strings -- should be the same
if(serial_result != tasking_result)
{
if (serial_result != tasking_result) {
G4Exception("TSRunAction", "003", JustWarning,
"Output written via tasking did not match output written "
"serially. Appending serial result to output file");
fileout
<< "\n\n#================CORRECT_SERIAL_OUTPUT================#\n\n";
fileout << "\n\n#================CORRECT_SERIAL_OUTPUT================#\n\n";
fileout << serial_result;
}
}
else
{
else {
// if thread-pool was not available, then just write serially
for(auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
for (auto itr = fTypeCompare.begin(); itr != fTypeCompare.end(); ++itr)
fileout << report_type_comparison(itr->first, itr->second);
}
File diff suppressed because it is too large Load Diff
@@ -43,12 +43,9 @@
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
#include "G4Types.hh"
#include "G4RunManagerFactory.hh"
#include "G4Threading.hh"
#include "G4Types.hh"
#include "Randomize.hh"
// User Defined Classes
@@ -56,12 +53,11 @@
#include "TSDetectorConstruction.hh"
#include "TSPhysicsList.hh"
#include "G4TiMemory.hh"
#include "G4Step.hh"
#include "G4Track.hh"
#include "G4UIExecutive.hh"
#include "G4UImanager.hh"
#include "G4VisExecutive.hh"
#include "G4Track.hh"
#include "G4Step.hh"
// for std::system(const char*)
#include <cstdlib>
@@ -71,15 +67,13 @@
void message(G4RunManager* runmanager)
{
G4MTRunManager* man = dynamic_cast<G4MTRunManager*>(runmanager);
if(man)
{
if (man) {
man->SetNumberOfThreads(G4Threading::G4GetNumberOfCores());
G4cout << "\n\n\t--> Running in multithreaded mode with "
<< man->GetNumberOfThreads() << " threads\n\n"
G4cout << "\n\n\t--> Running in multithreaded mode with " << man->GetNumberOfThreads()
<< " threads\n\n"
<< G4endl;
}
else
{
else {
G4cout << "\n\n\t--> Running in serial mode\n\n" << G4endl;
}
}
@@ -88,70 +82,18 @@ void message(G4RunManager* runmanager)
int main(int argc, char** argv)
{
// initialize timemory
G4Profiler::Configure(argc, argv);
G4String macro;
if(argc > 1)
macro = argv[argc - 1];
if (argc > 1) macro = argv[argc - 1];
// Detect interactive mode (if no arguments) and define UI session
//
G4UIExecutive* ui = 0;
if(macro.empty())
ui = new G4UIExecutive(argc, argv);
if (macro.empty()) ui = new G4UIExecutive(argc, argv);
// Set the random seed
CLHEP::HepRandom::setTheSeed(1245214UL);
#if defined(GEANT4_USE_TIMEMORY)
// The following exists for:
// - G4ProfileType::Run
// - G4ProfileType::Event
// - G4ProfileType::Track
// - G4ProfileType::Step
// - G4ProfileType::User
//
using TrackProfilerConfig = G4ProfilerConfig<G4ProfileType::Track>;
using TrackTool = typename TrackProfilerConfig::type;
TrackProfilerConfig::GetQueryFunctor() = [](const G4Track* _track) {
// only profile if _track != nullptr and dynamic-profiler != nullptr
// and /profiler/track/enable is true
//
return G4Profiler::GetEnabled(G4ProfileType::Track) && _track &&
_track->GetDynamicParticle();
};
TrackProfilerConfig::GetLabelFunctor() = [](const G4Track* _track) {
// create a label for the profiling entry. This can be customized
// to include and information necessary in the returning string
auto pdef = _track->GetDynamicParticle()->GetParticleDefinition();
static std::string _prefix = "G4Track/";
return _prefix + pdef->GetParticleName();
};
// env option to display track profiles as a hierarchy
bool track_tree = tim::get_env<bool>("G4PROFILER_TRACK_TREE", true);
// env option to enable timeline entries (every entry is unique, HUGE amount
// of data!)
bool track_time = tim::get_env<bool>("G4PROFILER_TRACK_TIMELINE", false);
// default scope is tree
auto _scope = tim::scope::config{};
if(track_tree == false)
_scope += tim::scope::flat{};
if(track_time == true)
_scope += tim::scope::timeline{};
TrackProfilerConfig::GetToolFunctor() = [=](const std::string& _label) {
// Configure the profiling tool for a given label. By default,
// G4Track and G4Step tools are "flat profiles" but this can be disabled
// to include tree
return new TrackTool(_label, _scope);
};
#endif
G4RunManager* runmanager =
G4RunManagerFactory::CreateRunManager(G4RunManagerType::Tasking);
G4RunManager* runmanager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Tasking);
message(runmanager);
@@ -175,14 +117,12 @@ int main(int argc, char** argv)
// Process macro or start UI session
//
if(!ui)
{
if (!ui) {
// batch mode
G4String command = "/control/execute ";
UImanager->ApplyCommand(command + macro);
}
else
{
else {
ui->SessionStart();
}