GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSRAMManager.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 
32 
35 
39 
41 {
42  GGcout("GGEMSRAMManager", "GGEMSRAMManager", 3) << "GGEMSRAMManager creating..." << GGendl;
43 
44  // Get the OpenCL manager and number of detected device
47 
48  // Creating buffer for each detected device
51 
55 
56  for (GGsize i = 0; i < number_detected_devices_; ++i) {
57  max_available_ram_[i] = opencl_manager.GetRAMMemory(i);
58  max_buffer_size_[i] = opencl_manager.GetMaxBufferAllocationSize(i);
59  allocated_memories_[i].clear();
60  }
61 
62  GGcout("GGEMSRAMManager", "GGEMSRAMManager", 3) << "GGEMSRAMManager created!!!" << GGendl;
63 }
64 
68 
70 {
71  GGcout("GGEMSRAMManager", "~GGEMSRAMManager", 3) << "GGEMSRAMManager erasing..." << GGendl;
72 
73  if (allocated_ram_) {
74  delete allocated_ram_;
75  allocated_ram_ = nullptr;
76  }
77 
78  if (max_available_ram_) {
79  delete max_available_ram_;
80  max_available_ram_ = nullptr;
81  }
82 
83  if (max_buffer_size_) {
84  delete max_buffer_size_;
85  max_buffer_size_ = nullptr;
86  }
87 
88  if (allocated_memories_) {
89  for (GGsize i = 0; i < number_detected_devices_; ++i) allocated_memories_[i].clear();
90  }
91 
92  GGcout("GGEMSRAMManager", "~GGEMSRAMManager", 3) << "GGEMSRAMManager erased!!!" << GGendl;
93 }
94 
98 
100 {
101  GGcout("GGEMSRAMManager", "Clean", 3) << "GGEMSRAMManager cleaning..." << GGendl;
102 
103  GGcout("GGEMSRAMManager", "Clean", 3) << "GGEMSRAMManager cleaned!!!" << GGendl;
104 }
105 
109 
110 void GGEMSRAMManager::IncrementRAMMemory(std::string const& class_name, GGsize const& index, GGsize const& size)
111 {
112  // Getting OpenCL manager
114 
115  // Get index of the device
116  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(index);
117 
118  // Checking if class has already allocated memory, if not, creating one
119  if (allocated_memories_[device_index].find(class_name) == allocated_memories_[device_index].end()) {
120  allocated_memories_[device_index].insert(std::make_pair(class_name, size));
121  }
122  else {
123  allocated_memories_[device_index][class_name] += size;
124  }
125 
126  // Increment size
127  allocated_ram_[device_index] += size;
128 }
129 
133 
134 void GGEMSRAMManager::DecrementRAMMemory(std::string const& class_name, GGsize const& index, GGsize const& size)
135 {
136  // Getting OpenCL manager
138 
139  // Get index of the device
140  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(index);
141 
142  // decrement size
143  allocated_ram_[device_index] -= size;
144  allocated_memories_[device_index][class_name] -= size;
145 }
146 
150 
152 {
153  // Get the OpenCL manager
155  GGsize number_activated_device = opencl_manager.GetNumberOfActivatedDevice();
156 
157  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << GGendl;
158 
159  // Loop over activated device
160  for (GGsize i = 0; i < number_activated_device; ++i) {
161  GGsize device_index = opencl_manager.GetIndexOfActivatedDevice(i);
162 
163  // Compute allocated percent RAM
164  GGfloat percent_allocated_RAM = static_cast<GGfloat>(allocated_ram_[device_index]) * 100.0f / static_cast<GGfloat>(max_available_ram_[device_index]);
165 
166  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "Device: " << opencl_manager.GetDeviceName(device_index) << GGendl;
167  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "-------" << GGendl;
168  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "Total RAM memory allocated: " << BestDigitalUnit(allocated_ram_[device_index]) << " / " << BestDigitalUnit(max_available_ram_[device_index]) << " (" << percent_allocated_RAM << "%)" << GGendl;
169  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "Details: " << GGendl;
170  for (auto&& i : allocated_memories_[device_index]) {
171  float usage = static_cast<GGfloat>(i.second) * 100.0f / static_cast<GGfloat>(allocated_ram_[device_index]);
172  if (allocated_ram_[device_index] == 0) usage = 0.0f;
173  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << " + In '" << i.first << "': " << BestDigitalUnit(i.second) << " allocated (" << usage << "%)" << GGendl;
174  }
175  }
176 
177  GGcout("GGEMSRAMManager", "PrintRAMStatus", 0) << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << GGendl;
178 }
179 
183 
185 {
187 }
188 
192 
194 {
195  ram_manager->PrintRAMStatus();
196 }
GGEMSRAMManager.hh
GGEMS class handling RAM memory.
print_infos_ram_manager
void print_infos_ram_manager(GGEMSRAMManager *ram_manager)
Print information about RAM memory.
Definition: GGEMSRAMManager.cc:193
GGEMSOpenCLManager::GetNumberOfActivatedDevice
GGsize GetNumberOfActivatedDevice(void) const
get the number of activated devices
Definition: GGEMSOpenCLManager.hh:167
GGEMSOpenCLManager::GetRAMMemory
GGsize GetRAMMemory(GGsize const &device_index) const
Get the RAM in bytes on OpenCL device.
Definition: GGEMSOpenCLManager.hh:191
GGEMSRAMManager
GGEMS class handling RAM memory.
Definition: GGEMSRAMManager.hh:51
GGEMSRAMManager::number_detected_devices_
GGsize number_detected_devices_
Definition: GGEMSRAMManager.hh:160
GGEMSOpenCLManager::GetMaxBufferAllocationSize
GGsize GetMaxBufferAllocationSize(GGsize const &device_index) const
Get the max buffer size in bytes on activated OpenCL device.
Definition: GGEMSOpenCLManager.hh:183
GGEMSRAMManager::GetInstance
static GGEMSRAMManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSRAMManager.hh:69
GGsize
#define GGsize
Definition: GGEMSTypes.hh:252
GGEMSRAMManager::~GGEMSRAMManager
~GGEMSRAMManager(void)
Unable the destructor for the user.
Definition: GGEMSRAMManager.cc:69
GGEMSRAMManager::allocated_ram_
GGsize * allocated_ram_
Definition: GGEMSRAMManager.hh:161
GGEMSRAMManager::allocated_memories_
AllocatedMemoryUMap * allocated_memories_
Definition: GGEMSRAMManager.hh:164
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSSystemOfUnits.hh
Namespace storing all the usefull physical units.
GGEMSOpenCLManager.hh
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
GGEMSRAMManager::GGEMSRAMManager
GGEMSRAMManager(void)
Unable the constructor for the user.
Definition: GGEMSRAMManager.cc:40
GGEMSOpenCLManager::GetNumberOfDetectedDevice
GGsize GetNumberOfDetectedDevice(void) const
get the number of detected devices
Definition: GGEMSOpenCLManager.hh:160
GGendl
#define GGendl
overload C++ std::endl
Definition: GGEMSPrint.hh:60
GGEMSRAMManager::max_available_ram_
GGsize * max_available_ram_
Definition: GGEMSRAMManager.hh:162
GGEMSRAMManager::max_buffer_size_
GGsize * max_buffer_size_
Definition: GGEMSRAMManager.hh:163
GGEMSOpenCLManager
Singleton class storing all informations about OpenCL and managing GPU/CPU devices,...
Definition: GGEMSOpenCLManager.hh:54
AllocatedMemoryUMap
std::unordered_map< std::string, GGsize > AllocatedMemoryUMap
Definition: GGEMSRAMManager.hh:44
GGEMSRAMManager::DecrementRAMMemory
void DecrementRAMMemory(std::string const &class_name, GGsize const &index, GGsize const &size)
decrement the size of the global allocated buffer
Definition: GGEMSRAMManager.cc:134
GGEMSRAMManager::Clean
void Clean(void)
clean OpenCL data if necessary
Definition: GGEMSRAMManager.cc:99
GGEMSOpenCLManager::GetDeviceName
std::string GetDeviceName(GGsize const &device_index) const
Get the name of the activated device.
Definition: GGEMSOpenCLManager.hh:145
GGEMSRAMManager::PrintRAMStatus
void PrintRAMStatus(void) const
print the RAM memory status for activated context
Definition: GGEMSRAMManager.cc:151
get_instance_ggems_ram_manager
GGEMSRAMManager * get_instance_ggems_ram_manager(void)
Get the GGEMSRAMManager pointer for python user.
Definition: GGEMSRAMManager.cc:184
GGEMSOpenCLManager::GetIndexOfActivatedDevice
GGsize GetIndexOfActivatedDevice(GGsize const &thread_index) const
get the index of activated device
Definition: GGEMSOpenCLManager.hh:175
BestDigitalUnit
std::string BestDigitalUnit(GGulong const &value)
Choose best digital unit.
Definition: GGEMSSystemOfUnits.hh:425
GGEMSRAMManager::IncrementRAMMemory
void IncrementRAMMemory(std::string const &class_name, GGsize const &index, GGsize const &size)
increment the size of the global allocated buffer
Definition: GGEMSRAMManager.cc:110
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
GGEMSOpenCLManager::GetInstance
static GGEMSOpenCLManager & GetInstance(void)
Create at first time the Singleton.
Definition: GGEMSOpenCLManager.hh:72