GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSolidBox.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 
39 GGEMSSolidBox::GGEMSSolidBox(GGsize const& virtual_element_number_x, GGsize const& virtual_element_number_y, GGsize const& virtual_element_number_z, GGfloat const& box_size_x, GGfloat const& box_size_y, GGfloat const& box_size_z, std::string const& data_reg_type)
40 : GGEMSSolid()
41 {
42  GGcout("GGEMSSolidBox", "GGEMSSolidBox", 3) << "GGEMSSolidBox creating..." << GGendl;
43 
45 
46  // Loop over the device
47  for (GGsize d = 0; d < number_activated_devices_; ++d) {
48  // Allocating memory on OpenCL device and getting pointer on it
49  solid_data_[d] = opencl_manager.Allocate(nullptr, sizeof(GGEMSSolidBoxData), d, CL_MEM_READ_WRITE, "GGEMSSolidBox");
50  GGEMSSolidBoxData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSSolidBoxData>(solid_data_[d], sizeof(GGEMSSolidBoxData), d);
51 
52  solid_data_device->virtual_element_number_xyz_[0] = virtual_element_number_x;
53  solid_data_device->virtual_element_number_xyz_[1] = virtual_element_number_y;
54  solid_data_device->virtual_element_number_xyz_[2] = virtual_element_number_z;
55 
56  solid_data_device->box_size_xyz_[0] = box_size_x;
57  solid_data_device->box_size_xyz_[1] = box_size_y;
58  solid_data_device->box_size_xyz_[2] = box_size_z;
59 
60  solid_data_device->obb_geometry_.border_min_xyz_.x = -box_size_x*0.5f;
61  solid_data_device->obb_geometry_.border_min_xyz_.y = -box_size_y*0.5f;
62  solid_data_device->obb_geometry_.border_min_xyz_.z = -box_size_z*0.5f;
63 
64  solid_data_device->obb_geometry_.border_max_xyz_.x = box_size_x*0.5f;
65  solid_data_device->obb_geometry_.border_max_xyz_.y = box_size_y*0.5f;
66  solid_data_device->obb_geometry_.border_max_xyz_.z = box_size_z*0.5f;
67 
68  // Releasing pointer
69  opencl_manager.ReleaseDeviceBuffer(solid_data_[d], solid_data_device, d);
70  }
71 
72  // Local axis definition for system
74  {
75  {0.0f, 0.0f, 1.0f},
76  {0.0f, 1.0f, 0.0f},
77  {-1.0f, 0.0f, 0.0f}
78  }
79  );
80 
81  // Solid box associated at hit collection
82  data_reg_type_ = data_reg_type;
83  if (data_reg_type == "HISTOGRAM") {
84  histogram_.number_of_elements_ = virtual_element_number_x*virtual_element_number_y*virtual_element_number_z;
85 
86  // Allocating memory storing data
89 
90  // Loop over number of device
91  for (GGsize d = 0; d < number_activated_devices_; ++d) {
92  histogram_.histogram_[d] = opencl_manager.Allocate(nullptr, histogram_.number_of_elements_*sizeof(GGint), d, CL_MEM_READ_WRITE, "GGEMSSolidBox");
93  histogram_.scatter_[d] = nullptr;
94 
95  if (d == 0) kernel_option_ += " -DHISTOGRAM";
96 
97  // Initialize value to 0
99  }
100  }
101  else {
102  std::ostringstream oss(std::ostringstream::out);
103  oss << "False registration type name!!!" << std::endl;
104  oss << "Registration type is :" << std::endl;
105  oss << " - HISTOGRAM" << std::endl;
106  //oss << " - LISTMODE" << std::endl;
107  //oss << " - DOSIMETRY" << std::endl;
108  GGEMSMisc::ThrowException("GGEMSSolidBox", "GGEMSSolidBox", oss.str());
109  }
110 
111  GGcout("GGEMSSolidBox", "GGEMSSolidBox", 3) << "GGEMSSolidBox created!!!" << GGendl;
112 }
113 
117 
119 {
120  GGcout("GGEMSSolidBox", "~GGEMSSolidBox", 3) << "GGEMSSolidBox erasing..." << GGendl;
121 
122  // Get the opencl manager
124 
125  if (data_reg_type_ == "HISTOGRAM") {
126  if (histogram_.histogram_) {
127  for (GGsize i = 0; i < number_activated_devices_; ++i) {
128  opencl_manager.Deallocate(histogram_.histogram_[i], histogram_.number_of_elements_*sizeof(GGint), i);
129  }
130  delete[] histogram_.histogram_;
131  histogram_.histogram_ = nullptr;
132  }
133 
134  if (is_scatter_) {
135  if (histogram_.scatter_) {
136  for (GGsize i = 0; i < number_activated_devices_; ++i) {
137  opencl_manager.Deallocate(histogram_.scatter_[i], histogram_.number_of_elements_*sizeof(GGint), i);
138  }
139  delete[] histogram_.scatter_;
140  histogram_.scatter_ = nullptr;
141  }
142  }
143  }
144 
145  GGcout("GGEMSSolidBox", "~GGEMSSolidBox", 3) << "GGEMSSolidBox erased!!!" << GGendl;
146 }
147 
151 
153 {
154  GGcout("GGEMSSolidBox", "InitializeKernel", 3) << "Initializing kernel for solid box..." << GGendl;
155 
156  // Getting the OpenCLManager singleton
158 
159  // Getting the path to kernel
160  std::string openCL_kernel_path = OPENCL_KERNEL_PATH;
161  std::string particle_solid_distance_filename = openCL_kernel_path + "/ParticleSolidDistanceGGEMSSolidBox.cl";
162  std::string project_to_filename = openCL_kernel_path + "/ProjectToGGEMSSolidBox.cl";
163  std::string track_through_filename = openCL_kernel_path + "/TrackThroughGGEMSSolidBox.cl";
164 
165  // Compiling the kernels
166  opencl_manager.CompileKernel(particle_solid_distance_filename, "particle_solid_distance_ggems_solid_box", kernel_particle_solid_distance_, nullptr, const_cast<char*>(kernel_option_.c_str()));
167  opencl_manager.CompileKernel(project_to_filename, "project_to_ggems_solid_box", kernel_project_to_solid_, nullptr, const_cast<char*>(kernel_option_.c_str()));
168  opencl_manager.CompileKernel(track_through_filename, "track_through_ggems_solid_box", kernel_track_through_solid_, nullptr, const_cast<char*>(kernel_option_.c_str()));
169 }
170 
174 
176 {
177  GGcout("GGEMSSolidBox", "Initialize", 3) << "Initializing voxelized solid..." << GGendl;
178 
179  // Initializing kernels
181 }
182 
186 
188 {
189  // Getting the OpenCLManager singleton
191 
192  is_scatter_ = true;
193 
194  // Loop over number of device
195  for (GGsize d = 0; d < number_activated_devices_; ++d) {
196  histogram_.scatter_[d] = opencl_manager.Allocate(nullptr, histogram_.number_of_elements_*sizeof(GGint), d, CL_MEM_READ_WRITE, "GGEMSSolidBox");
197 
198  // Initialize value to 0
199  opencl_manager.CleanBuffer(histogram_.scatter_[d], histogram_.number_of_elements_*sizeof(GGint), d);
200  }
201 }
202 
206 
208 {
210 
211  // Loop over the device
212  for (GGsize d = 0; d < number_activated_devices_; ++d) {
213  // Getting pointer on OpenCL device
214  GGEMSSolidBoxData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSSolidBoxData>(solid_data_[d], sizeof(GGEMSSolidBoxData), d);
215 
216  // Get the index of device
217  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(d);
218 
219  GGcout("GGEMSSolidBox", "PrintInfos", 0) << GGendl;
220  GGcout("GGEMSSolidBox", "PrintInfos", 0) << "GGEMSSolidBox Infos:" << GGendl;
221  GGcout("GGEMSSolidBox", "PrintInfos", 0) << "--------------------------" << GGendl;
222  GGcout("GGEMSMaterials", "PrintInfos", 0) << "Material on device: " << opencl_manager.GetDeviceName(device_index) << GGendl;
223  GGcout("GGEMSSolidBox", "PrintInfos", 0) << "* Virtual elements: " << solid_data_device->virtual_element_number_xyz_[0] << "x" << solid_data_device->virtual_element_number_xyz_[1] << "x" << solid_data_device->virtual_element_number_xyz_[2] << GGendl;
224  GGcout("GGEMSSolidBox", "PrintInfos", 0) << "* Lengths: (" << solid_data_device->box_size_xyz_[0] << "x" << solid_data_device->box_size_xyz_[1] << "x" << solid_data_device->box_size_xyz_[2] << ") mm3" << GGendl;
225  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << "* Oriented bounding box (OBB) in local position:" << GGendl;
226  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - X: " << solid_data_device->obb_geometry_.border_min_xyz_.x << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.x << GGendl;
227  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - Y: " << solid_data_device->obb_geometry_.border_min_xyz_.y << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.y << GGendl;
228  GGcout("GGEMSVoxelizedSolid", "PrintInfos", 0) << " - Z: " << solid_data_device->obb_geometry_.border_min_xyz_.z << " <-> " << solid_data_device->obb_geometry_.border_max_xyz_.z << GGendl;
229  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " - Transformation matrix:" << GGendl;
230  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " [" << GGendl;
231  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m0_[3] << GGendl;
232  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m1_[3] << GGendl;
233  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m2_[3] << GGendl;
234  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[0] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[1] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[2] << " " << solid_data_device->obb_geometry_.matrix_transformation_.m3_[3] << GGendl;
235  GGcout("GGEMSSolidBox", "PrintInfos", 0) << " ]" << GGendl;
236  GGcout("GGEMSSolidBox", "PrintInfos", 0) << "* Solid index: " << solid_data_device->solid_id_ << GGendl;
237  GGcout("GGEMSSolidBox", "PrintInfos", 0) << GGendl;
238 
239  // Releasing the pointer
240  opencl_manager.ReleaseDeviceBuffer(solid_data_[d], solid_data_device, d);
241  }
242 }
243 
247 
249 {
250  // Get the OpenCL manager
252 
253  // Copy information to OBB
254  GGEMSSolidBoxData* solid_data_device = opencl_manager.GetDeviceBuffer<GGEMSSolidBoxData>(solid_data_[thread_index], sizeof(GGEMSSolidBoxData), thread_index);
255  GGfloat44* transformation_matrix_device = opencl_manager.GetDeviceBuffer<GGfloat44>(geometry_transformation_->GetTransformationMatrix(thread_index), sizeof(GGfloat44), thread_index);
256 
257  for (GGint i = 0; i < 4; ++i) {
258  solid_data_device->obb_geometry_.matrix_transformation_.m0_[i] = transformation_matrix_device->m0_[i];
259  solid_data_device->obb_geometry_.matrix_transformation_.m1_[i] = transformation_matrix_device->m1_[i];
260  solid_data_device->obb_geometry_.matrix_transformation_.m2_[i] = transformation_matrix_device->m2_[i];
261  solid_data_device->obb_geometry_.matrix_transformation_.m3_[i] = transformation_matrix_device->m3_[i];
262  }
263 
264  // Release the pointer
265  opencl_manager.ReleaseDeviceBuffer(solid_data_[thread_index], solid_data_device, thread_index);
266  opencl_manager.ReleaseDeviceBuffer(geometry_transformation_->GetTransformationMatrix(thread_index), transformation_matrix_device, thread_index);
267 }
GGfloat44
struct GGfloat44_t GGfloat44
GGfloat44_t::m2_
GGfloat m2_[4]
Definition: GGEMSMatrixTypes.hh:56
GGEMSSolid::solid_data_
cl::Buffer ** solid_data_
Definition: GGEMSSolid.hh:215
GGEMSMaterials
GGEMS class handling material(s) for a specific navigator.
Definition: GGEMSMaterials.hh:49
GGfloat44_t::m0_
GGfloat m0_[4]
Definition: GGEMSMatrixTypes.hh:54
GGEMSSolid
GGEMS class for solid informations.
Definition: GGEMSSolid.hh:48
GGEMSSolidBox::Initialize
void Initialize(GGEMSMaterials *materials) override
Initialize solid for geometric navigation.
Definition: GGEMSSolidBox.cc:175
GGEMSSolid::kernel_project_to_solid_
cl::Kernel ** kernel_project_to_solid_
Definition: GGEMSSolid.hh:224
GGEMSSolidBox.hh
GGEMS class for solid box.
GGEMSSolidBoxData_t::box_size_xyz_
GGfloat box_size_xyz_[3]
Definition: GGEMSSolidBoxData.hh:44
GGEMSOpenCLManager::Deallocate
void Deallocate(cl::Buffer *buffer, GGsize size, GGsize const &thread_index, std::string const &class_name="Undefined")
Deallocation of OpenCL memory.
Definition: GGEMSOpenCLManager.cc:981
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
GGfloat44_t::m1_
GGfloat m1_[4]
Definition: GGEMSMatrixTypes.hh:55
GGEMSHistogramMode_t::scatter_
cl::Buffer ** scatter_
Definition: GGEMSHistogramMode.hh:45
GGEMSSolidBoxData_t::obb_geometry_
GGEMSOBB obb_geometry_
Definition: GGEMSSolidBoxData.hh:42
GGEMSSolidBox::InitializeKernel
void InitializeKernel(void) override
Initialize kernel for particle solid distance.
Definition: GGEMSSolidBox.cc:152
GGEMSSolid::kernel_track_through_solid_
cl::Kernel ** kernel_track_through_solid_
Definition: GGEMSSolid.hh:225
GGEMSSolidBox::~GGEMSSolidBox
~GGEMSSolidBox(void)
GGEMSSolidBox destructor.
Definition: GGEMSSolidBox.cc:118
GGEMSSolidBox::EnableScatter
void EnableScatter(void) override
Activate scatter registration.
Definition: GGEMSSolidBox.cc:187
GGEMSOBB_t::matrix_transformation_
GGfloat44 matrix_transformation_
Definition: GGEMSPrimitiveGeometries.hh:42
GGEMSHistogramMode_t::histogram_
cl::Buffer ** histogram_
Definition: GGEMSHistogramMode.hh:44
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSGeometryTransformation::SetAxisTransformation
void SetAxisTransformation(GGfloat33 const &axis)
Set the transformation of the frame, usefull for mirroring or convert 3D to 2D.
Definition: GGEMSGeometryTransformation.hh:126
GGEMSSolid::geometry_transformation_
GGEMSGeometryTransformation * geometry_transformation_
Definition: GGEMSSolid.hh:220
GGEMSSolidBox::GGEMSSolidBox
GGEMSSolidBox(GGsize const &virtual_element_number_x, GGsize const &virtual_element_number_y, GGsize const &virtual_element_number_z, GGfloat const &element_size_x, GGfloat const &element_size_y, GGfloat const &element_size_z, std::string const &data_reg_type)
GGEMSSolidBox constructor.
Definition: GGEMSSolidBox.cc:39
GGEMSSolid::kernel_particle_solid_distance_
cl::Kernel ** kernel_particle_solid_distance_
Definition: GGEMSSolid.hh:223
GGEMSOpenCLManager::CleanBuffer
void CleanBuffer(cl::Buffer *buffer, GGsize const &size, GGsize const &thread_index)
Cleaning buffer on OpenCL device.
Definition: GGEMSOpenCLManager.cc:998
GGEMSSolidBoxData
struct GGEMSSolidBoxData_t GGEMSSolidBoxData
GGEMSSolidBoxData_t
Structure storing the stack of data for solid box.
Definition: GGEMSSolidBoxData.hh:41
GGEMSSolid::histogram_
GGEMSHistogramMode histogram_
Definition: GGEMSSolid.hh:230
GGEMSOpenCLManager::Allocate
cl::Buffer * Allocate(void *host_ptr, GGsize const &size, GGsize const &thread_index, cl_mem_flags flags, std::string const &class_name="Undefined")
Allocation of OpenCL memory.
Definition: GGEMSOpenCLManager.cc:945
GGEMSSolidBoxData_t::virtual_element_number_xyz_
GGsize virtual_element_number_xyz_[3]
Definition: GGEMSSolidBoxData.hh:43
GGEMSOpenCLManager::CompileKernel
void CompileKernel(std::string const &kernel_filename, std::string const &kernel_name, cl::Kernel **kernel_list, char *const custom_options=nullptr, char *const additional_options=nullptr)
Compile the OpenCL kernel on the activated device.
Definition: GGEMSOpenCLManager.cc:849
GGEMSHistogramMode_t::number_of_elements_
GGsize number_of_elements_
Definition: GGEMSHistogramMode.hh:46
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSOBB_t::border_max_xyz_
GGfloat3 border_max_xyz_
Definition: GGEMSPrimitiveGeometries.hh:44
GGEMSSolid::number_activated_devices_
GGsize number_activated_devices_
Definition: GGEMSSolid.hh:217
GGEMSSolid::is_scatter_
bool is_scatter_
Definition: GGEMSSolid.hh:231
GGEMSGeometryTransformation::GetTransformationMatrix
cl::Buffer * GetTransformationMatrix(GGsize const &index) const
return the transformation matrix
Definition: GGEMSGeometryTransformation.hh:178
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
GGEMSSolidBoxData.hh
Structure storing the data for solid box.
GGEMSSolidBox::UpdateTransformationMatrix
void UpdateTransformationMatrix(GGsize const &thread_index) override
Update transformation matrix for solid box object.
Definition: GGEMSSolidBox.cc:248
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
GGEMSSolidBoxData_t::solid_id_
GGint solid_id_
Definition: GGEMSSolidBoxData.hh:45
GGEMSSolidBox::PrintInfos
void PrintInfos(void) const override
printing infos about voxelized solid
Definition: GGEMSSolidBox.cc:207
GGEMSOpenCLManager::GetDeviceName
std::string GetDeviceName(GGsize const &device_index) const
Get the name of the activated device.
Definition: GGEMSOpenCLManager.hh:145
GGEMSSolid::data_reg_type_
std::string data_reg_type_
Definition: GGEMSSolid.hh:229
GGEMSGeometryTransformation.hh
Class managing the geometry transformation.
GGEMSOBB_t::border_min_xyz_
GGfloat3 border_min_xyz_
Definition: GGEMSPrimitiveGeometries.hh:43
GGEMSOpenCLManager::GetIndexOfActivatedDevice
GGsize GetIndexOfActivatedDevice(GGsize const &thread_index) const
get the index of activated device
Definition: GGEMSOpenCLManager.hh:175
GGfloat44_t::m3_
GGfloat m3_[4]
Definition: GGEMSMatrixTypes.hh:57
GGEMSSolid::kernel_option_
std::string kernel_option_
Definition: GGEMSSolid.hh:226
GGfloat44_t
Structure storing float 4 x 4 matrix.
Definition: GGEMSMatrixTypes.hh:53
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
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72