Loading...
Searching...
No Matches
Par04InferenceMessenger.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26#ifdef USE_INFERENCE
27#include "G4UIcmdWithADoubleAndUnit.hh" // for G4UIcmdWithADoubleAndUnit
28#include "G4UIcmdWithAString.hh" // for G4UIcmdWithAString
29#include "G4UIcmdWithAnInteger.hh" // for G4UIcmdWithAnInteger
30#include "G4UIdirectory.hh" // for G4UIdirectory
32#include "Par04InferenceSetup.hh" // for Par04InferenceSetup
33#include <CLHEP/Units/SystemOfUnits.h> // for pi
34#include <G4ApplicationState.hh> // for G4State_Idle
35#include <G4ThreeVector.hh> // for G4ThreeVector
36#include <G4UImessenger.hh> // for G4UImessenger
37#include <string> // for stoi
38class G4UIcommand;
39
40//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
41
42Par04InferenceMessenger::Par04InferenceMessenger(
43 Par04InferenceSetup *aInference)
44 : G4UImessenger(), fInference(aInference) {
45 fExampleDir = new G4UIdirectory("/Par04/");
46 fExampleDir->SetGuidance("UI commands specific to this example");
47
48 fInferenceDir = new G4UIdirectory("/Par04/inference/");
49 fInferenceDir->SetGuidance("Inference construction UI commands");
50
51 fInferenceLibraryCmd =
52 new G4UIcmdWithAString("/Par04/inference/setInferenceLibrary", this);
53 fInferenceLibraryCmd->SetGuidance("Inference library.");
54 fInferenceLibraryCmd->SetParameterName("InferenceLibrary", false);
55 fInferenceLibraryCmd->AvailableForStates(G4State_Idle);
56 fInferenceLibraryCmd->SetToBeBroadcasted(true);
57
58 fSizeLatentVectorCmd =
59 new G4UIcmdWithAnInteger("/Par04/inference/setSizeLatentVector", this);
60 fSizeLatentVectorCmd->SetGuidance("Set size of the latent space vector.");
61 fSizeLatentVectorCmd->SetParameterName("SizeLatentVector", false);
62 fSizeLatentVectorCmd->SetRange("SizeLatentVector>0");
63 fSizeLatentVectorCmd->AvailableForStates(G4State_Idle);
64 fSizeLatentVectorCmd->SetToBeBroadcasted(true);
65
66 fSizeConditionVectorCmd =
67 new G4UIcmdWithAnInteger("/Par04/inference/setSizeConditionVector", this);
68 fSizeConditionVectorCmd->SetGuidance("Set size of the condition vector.");
69 fSizeConditionVectorCmd->SetParameterName("SizeConditionVector", false);
70 fSizeConditionVectorCmd->SetRange("SizeConditionVector>0");
71 fSizeConditionVectorCmd->AvailableForStates(G4State_Idle);
72 fSizeConditionVectorCmd->SetToBeBroadcasted(true);
73
74 fModelPathNameCmd =
75 new G4UIcmdWithAString("/Par04/inference/setModelPathName", this);
76 fModelPathNameCmd->SetGuidance("Model path and name.");
77 fModelPathNameCmd->SetParameterName("Name", false);
78 fModelPathNameCmd->AvailableForStates(G4State_Idle);
79 fModelPathNameCmd->SetToBeBroadcasted(true);
80
81 fProfileFlagCmd =
82 new G4UIcmdWithAnInteger("/Par04/inference/setProfileFlag", this);
83 fProfileFlagCmd->SetGuidance(
84 "Flag to save a json file for model execution profiling.");
85 fProfileFlagCmd->SetParameterName("ProfileFlag", false);
86 fProfileFlagCmd->SetRange("ProfileFlag>-1");
87 fProfileFlagCmd->AvailableForStates(G4State_Idle);
88 fProfileFlagCmd->SetToBeBroadcasted(true);
89
90 fOptimizationFlagCmd =
91 new G4UIcmdWithAnInteger("/Par04/inference/setOptimizationFlag", this);
92 fOptimizationFlagCmd->SetGuidance("Set optimization flag");
93 fOptimizationFlagCmd->SetParameterName("OptimizationFlag", false);
94 fOptimizationFlagCmd->SetRange("OptimizationFlag>-1");
95 fOptimizationFlagCmd->AvailableForStates(G4State_Idle);
96 fOptimizationFlagCmd->SetToBeBroadcasted(true);
97
98 fMeshNbRhoCellsCmd =
99 new G4UIcmdWithAnInteger("/Par04/inference/setNbOfRhoCells", this);
100 fMeshNbRhoCellsCmd->SetGuidance(
101 "Set number of rho cells in the cylindrical mesh readout.");
102 fMeshNbRhoCellsCmd->SetParameterName("NbRhoCells", false);
103 fMeshNbRhoCellsCmd->SetRange("NbRhoCells>0");
104 fMeshNbRhoCellsCmd->AvailableForStates(G4State_Idle);
105 fMeshNbRhoCellsCmd->SetToBeBroadcasted(true);
106
107 fMeshNbPhiCellsCmd =
108 new G4UIcmdWithAnInteger("/Par04/inference/setNbOfPhiCells", this);
109 fMeshNbPhiCellsCmd->SetGuidance(
110 "Set number of phi cells in the cylindrical mesh readout.");
111 fMeshNbPhiCellsCmd->SetParameterName("NbPhiCells", false);
112 fMeshNbPhiCellsCmd->SetRange("NbPhiCells>0");
113 fMeshNbPhiCellsCmd->AvailableForStates(G4State_Idle);
114 fMeshNbPhiCellsCmd->SetToBeBroadcasted(true);
115
116 fMeshNbZCellsCmd =
117 new G4UIcmdWithAnInteger("/Par04/inference/setNbOfZCells", this);
118 fMeshNbZCellsCmd->SetGuidance(
119 "Set number of z cells in the cylindrical mesh readout.");
120 fMeshNbZCellsCmd->SetParameterName("NbZCells", false);
121 fMeshNbZCellsCmd->SetRange("NbZCells>0");
122 fMeshNbZCellsCmd->AvailableForStates(G4State_Idle);
123 fMeshNbZCellsCmd->SetToBeBroadcasted(true);
124
125 fMeshSizeRhoCellsCmd =
126 new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfRhoCells", this);
127 fMeshSizeRhoCellsCmd->SetGuidance(
128 "Set size of rho cells in the cylindrical readout mesh");
129 fMeshSizeRhoCellsCmd->SetParameterName("Size", false);
130 fMeshSizeRhoCellsCmd->SetRange("Size>0.");
131 fMeshSizeRhoCellsCmd->SetUnitCategory("Length");
132 fMeshSizeRhoCellsCmd->AvailableForStates(G4State_Idle);
133 fMeshSizeRhoCellsCmd->SetToBeBroadcasted(true);
134
135 fMeshSizeZCellsCmd =
136 new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfZCells", this);
137 fMeshSizeZCellsCmd->SetGuidance(
138 "Set size of z cells in the cylindrical readout mesh");
139 fMeshSizeZCellsCmd->SetParameterName("Size", false);
140 fMeshSizeZCellsCmd->SetRange("Size>0.");
141 fMeshSizeZCellsCmd->SetUnitCategory("Length");
142 fMeshSizeZCellsCmd->AvailableForStates(G4State_Idle);
143 fMeshSizeZCellsCmd->SetToBeBroadcasted(true);
144
145 // Onnx Runtime Execution Provider flag commands
146 fCudaFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setCudaFlag", this);
147 G4cout << "f CudaFlagCmd " << fCudaFlagCmd << G4endl;
148 fCudaFlagCmd->SetGuidance(
149 "Whether to use CUDA Execution Provider for Onnx Runtime or not");
150 fCudaFlagCmd->SetParameterName("CudaFlag", false);
151 fCudaFlagCmd->SetRange("CudaFlag>-1");
152 fCudaFlagCmd->AvailableForStates(G4State_Idle);
153 fCudaFlagCmd->SetToBeBroadcasted(true);
154
155 /// OnnxRuntime Execution Provider Options
156 /// Cuda
157 fCudaOptionsDir = new G4UIdirectory("/Par04/inference/cuda/");
158 fCudaOptionsDir->SetGuidance(
159 "Commands for setting options for Cuda execution provider");
160
161 fCudaDeviceIdCmd =
162 new G4UIcmdWithAString("/Par04/inference/cuda/setDeviceId", this);
163 fCudaDeviceIdCmd->SetGuidance(
164 "Device ID of Device on which to run CUDA code");
165 fCudaDeviceIdCmd->SetParameterName("CudaDeviceId", false);
166 fCudaDeviceIdCmd->AvailableForStates(G4State_Idle);
167 fCudaDeviceIdCmd->SetToBeBroadcasted(true);
168
169 fCudaGpuMemLimitCmd =
170 new G4UIcmdWithAString("/Par04/inference/cuda/setGpuMemLimit", this);
171 fCudaGpuMemLimitCmd->SetGuidance("GPU Memory limit for CUDA");
172 fCudaGpuMemLimitCmd->SetParameterName("CudaGpuMemLimit", false);
173 fCudaGpuMemLimitCmd->AvailableForStates(G4State_Idle);
174 fCudaGpuMemLimitCmd->SetToBeBroadcasted(true);
175
176 fCudaArenaExtendedStrategyCmd = new G4UIcmdWithAString(
177 "/Par04/inference/cuda/setArenaExtendedStrategy", this);
178 fCudaArenaExtendedStrategyCmd->SetGuidance(
179 "Strategy for extending the device memory arena for CUDA");
180 fCudaArenaExtendedStrategyCmd->SetParameterName("CudaArenaExtendedStrategy",
181 false);
182 fCudaArenaExtendedStrategyCmd->AvailableForStates(G4State_Idle);
183 fCudaArenaExtendedStrategyCmd->SetToBeBroadcasted(true);
184
185 fCudaCudnnConvAlgoSearchCmd = new G4UIcmdWithAString(
186 "/Par04/inference/cuda/setCudnnConvAlgoSearch", this);
187 fCudaCudnnConvAlgoSearchCmd->SetGuidance(
188 "Set which cuDNN Convolution Operation to use");
189 fCudaCudnnConvAlgoSearchCmd->SetParameterName("CudaCudnnConvAlgoSearch",
190 false);
191 fCudaCudnnConvAlgoSearchCmd->AvailableForStates(G4State_Idle);
192 fCudaCudnnConvAlgoSearchCmd->SetToBeBroadcasted(true);
193
194 fCudaDoCopyInDefaultStreamCmd = new G4UIcmdWithAString(
195 "/Par04/inference/cuda/setDoCopyInDefaultStream", this);
196 fCudaDoCopyInDefaultStreamCmd->SetGuidance(
197 "Whether to use same stream for copying");
198 fCudaDoCopyInDefaultStreamCmd->SetParameterName("CudaDoCopyInDefaultStream",
199 false);
200 fCudaDoCopyInDefaultStreamCmd->AvailableForStates(G4State_Idle);
201 fCudaDoCopyInDefaultStreamCmd->SetToBeBroadcasted(true);
202
203 fCudaCudnnConvUseMaxWorkspaceCmd = new G4UIcmdWithAString(
204 "/Par04/inference/cuda/setCudnnConvUseMaxWorkspace", this);
205 fCudaCudnnConvUseMaxWorkspaceCmd->SetGuidance(
206 "Memory Limit for cuDNN convolution operations");
207 fCudaCudnnConvUseMaxWorkspaceCmd->SetParameterName(
208 "CudaCudnnConvUseMaxWorkspace", false);
209 fCudaCudnnConvUseMaxWorkspaceCmd->AvailableForStates(G4State_Idle);
210 fCudaCudnnConvUseMaxWorkspaceCmd->SetToBeBroadcasted(true);
211}
212
213//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
214
215Par04InferenceMessenger::~Par04InferenceMessenger() {
216 delete fInferenceLibraryCmd;
217 delete fSizeLatentVectorCmd;
218 delete fSizeConditionVectorCmd;
219 delete fModelPathNameCmd;
220 delete fProfileFlagCmd;
221 delete fOptimizationFlagCmd;
222 delete fMeshNbRhoCellsCmd;
223 delete fMeshNbPhiCellsCmd;
224 delete fMeshNbZCellsCmd;
225 delete fMeshSizeRhoCellsCmd;
226 delete fMeshSizeZCellsCmd;
227}
228
229//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
230
231void Par04InferenceMessenger::SetNewValue(G4UIcommand *aCommand,
232 G4String aNewValue) {
233 if (aCommand == fInferenceLibraryCmd) {
234 fInference->SetInferenceLibrary(aNewValue);
235 }
236 if (aCommand == fSizeLatentVectorCmd) {
237 fInference->SetSizeLatentVector(std::stoi(aNewValue));
238 }
239 if (aCommand == fSizeConditionVectorCmd) {
240 fInference->SetSizeConditionVector(std::stoi(aNewValue));
241 }
242 if (aCommand == fModelPathNameCmd) {
243 fInference->SetModelPathName(aNewValue);
244 }
245 if (aCommand == fProfileFlagCmd) {
246 fInference->SetProfileFlag(std::stoi(aNewValue));
247 }
248 if (aCommand == fOptimizationFlagCmd) {
249 fInference->SetOptimizationFlag(std::stoi(aNewValue));
250 } else if (aCommand == fMeshNbRhoCellsCmd) {
251 fInference->SetMeshNbOfCells(0,
252 fMeshNbRhoCellsCmd->GetNewIntValue(aNewValue));
253 } else if (aCommand == fMeshNbPhiCellsCmd) {
254 fInference->SetMeshNbOfCells(1,
255 fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
256 fInference->SetMeshSizeOfCells(
257 1, 2. * CLHEP::pi / fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
258 } else if (aCommand == fMeshNbZCellsCmd) {
259 fInference->SetMeshNbOfCells(2,
260 fMeshNbZCellsCmd->GetNewIntValue(aNewValue));
261 } else if (aCommand == fMeshSizeRhoCellsCmd) {
262 fInference->SetMeshSizeOfCells(
263 0, fMeshSizeRhoCellsCmd->GetNewDoubleValue(aNewValue));
264 } else if (aCommand == fMeshSizeZCellsCmd) {
265 fInference->SetMeshSizeOfCells(
266 2, fMeshSizeZCellsCmd->GetNewDoubleValue(aNewValue));
267 }
268 /// Onnx Runtime Execution Provider Flags
269 /// Cuda
270 if (aCommand == fCudaFlagCmd) {
271 fInference->SetCudaFlag(std::stoi(aNewValue));
272 }
273 if (aCommand == fCudaDeviceIdCmd) {
274 fInference->SetCudaDeviceId(aNewValue);
275 } else if (aCommand == fCudaGpuMemLimitCmd) {
276 fInference->SetCudaGpuMemLimit(aNewValue);
277 } else if (aCommand == fCudaArenaExtendedStrategyCmd) {
278 fInference->SetCudaArenaExtendedStrategy(aNewValue);
279 } else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
280 fInference->SetCudaCudnnConvAlgoSearch(aNewValue);
281 } else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
282 fInference->SetCudaDoCopyInDefaultStream(aNewValue);
283 } else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
284 fInference->SetCudaCudnnConvUseMaxWorkspace(aNewValue);
285 }
286}
287
288//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
289
290G4String Par04InferenceMessenger::GetCurrentValue(G4UIcommand *aCommand) {
291 G4String cv;
292
293 if (aCommand == fInferenceLibraryCmd) {
294 cv = fInferenceLibraryCmd->ConvertToString(
295 fInference->GetInferenceLibrary());
296 }
297 if (aCommand == fSizeLatentVectorCmd) {
298 cv = fSizeLatentVectorCmd->ConvertToString(
299 fInference->GetSizeLatentVector());
300 }
301 if (aCommand == fSizeConditionVectorCmd) {
302 cv = fSizeConditionVectorCmd->ConvertToString(
303 fInference->GetSizeConditionVector());
304 }
305 if (aCommand == fModelPathNameCmd) {
306 cv = fModelPathNameCmd->ConvertToString(fInference->GetModelPathName());
307 }
308 if (aCommand == fProfileFlagCmd) {
309 cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetProfileFlag());
310 }
311 if (aCommand == fOptimizationFlagCmd) {
312 cv = fSizeLatentVectorCmd->ConvertToString(
313 fInference->GetOptimizationFlag());
314 } else if (aCommand == fMeshNbRhoCellsCmd) {
315 cv = fMeshNbRhoCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[0]);
316 } else if (aCommand == fMeshNbPhiCellsCmd) {
317 cv = fMeshNbPhiCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[1]);
318 } else if (aCommand == fMeshNbZCellsCmd) {
319 cv = fMeshNbZCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[2]);
320 } else if (aCommand == fMeshSizeRhoCellsCmd) {
321 cv = fMeshSizeRhoCellsCmd->ConvertToString(
322 fInference->GetMeshSizeOfCells()[0]);
323 } else if (aCommand == fMeshSizeZCellsCmd) {
324 cv = fMeshSizeZCellsCmd->ConvertToString(
325 fInference->GetMeshSizeOfCells()[2]);
326 }
327 /// Onnx Runtime Execution Provider Flags
328 /// Cuda
329 if (aCommand == fCudaDeviceIdCmd) {
330 cv = fCudaDeviceIdCmd->ConvertToString(fInference->GetCudaDeviceId());
331 } else if (aCommand == fCudaGpuMemLimitCmd) {
332 cv = fCudaGpuMemLimitCmd->ConvertToString(fInference->GetCudaGpuMemLimit());
333 } else if (aCommand == fCudaArenaExtendedStrategyCmd) {
334 cv = fCudaArenaExtendedStrategyCmd->ConvertToString(
335 fInference->GetCudaArenaExtendedStrategy());
336 } else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
337 cv = fCudaCudnnConvAlgoSearchCmd->ConvertToString(
338 fInference->GetCudaCudnnConvAlgoSearch());
339 } else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
340 cv = fCudaDoCopyInDefaultStreamCmd->ConvertToString(
341 fInference->GetCudaDoCopyInDefaultStream());
342 } else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
343 cv = fCudaCudnnConvUseMaxWorkspaceCmd->ConvertToString(
344 fInference->GetCudaCudnnConvUseMaxWorkspace());
345 }
346
347 return cv;
348}
349
350#endif

Applications | User Support | Publications | Collaboration