GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSBox.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 
35 
39 
40 GGEMSBox::GGEMSBox(GGfloat const& width, GGfloat const& height, GGfloat const& depth, std::string const& unit)
41 : GGEMSVolume()
42 {
43  GGcout("GGEMSBox", "GGEMSBox", 3) << "GGEMSBox creating..." << GGendl;
44 
45  width_ = DistanceUnit(width, unit);
46  height_ = DistanceUnit(height, unit);
47  depth_ = DistanceUnit(depth, unit);
48 
49  GGcout("GGEMSBox", "GGEMSBox", 3) << "GGEMSBox created!!!" << GGendl;
50 }
51 
55 
57 {
58  GGcout("GGEMSBox", "~GGEMSBox", 3) << "GGEMSBox erasing..." << GGendl;
59 
60  GGcout("GGEMSBox", "~GGEMSBox", 3) << "GGEMSBox erased!!!" << GGendl;
61 }
62 
66 
68 {
69  GGcout("GGEMSBox", "Initialize", 3) << "Initializing GGEMSBox solid volume..." << GGendl;
70 
71  // Getting the path to kernel
72  std::string const kOpenCLKernelPath = OPENCL_KERNEL_PATH;
73  std::string const kFilename = kOpenCLKernelPath + "/DrawGGEMSBox.cl";
74 
75  // Get the volume creator manager
77 
78  // Get the OpenCL manager
80 
81  // Get the data type and compiling kernel
82  std::string const kDataType = "-D" + volume_creator_manager.GetDataType();
83  opencl_manager.CompileKernel(kFilename, "draw_ggems_box", kernel_draw_volume_, nullptr, const_cast<char*>(kDataType.c_str()));
84 }
85 
89 
90 void GGEMSBox::Draw(void)
91 {
92  GGcout("GGEMSBox", "Draw", 3) << "Drawing Box..." << GGendl;
93 
94  // Get the OpenCL manager
96 
97  // Get the volume creator manager
99 
100  // Get command queue and event
101  cl::CommandQueue* queue = opencl_manager.GetCommandQueue(0);
102  cl::Event* event = opencl_manager.GetEvent(0);
103 
104  // Get Device name and storing methode name + device
105  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(0);
106  std::string device_name = opencl_manager.GetDeviceName(device_index);
107  std::ostringstream oss(std::ostringstream::out);
108  oss << "GGEMSBox::Draw on " << device_name << ", index " << device_index;
109 
110  // Get parameters from phantom creator
111  GGfloat3 voxel_sizes = volume_creator_manager.GetElementsSizes();
112 
113  GGint3 phantom_dimensions;
114  phantom_dimensions.x = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().x_);
115  phantom_dimensions.y = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().y_);
116  phantom_dimensions.z = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().z_);
117 
118  GGsize number_of_elements = volume_creator_manager.GetNumberElements();
119  cl::Buffer* voxelized_phantom = volume_creator_manager.GetVoxelizedVolume();
120 
121  // Getting work group size, and work-item number
122  GGsize work_group_size = opencl_manager.GetWorkGroupSize();
123  GGsize number_of_work_items = opencl_manager.GetBestWorkItem(number_of_elements);
124 
125  // Parameters for work-item in kernel
126  cl::NDRange global_wi(number_of_work_items);
127  cl::NDRange local_wi(work_group_size);
128 
129  // Set parameters for kernel
130  kernel_draw_volume_[0]->setArg(0, number_of_elements);
131  kernel_draw_volume_[0]->setArg(1, voxel_sizes);
132  kernel_draw_volume_[0]->setArg(2, phantom_dimensions);
133  kernel_draw_volume_[0]->setArg(3, positions_);
134  kernel_draw_volume_[0]->setArg(4, label_value_);
135  kernel_draw_volume_[0]->setArg(5, height_);
136  kernel_draw_volume_[0]->setArg(6, width_);
137  kernel_draw_volume_[0]->setArg(7, depth_);
138  kernel_draw_volume_[0]->setArg(8, *voxelized_phantom);
139 
140  // Launching kernel
141  GGint kernel_status = queue->enqueueNDRangeKernel(*kernel_draw_volume_[0], 0, global_wi, local_wi, nullptr, event);
142  opencl_manager.CheckOpenCLError(kernel_status, "GGEMSBox", "Draw");
143 
144  // GGEMS Profiling
146  profiler_manager.HandleEvent(*event, oss.str());
147 
148  queue->finish();
149 }
150 
154 
155 GGEMSBox* create_box(GGfloat const width, GGfloat const height, GGfloat const depth, char const* unit)
156 {
157  return new(std::nothrow) GGEMSBox(width, height, depth, unit);
158 }
159 
163 
165 {
166  if (box) {
167  delete box;
168  box = nullptr;
169  }
170 }
171 
175 
176 void set_position_box(GGEMSBox* box, GGfloat const pos_x, GGfloat const pos_y, GGfloat const pos_z, char const* unit)
177 {
178  box->SetPosition(pos_x, pos_y, pos_z, unit);
179 }
180 
184 
185 void set_material_box(GGEMSBox* box, char const* material)
186 {
187  box->SetMaterial(material);
188 }
189 
193 
194 void set_label_value_box(GGEMSBox* box, GGfloat const label_value)
195 {
196  box->SetLabelValue(label_value);
197 }
198 
202 
204 {
205  box->Initialize();
206 }
207 
211 
212 void draw_box(GGEMSBox* box)
213 {
214  box->Draw();
215 }
GGEMSVolumeCreatorManager::GetElementsSizes
GGfloat3 GetElementsSizes() const
size of voxels in the voxelized volume
Definition: GGEMSVolumeCreatorManager.hh:116
GGEMSVolume::kernel_draw_volume_
cl::Kernel ** kernel_draw_volume_
Definition: GGEMSVolume.hh:120
GGEMSVolumeCreatorManager::GetNumberElements
GGsize GetNumberElements(void) const
Return the total number of voxels.
Definition: GGEMSVolumeCreatorManager.hh:147
GGEMSVolume
Mother class handle volume.
Definition: GGEMSVolume.hh:41
GGEMSBox::width_
GGfloat width_
Definition: GGEMSBox.hh:99
set_position_box
void set_position_box(GGEMSBox *box, GGfloat const pos_x, GGfloat const pos_y, GGfloat const pos_z, char const *unit)
Set the position of the box.
Definition: GGEMSBox.cc:176
set_label_value_box
void set_label_value_box(GGEMSBox *box, GGfloat const label_value)
Set the label value in box.
Definition: GGEMSBox.cc:194
GGEMSVolumeCreatorManager::GetDataType
std::string GetDataType(void) const
get the type of data
Definition: GGEMSVolumeCreatorManager.hh:154
GGEMSVolumeCreatorManager::GetInstance
static GGEMSVolumeCreatorManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSVolumeCreatorManager.hh:67
GGEMSProfilerManager::GetInstance
static GGEMSProfilerManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSProfilerManager.hh:66
GGEMSBox::~GGEMSBox
~GGEMSBox(void)
GGEMSBox destructor.
Definition: GGEMSBox.cc:56
delete_box
void delete_box(GGEMSBox *box)
Delete instance of GGEMSBox.
Definition: GGEMSBox.cc:164
GGEMSOpenCLManager::CheckOpenCLError
void CheckOpenCLError(GGint const &error, std::string const &class_name, std::string const &method_name) const
check the OpenCL error
Definition: GGEMSOpenCLManager.cc:1048
GGEMSVolumeCreatorManager::GetVoxelizedVolume
cl::Buffer * GetVoxelizedVolume(void) const
Return the voxelized volume on OpenCL device.
Definition: GGEMSVolumeCreatorManager.hh:161
draw_box
void draw_box(GGEMSBox *box)
Draw analytical volume in voxelized phantom.
Definition: GGEMSBox.cc:212
GGEMSBox.hh
Class GGEMSBox inheriting from GGEMSVolume handling Box solid.
GGEMSBox
Class GGEMSBox inheriting from GGEMSVolume handling Box solid.
Definition: GGEMSBox.hh:41
GGEMSOpenCLManager::GetBestWorkItem
GGsize GetBestWorkItem(GGsize const &number_of_elements) const
get the best number of work item
Definition: GGEMSOpenCLManager.cc:1030
GGEMSOpenCLManager::GetEvent
cl::Event * GetEvent(GGsize const &thread_index) const
return an event to activated context
Definition: GGEMSOpenCLManager.hh:230
GGEMSTools.hh
Namespaces for different useful fonctions.
GGEMSBox::Draw
void Draw(void) override
Draw analytical volume in voxelized phantom.
Definition: GGEMSBox.cc:90
GGEMSVolume::label_value_
GGfloat label_value_
Definition: GGEMSVolume.hh:118
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSProfilerManager
GGEMS class managing profiler data.
Definition: GGEMSProfilerManager.hh:48
GGsize3_t::z_
GGsize z_
Definition: GGEMSTypes.hh:270
GGEMSOpenCLManager::GetWorkGroupSize
GGsize GetWorkGroupSize(void) const
Get the work group size defined in GGEMS on activated OpenCL context.
Definition: GGEMSOpenCLManager.hh:198
GGEMSVolumeCreatorManager
Singleton class handling convertion from analytical volume to voxelized volume.
Definition: GGEMSVolumeCreatorManager.hh:49
GGEMSBox::Initialize
void Initialize(void) override
Initialize the solid and store it in Phantom creator manager.
Definition: GGEMSBox.cc:67
GGfloat3
#define GGfloat3
Definition: GGEMSTypes.hh:275
GGEMSVolume::SetLabelValue
void SetLabelValue(GGfloat const &label_value)
Set the label value.
Definition: GGEMSVolume.cc:74
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
GGEMSVolumeCreatorManager::GetVolumeDimensions
GGsize3 GetVolumeDimensions() const
dimensions of volume
Definition: GGEMSVolumeCreatorManager.hh:133
GGEMSBox::height_
GGfloat height_
Definition: GGEMSBox.hh:98
GGEMSVolume::positions_
GGfloat3 positions_
Definition: GGEMSVolume.hh:119
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
set_material_box
void set_material_box(GGEMSBox *box, char const *material)
Set the material of the box.
Definition: GGEMSBox.cc:185
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
GGsize3_t::x_
GGsize x_
Definition: GGEMSTypes.hh:268
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
create_box
GGEMSBox * create_box(GGfloat const width, GGfloat const height, GGfloat const depth, char const *unit)
Create instance of GGEMSBox.
Definition: GGEMSBox.cc:155
GGEMSBox::depth_
GGfloat depth_
Definition: GGEMSBox.hh:100
GGEMSOpenCLManager::GetDeviceName
std::string GetDeviceName(GGsize const &device_index) const
Get the name of the activated device.
Definition: GGEMSOpenCLManager.hh:145
GGEMSProfilerManager.hh
GGEMS class managing profiler data.
GGEMSVolume::SetPosition
void SetPosition(GGfloat const &pos_x, GGfloat const &pos_y, GGfloat const &pos_z, std::string const &unit="mm")
Set the solid phantom position.
Definition: GGEMSVolume.cc:92
GGint3
#define GGint3
Definition: GGEMSTypes.hh:226
GGEMSProfilerManager::HandleEvent
void HandleEvent(cl::Event event, std::string const &profile_name)
handle an OpenCL event in profile_name type
Definition: GGEMSProfilerManager.cc:75
GGEMSOpenCLManager::GetIndexOfActivatedDevice
GGsize GetIndexOfActivatedDevice(GGsize const &thread_index) const
get the index of activated device
Definition: GGEMSOpenCLManager.hh:175
initialize_box
void initialize_box(GGEMSBox *box)
Initialize the solid and store it in Phantom creator manager.
Definition: GGEMSBox.cc:203
GGEMSOpenCLManager::GetCommandQueue
cl::CommandQueue * GetCommandQueue(GGsize const &thread_index) const
Return the command queue to activated context.
Definition: GGEMSOpenCLManager.hh:222
GGEMSBox::GGEMSBox
GGEMSBox(GGfloat const &width, GGfloat const &height, GGfloat const &depth, std::string const &unit="mm")
GGEMSBox constructor.
Definition: GGEMSBox.cc:40
DistanceUnit
T DistanceUnit(T const &value, std::string const &unit)
Choose best distance unit.
Definition: GGEMSSystemOfUnits.hh:243
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72
GGsize3_t::y_
GGsize y_
Definition: GGEMSTypes.hh:269
GGEMSVolume::SetMaterial
void SetMaterial(std::string const &material)
set the material, Air by default
Definition: GGEMSVolume.cc:79