GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSourceManager.cc
Go to the documentation of this file.
1 // ************************************************************************
2 // * This file is part of GGEMS. *
3 // * *
4 // * GGEMS is free software: you can redistribute it and/or modify *
5 // * it under the terms of the GNU General Public License as published by *
6 // * the Free Software Foundation, either version 3 of the License, or *
7 // * (at your option) any later version. *
8 // * *
9 // * GGEMS is distributed in the hope that it will be useful, *
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 // * GNU General Public License for more details. *
13 // * *
14 // * You should have received a copy of the GNU General Public License *
15 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
16 // * *
17 // ************************************************************************
18 
34 
38 
40 : sources_(nullptr),
41  number_of_sources_(0)
42 {
43  GGcout("GGEMSSourceManager", "GGEMSSourceManager", 3) << "GGEMSSourceManager creating..." << GGendl;
44 
45  // Allocation of Particle object
47 
48  // Allocation of pseudo random generator object
50 
51  GGcout("GGEMSSourceManager", "GGEMSSourceManager", 3) << "GGEMSSourceManager created!!!" << GGendl;
52 }
53 
57 
59 {
60  GGcout("GGEMSSourceManager", "~GGEMSSourceManager", 3) << "GGEMSSourceManager erasing..." << GGendl;
61 
62  GGcout("GGEMSSourceManager", "~GGEMSSourceManager", 3) << "GGEMSSourceManager erased!!!" << GGendl;
63 }
64 
68 
70 {
71  GGcout("GGEMSSourceManager", "Clean", 3) << "GGEMSSourceManager cleaning..." << GGendl;
72 
73  if (particles_) {
74  delete particles_;
75  particles_ = nullptr;
76  }
77 
80  pseudo_random_generator_ = nullptr;
81  }
82 
83  GGcout("GGEMSSourceManager", "Clean", 3) << "GGEMSSourceManager cleaned!!!" << GGendl;
84 }
85 
89 
91 {
92  GGcout("GGEMSSourceManager", "Store", 3) << "Storing new source in GGEMS source manager..." << GGendl;
93 
94  if (number_of_sources_ == 0) {
95  sources_ = new GGEMSSource*[1];
96  sources_[0] = source;
97  }
98  else {
100  for (GGsize i = 0; i < number_of_sources_; ++i) {
101  tmp[i] = sources_[i];
102  }
103 
104  tmp[number_of_sources_] = source;
105 
106  delete[] sources_;
107  sources_ = tmp;
108  }
109 
111 }
112 
116 
118 {
119  GGcout("GGEMSSourceManager", "PrintInfos", 0) << "Printing infos about sources" << GGendl;
120  GGcout("GGEMSSourceManager", "PrintInfos", 0) << "Number of source(s): " << number_of_sources_ << GGendl;
121 
122  // Printing infos about each source
123  for (GGsize i = 0; i < number_of_sources_; ++i ) sources_[i]->PrintInfos();
124 }
125 
129 
130 void GGEMSSourceManager::Initialize(GGuint const& seed, bool const& is_tracking, GGint const& particle_tracking_id) const
131 {
132  GGcout("GGEMSSourceManager", "Initialize", 3) << "Initializing the GGEMS source(s)..." << GGendl;
133 
134  // Checking number of source, if 0 kill the simulation
135  if (number_of_sources_ == 0) {
136  GGEMSMisc::ThrowException("GGEMSSourceManager", "Initialize", "You have to define a source before to run GGEMS!!!");
137  }
138 
139  // Initialization of particle stack and random stack
141  GGcout("GGEMSSourceManager", "Initialize", 0) << "Initialization of particles OK" << GGendl;
142 
144  GGcout("GGEMSSourceManager", "Initialize", 0) << "Initialization of GGEMS pseudo random generator OK" << GGendl;
145 
146  // Initialization of sources
147  for (GGsize i = 0; i < number_of_sources_; ++i) sources_[i]->Initialize(is_tracking);
148 
149  // If tracking activated, set the particle id to track
150  if (is_tracking) {
151  // Get the OpenCL manager
153 
154  // Loop over activated device
155  for (GGsize i = 0; i < opencl_manager.GetNumberOfActivatedDevice(); ++i) {
156  // Get pointer on OpenCL device for particles
157  GGEMSPrimaryParticles* primary_particles_device = opencl_manager.GetDeviceBuffer<GGEMSPrimaryParticles>(particles_->GetPrimaryParticles(i), sizeof(GGEMSPrimaryParticles), i);
158 
159  primary_particles_device->particle_tracking_id = particle_tracking_id;
160 
161  // Release the pointer
162  opencl_manager.ReleaseDeviceBuffer(particles_->GetPrimaryParticles(i), primary_particles_device, i);
163  }
164  }
165 }
166 
170 
171 bool GGEMSSourceManager::IsAlive(GGsize const& thread_index) const
172 {
173  // Check if all particles are DEAD in OpenCL particle buffer
174  return particles_->IsAlive(thread_index);
175 }
176 
180 
182 {
184 }
185 
189 
190 void initialize_source_manager(GGEMSSourceManager* source_manager, GGuint const seed)
191 {
192  source_manager->Initialize(seed);
193 }
194 
198 
200 {
201  source_manager->PrintInfos();
202 }
GGEMSSourceManager::GetInstance
static GGEMSSourceManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSSourceManager.hh:67
GGEMSParticles
Class managing the particles in GGEMS.
Definition: GGEMSParticles.hh:48
GGEMSParticles::IsAlive
bool IsAlive(GGsize const &thread_index) const
check if some particles are alive in OpenCL particle buffer
Definition: GGEMSParticles.hh:116
GGEMSSourceManager::sources_
GGEMSSource ** sources_
Definition: GGEMSSourceManager.hh:199
GGEMSPseudoRandomGenerator
Class managing the random number in GGEMS.
Definition: GGEMSPseudoRandomGenerator.hh:42
GGEMSSourceManager::Store
void Store(GGEMSSource *source)
storing the source pointer to source manager
Definition: GGEMSSourceManager.cc:90
GGEMSSourceManager::Clean
void Clean(void)
clean OpenCL data
Definition: GGEMSSourceManager.cc:69
GGEMSOpenCLManager::GetNumberOfActivatedDevice
GGsize GetNumberOfActivatedDevice(void) const
get the number of activated devices
Definition: GGEMSOpenCLManager.hh:167
GGEMSSourceManager::number_of_sources_
GGsize number_of_sources_
Definition: GGEMSSourceManager.hh:200
GGEMSParticles::Initialize
void Initialize(void)
Initialize the GGEMSParticles object.
Definition: GGEMSParticles.cc:120
GGEMSPrimaryParticles_t
Structure storing informations about primary particles.
Definition: GGEMSPrimaryParticles.hh:42
GGEMSOpenCLManager::ReleaseDeviceBuffer
void ReleaseDeviceBuffer(cl::Buffer *const device_ptr, T *host_ptr, GGsize const &thread_index)
Get the device pointer on host to write on it. Mandatory after a GetDeviceBufferWrite ou GetDeviceBuf...
Definition: GGEMSOpenCLManager.hh:495
GGEMSSourceManager::Initialize
void Initialize(GGuint const &seed, bool const &is_tracking=false, GGint const &particle_tracking_id=0) const
Initialize a GGEMS source.
Definition: GGEMSSourceManager.cc:130
GGEMSSourceManager::pseudo_random_generator_
GGEMSPseudoRandomGenerator * pseudo_random_generator_
Definition: GGEMSSourceManager.hh:202
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSPrimaryParticles.hh
Structure storing the primary particle buffers for both OpenCL and GGEMS.
GGEMSSourceManager::PrintInfos
void PrintInfos(void) const
Printing infos about the sources.
Definition: GGEMSSourceManager.cc:117
GGEMSSourceManager::GGEMSSourceManager
GGEMSSourceManager(void)
Unable the constructor for the user.
Definition: GGEMSSourceManager.cc:39
GGEMSPrimaryParticles
struct GGEMSPrimaryParticles_t GGEMSPrimaryParticles
initialize_source_manager
void initialize_source_manager(GGEMSSourceManager *source_manager, GGuint const seed)
Initialize source.
Definition: GGEMSSourceManager.cc:190
GGEMSSourceManager::IsAlive
bool IsAlive(GGsize const &thread_index) const
check if some particles are alive in OpenCL particle buffer
Definition: GGEMSSourceManager.cc:171
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSPrimaryParticles_t::particle_tracking_id
GGint particle_tracking_id
Definition: GGEMSPrimaryParticles.hh:43
GGEMSPseudoRandomGenerator::Initialize
void Initialize(GGuint const &seed)
Initialize the Random object.
Definition: GGEMSPseudoRandomGenerator.cc:123
GGEMSSourceManager.hh
GGEMS class handling the source(s)
GGEMSSourceManager::particles_
GGEMSParticles * particles_
Definition: GGEMSSourceManager.hh:201
GGEMSOpenCLManager::GetDeviceBuffer
T * GetDeviceBuffer(cl::Buffer *device_ptr, GGsize const &size, GGsize const &thread_index)
Get the device pointer on host to write on it. ReleaseDeviceBuffer must be used after this method!...
Definition: GGEMSOpenCLManager.hh:480
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSSourceManager::~GGEMSSourceManager
~GGEMSSourceManager(void)
Unable the destructor for the user.
Definition: GGEMSSourceManager.cc:58
get_instance_ggems_source_manager
GGEMSSourceManager * get_instance_ggems_source_manager(void)
Get the GGEMSSourceManager pointer for python user.
Definition: GGEMSSourceManager.cc:181
GGEMSSourceManager
GGEMS class handling the source(s)
Definition: GGEMSSourceManager.hh:49
GGEMSPseudoRandomGenerator.hh
Class managing the random number in GGEMS.
GGuint
#define GGuint
Definition: GGEMSTypes.hh:231
print_infos_source_manager
void print_infos_source_manager(GGEMSSourceManager *source_manager)
Print information about source.
Definition: GGEMSSourceManager.cc:199
GGEMSMisc::ThrowException
void ThrowException(std::string const &class_name, std::string const &method_name, std::string const &message)
Throw a C++ exception.
Definition: GGEMSTools.cc:61
GGEMSSource
GGEMS mother class for the source.
Definition: GGEMSSource.hh:45
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72
GGEMSParticles::GetPrimaryParticles
cl::Buffer * GetPrimaryParticles(GGsize const &thread_index) const
return the pointer to OpenCL buffer storing particles
Definition: GGEMSParticles.hh:100