GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSphere.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 GGEMSSphere::GGEMSSphere(GGfloat const& radius, std::string const& unit)
40 : GGEMSVolume()
41 {
42  GGcout("GGEMSSphere", "GGEMSSphere", 3) << "GGEMSSphere creating..." << GGendl;
43 
44  radius_ = DistanceUnit(radius, unit);
45 
46  GGcout("GGEMSSphere", "GGEMSSphere", 3) << "GGEMSSphere created!!!" << GGendl;
47 }
48 
52 
54 {
55  GGcout("GGEMSSphere", "~GGEMSSphere", 3) << "GGEMSSphere erasing..." << GGendl;
56 
57  GGcout("GGEMSSphere", "~GGEMSSphere", 3) << "GGEMSSphere erased!!!" << GGendl;
58 }
59 
63 
65 {
66  GGcout("GGEMSSphere", "Initialize", 3) << "Initializing GGEMSSphere solid volume..." << GGendl;
67 
68  // Getting the path to kernel
69  std::string const kOpenCLKernelPath = OPENCL_KERNEL_PATH;
70  std::string const kFilename = kOpenCLKernelPath + "/DrawGGEMSSphere.cl";
71 
72  // Get the volume creator manager
74 
75  // Get the OpenCL manager
77 
78  // Get the data type and compiling kernel
79  std::string const kDataType = "-D" + volume_creator_manager.GetDataType();
80  opencl_manager.CompileKernel(kFilename, "draw_ggems_sphere", kernel_draw_volume_, nullptr, const_cast<char*>(kDataType.c_str()));
81 }
82 
86 
88 {
89  GGcout("GGEMSSphere", "Draw", 3) << "Drawing Sphere..." << GGendl;
90 
91  // Get the OpenCL manager
93 
94  // Get the volume creator manager
96 
97  // Get command queue and event
98  cl::CommandQueue* queue = opencl_manager.GetCommandQueue(0);
99  cl::Event* event = opencl_manager.GetEvent(0);
100 
101  // Get Device name and storing methode name + device
102  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(0);
103  std::string device_name = opencl_manager.GetDeviceName(device_index);
104  std::ostringstream oss(std::ostringstream::out);
105  oss << "GGEMSSphere::Draw on " << device_name << ", index " << device_index;
106 
107  // Get parameters from phantom creator
108  GGfloat3 voxel_sizes = volume_creator_manager.GetElementsSizes();
109 
110  GGint3 phantom_dimensions;
111  phantom_dimensions.x = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().x_);
112  phantom_dimensions.y = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().y_);
113  phantom_dimensions.z = static_cast<GGint>(volume_creator_manager.GetVolumeDimensions().z_);
114 
115  GGsize number_of_elements = volume_creator_manager.GetNumberElements();
116  cl::Buffer* voxelized_phantom = volume_creator_manager.GetVoxelizedVolume();
117 
118  // Getting work group size, and work-item number
119  GGsize work_group_size = opencl_manager.GetWorkGroupSize();
120  GGsize number_of_work_items = opencl_manager.GetBestWorkItem(number_of_elements);
121 
122  // Parameters for work-item in kernel
123  cl::NDRange global_wi(number_of_work_items);
124  cl::NDRange local_wi(work_group_size);
125 
126  // Set parameters for kernel
127  kernel_draw_volume_[0]->setArg(0, number_of_elements);
128  kernel_draw_volume_[0]->setArg(1, voxel_sizes);
129  kernel_draw_volume_[0]->setArg(2, phantom_dimensions);
130  kernel_draw_volume_[0]->setArg(3, positions_);
131  kernel_draw_volume_[0]->setArg(4, label_value_);
132  kernel_draw_volume_[0]->setArg(5, radius_);
133  kernel_draw_volume_[0]->setArg(6, *voxelized_phantom);
134 
135  // Launching kernel
136  cl_int kernel_status = queue->enqueueNDRangeKernel(*kernel_draw_volume_[0], 0, global_wi, local_wi, nullptr, event);
137  opencl_manager.CheckOpenCLError(kernel_status, "GGEMSSphere", "Draw");
138 
139  // GGEMS Profiling
141  profiler_manager.HandleEvent(*event, oss.str());
142 
143  queue->finish();
144 }
145 
149 
150 GGEMSSphere* create_sphere(GGfloat const radius, char const* unit)
151 {
152  return new(std::nothrow) GGEMSSphere(radius, unit);
153 }
154 
158 
160 {
161  if (sphere) {
162  delete sphere;
163  sphere = nullptr;
164  }
165 }
166 
170 
171 void set_position_sphere(GGEMSSphere* sphere, GGfloat const pos_x, GGfloat const pos_y, GGfloat const pos_z, char const* unit)
172 {
173  sphere->SetPosition(pos_x, pos_y, pos_z, unit);
174 }
175 
179 
180 void set_material_sphere(GGEMSSphere* sphere, char const* material)
181 {
182  sphere->SetMaterial(material);
183 }
184 
188 
189 void set_label_value_sphere(GGEMSSphere* sphere, GGfloat const label_value)
190 {
191  sphere->SetLabelValue(label_value);
192 }
193 
197 
199 {
200  sphere->Initialize();
201 }
202 
206 
208 {
209  sphere->Draw();
210 }
GGEMSSphere::~GGEMSSphere
~GGEMSSphere(void)
GGEMSSphere destructor.
Definition: GGEMSSphere.cc:53
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
set_position_sphere
void set_position_sphere(GGEMSSphere *sphere, GGfloat const pos_x, GGfloat const pos_y, GGfloat const pos_z, char const *unit)
Set the position of the sphere.
Definition: GGEMSSphere.cc:171
draw_sphere
void draw_sphere(GGEMSSphere *sphere)
Draw analytical volume in voxelized phantom.
Definition: GGEMSSphere.cc:207
set_label_value_sphere
void set_label_value_sphere(GGEMSSphere *sphere, GGfloat const label_value)
Set the label value in sphere.
Definition: GGEMSSphere.cc:189
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
initialize_sphere
void initialize_sphere(GGEMSSphere *sphere)
Initialize the solid and store it in Phantom creator manager.
Definition: GGEMSSphere.cc:198
GGEMSSphere
Class GGEMSSphere inheriting from GGEMSVolume handling Sphere solid.
Definition: GGEMSSphere.hh:41
delete_sphere
void delete_sphere(GGEMSSphere *sphere)
Delete instance of GGEMSSphere.
Definition: GGEMSSphere.cc:159
GGEMSProfilerManager::GetInstance
static GGEMSProfilerManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSProfilerManager.hh:66
GGEMSSphere::Draw
void Draw(void) override
Draw analytical volume in voxelized phantom.
Definition: GGEMSSphere.cc:87
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
create_sphere
GGEMSSphere * create_sphere(GGfloat const radius, char const *unit)
Create instance of GGEMSSphere.
Definition: GGEMSSphere.cc:150
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
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
GGEMSSphere::Initialize
void Initialize(void) override
Initialize the solid and store it in Phantom creator manager.
Definition: GGEMSSphere.cc:64
GGEMSVolumeCreatorManager
Singleton class handling convertion from analytical volume to voxelized volume.
Definition: GGEMSVolumeCreatorManager.hh:49
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
GGEMSVolume::positions_
GGfloat3 positions_
Definition: GGEMSVolume.hh:119
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
GGsize3_t::x_
GGsize x_
Definition: GGEMSTypes.hh:268
GGEMSSphere::radius_
GGfloat radius_
Definition: GGEMSSphere.hh:96
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
GGEMSSphere::GGEMSSphere
GGEMSSphere(GGfloat const &radius, std::string const &unit="mm")
GGEMSSphere constructor.
Definition: GGEMSSphere.cc:39
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
GGEMSSphere.hh
Class GGEMSSphere inheriting from GGEMSVolume handling Sphere solid.
set_material_sphere
void set_material_sphere(GGEMSSphere *sphere, char const *material)
Set the material of the sphere.
Definition: GGEMSSphere.cc:180
GGEMSOpenCLManager::GetCommandQueue
cl::CommandQueue * GetCommandQueue(GGsize const &thread_index) const
Return the command queue to activated context.
Definition: GGEMSOpenCLManager.hh:222
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