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 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 "Par04InferenceMessenger.hh"
 28 
 29 #  include "Par04InferenceSetup.hh"  // for Par04InferenceSetup
 30 
 31 #  include "G4UIcmdWithADoubleAndUnit.hh"  // for G4UIcmdWithADoubleAndUnit
 32 #  include "G4UIcmdWithAString.hh"  // for G4UIcmdWithAString
 33 #  include "G4UIcmdWithAnInteger.hh"  // for G4UIcmdWithAnInteger
 34 #  include "G4UIdirectory.hh"  // for G4UIdirectory
 35 
 36 #  include <CLHEP/Units/SystemOfUnits.h>  // for pi
 37 #  include <G4ApplicationState.hh>  // for G4State_Idle
 38 #  include <G4ThreeVector.hh>  // for G4ThreeVector
 39 #  include <G4UImessenger.hh>  // for G4UImessenger
 40 #  include <string>  // for stoi
 41 class G4UIcommand;
 42 
 43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
 44 
 45 Par04InferenceMessenger::Par04InferenceMessenger(Par04InferenceSetup* aInference)
 46   : G4UImessenger(), fInference(aInference)
 47 {
 48   fExampleDir = new G4UIdirectory("/Par04/");
 49   fExampleDir->SetGuidance("UI commands specific to this example");
 50 
 51   fInferenceDir = new G4UIdirectory("/Par04/inference/");
 52   fInferenceDir->SetGuidance("Inference construction UI commands");
 53 
 54   fInferenceLibraryCmd = new G4UIcmdWithAString("/Par04/inference/setInferenceLibrary", this);
 55   fInferenceLibraryCmd->SetGuidance("Inference library.");
 56   fInferenceLibraryCmd->SetParameterName("InferenceLibrary", false);
 57   fInferenceLibraryCmd->AvailableForStates(G4State_Idle);
 58   fInferenceLibraryCmd->SetToBeBroadcasted(true);
 59 
 60   fSizeLatentVectorCmd = new G4UIcmdWithAnInteger("/Par04/inference/setSizeLatentVector", this);
 61   fSizeLatentVectorCmd->SetGuidance("Set size of the latent space vector.");
 62   fSizeLatentVectorCmd->SetParameterName("SizeLatentVector", false);
 63   fSizeLatentVectorCmd->SetRange("SizeLatentVector>0");
 64   fSizeLatentVectorCmd->AvailableForStates(G4State_Idle);
 65   fSizeLatentVectorCmd->SetToBeBroadcasted(true);
 66 
 67   fSizeConditionVectorCmd =
 68     new G4UIcmdWithAnInteger("/Par04/inference/setSizeConditionVector", this);
 69   fSizeConditionVectorCmd->SetGuidance("Set size of the condition vector.");
 70   fSizeConditionVectorCmd->SetParameterName("SizeConditionVector", false);
 71   fSizeConditionVectorCmd->SetRange("SizeConditionVector>0");
 72   fSizeConditionVectorCmd->AvailableForStates(G4State_Idle);
 73   fSizeConditionVectorCmd->SetToBeBroadcasted(true);
 74 
 75   fModelPathNameCmd = 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 = new G4UIcmdWithAnInteger("/Par04/inference/setProfileFlag", this);
 82   fProfileFlagCmd->SetGuidance("Flag to save a json file for model execution profiling.");
 83   fProfileFlagCmd->SetParameterName("ProfileFlag", false);
 84   fProfileFlagCmd->SetRange("ProfileFlag>-1");
 85   fProfileFlagCmd->AvailableForStates(G4State_Idle);
 86   fProfileFlagCmd->SetToBeBroadcasted(true);
 87 
 88   fOptimizationFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setOptimizationFlag", this);
 89   fOptimizationFlagCmd->SetGuidance("Set optimization flag");
 90   fOptimizationFlagCmd->SetParameterName("OptimizationFlag", false);
 91   fOptimizationFlagCmd->SetRange("OptimizationFlag>-1");
 92   fOptimizationFlagCmd->AvailableForStates(G4State_Idle);
 93   fOptimizationFlagCmd->SetToBeBroadcasted(true);
 94 
 95   fMeshNbRhoCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfRhoCells", this);
 96   fMeshNbRhoCellsCmd->SetGuidance("Set number of rho cells in the cylindrical mesh readout.");
 97   fMeshNbRhoCellsCmd->SetParameterName("NbRhoCells", false);
 98   fMeshNbRhoCellsCmd->SetRange("NbRhoCells>0");
 99   fMeshNbRhoCellsCmd->AvailableForStates(G4State_Idle);
100   fMeshNbRhoCellsCmd->SetToBeBroadcasted(true);
101 
102   fMeshNbPhiCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfPhiCells", this);
103   fMeshNbPhiCellsCmd->SetGuidance("Set number of phi cells in the cylindrical mesh readout.");
104   fMeshNbPhiCellsCmd->SetParameterName("NbPhiCells", false);
105   fMeshNbPhiCellsCmd->SetRange("NbPhiCells>0");
106   fMeshNbPhiCellsCmd->AvailableForStates(G4State_Idle);
107   fMeshNbPhiCellsCmd->SetToBeBroadcasted(true);
108 
109   fMeshNbZCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfZCells", this);
110   fMeshNbZCellsCmd->SetGuidance("Set number of z cells in the cylindrical mesh readout.");
111   fMeshNbZCellsCmd->SetParameterName("NbZCells", false);
112   fMeshNbZCellsCmd->SetRange("NbZCells>0");
113   fMeshNbZCellsCmd->AvailableForStates(G4State_Idle);
114   fMeshNbZCellsCmd->SetToBeBroadcasted(true);
115 
116   fMeshSizeRhoCellsCmd = new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfRhoCells", this);
117   fMeshSizeRhoCellsCmd->SetGuidance("Set size of rho cells in the cylindrical readout mesh");
118   fMeshSizeRhoCellsCmd->SetParameterName("Size", false);
119   fMeshSizeRhoCellsCmd->SetRange("Size>0.");
120   fMeshSizeRhoCellsCmd->SetUnitCategory("Length");
121   fMeshSizeRhoCellsCmd->AvailableForStates(G4State_Idle);
122   fMeshSizeRhoCellsCmd->SetToBeBroadcasted(true);
123 
124   fMeshSizeZCellsCmd = new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfZCells", this);
125   fMeshSizeZCellsCmd->SetGuidance("Set size of z cells in the cylindrical readout mesh");
126   fMeshSizeZCellsCmd->SetParameterName("Size", false);
127   fMeshSizeZCellsCmd->SetRange("Size>0.");
128   fMeshSizeZCellsCmd->SetUnitCategory("Length");
129   fMeshSizeZCellsCmd->AvailableForStates(G4State_Idle);
130   fMeshSizeZCellsCmd->SetToBeBroadcasted(true);
131 
132   // Onnx Runtime Execution Provider flag commands
133   fCudaFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setCudaFlag", this);
134   G4cout << "f CudaFlagCmd " << fCudaFlagCmd << G4endl;
135   fCudaFlagCmd->SetGuidance("Whether to use CUDA Execution Provider for Onnx Runtime or not");
136   fCudaFlagCmd->SetParameterName("CudaFlag", false);
137   fCudaFlagCmd->SetRange("CudaFlag>-1");
138   fCudaFlagCmd->AvailableForStates(G4State_Idle);
139   fCudaFlagCmd->SetToBeBroadcasted(true);
140 
141   /// OnnxRuntime Execution Provider Options
142   /// Cuda
143   fCudaOptionsDir = new G4UIdirectory("/Par04/inference/cuda/");
144   fCudaOptionsDir->SetGuidance("Commands for setting options for Cuda execution provider");
145 
146   fCudaDeviceIdCmd = new G4UIcmdWithAString("/Par04/inference/cuda/setDeviceId", this);
147   fCudaDeviceIdCmd->SetGuidance("Device ID of Device on which to run CUDA code");
148   fCudaDeviceIdCmd->SetParameterName("CudaDeviceId", false);
149   fCudaDeviceIdCmd->AvailableForStates(G4State_Idle);
150   fCudaDeviceIdCmd->SetToBeBroadcasted(true);
151 
152   fCudaGpuMemLimitCmd = new G4UIcmdWithAString("/Par04/inference/cuda/setGpuMemLimit", this);
153   fCudaGpuMemLimitCmd->SetGuidance("GPU Memory limit for CUDA");
154   fCudaGpuMemLimitCmd->SetParameterName("CudaGpuMemLimit", false);
155   fCudaGpuMemLimitCmd->AvailableForStates(G4State_Idle);
156   fCudaGpuMemLimitCmd->SetToBeBroadcasted(true);
157 
158   fCudaArenaExtendedStrategyCmd =
159     new G4UIcmdWithAString("/Par04/inference/cuda/setArenaExtendedStrategy", this);
160   fCudaArenaExtendedStrategyCmd->SetGuidance(
161     "Strategy for extending the device memory arena for CUDA");
162   fCudaArenaExtendedStrategyCmd->SetParameterName("CudaArenaExtendedStrategy", false);
163   fCudaArenaExtendedStrategyCmd->AvailableForStates(G4State_Idle);
164   fCudaArenaExtendedStrategyCmd->SetToBeBroadcasted(true);
165 
166   fCudaCudnnConvAlgoSearchCmd =
167     new G4UIcmdWithAString("/Par04/inference/cuda/setCudnnConvAlgoSearch", this);
168   fCudaCudnnConvAlgoSearchCmd->SetGuidance("Set which cuDNN Convolution Operation to use");
169   fCudaCudnnConvAlgoSearchCmd->SetParameterName("CudaCudnnConvAlgoSearch", false);
170   fCudaCudnnConvAlgoSearchCmd->AvailableForStates(G4State_Idle);
171   fCudaCudnnConvAlgoSearchCmd->SetToBeBroadcasted(true);
172 
173   fCudaDoCopyInDefaultStreamCmd =
174     new G4UIcmdWithAString("/Par04/inference/cuda/setDoCopyInDefaultStream", this);
175   fCudaDoCopyInDefaultStreamCmd->SetGuidance("Whether to use same stream for copying");
176   fCudaDoCopyInDefaultStreamCmd->SetParameterName("CudaDoCopyInDefaultStream", false);
177   fCudaDoCopyInDefaultStreamCmd->AvailableForStates(G4State_Idle);
178   fCudaDoCopyInDefaultStreamCmd->SetToBeBroadcasted(true);
179 
180   fCudaCudnnConvUseMaxWorkspaceCmd =
181     new G4UIcmdWithAString("/Par04/inference/cuda/setCudnnConvUseMaxWorkspace", this);
182   fCudaCudnnConvUseMaxWorkspaceCmd->SetGuidance("Memory Limit for cuDNN convolution operations");
183   fCudaCudnnConvUseMaxWorkspaceCmd->SetParameterName("CudaCudnnConvUseMaxWorkspace", false);
184   fCudaCudnnConvUseMaxWorkspaceCmd->AvailableForStates(G4State_Idle);
185   fCudaCudnnConvUseMaxWorkspaceCmd->SetToBeBroadcasted(true);
186 }
187 
188 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
189 
190 Par04InferenceMessenger::~Par04InferenceMessenger()
191 {
192   delete fInferenceLibraryCmd;
193   delete fSizeLatentVectorCmd;
194   delete fSizeConditionVectorCmd;
195   delete fModelPathNameCmd;
196   delete fProfileFlagCmd;
197   delete fOptimizationFlagCmd;
198   delete fMeshNbRhoCellsCmd;
199   delete fMeshNbPhiCellsCmd;
200   delete fMeshNbZCellsCmd;
201   delete fMeshSizeRhoCellsCmd;
202   delete fMeshSizeZCellsCmd;
203 }
204 
205 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
206 
207 void Par04InferenceMessenger::SetNewValue(G4UIcommand* aCommand, G4String aNewValue)
208 {
209   if (aCommand == fInferenceLibraryCmd) {
210     fInference->SetInferenceLibrary(aNewValue);
211   }
212   if (aCommand == fSizeLatentVectorCmd) {
213     fInference->SetSizeLatentVector(std::stoi(aNewValue));
214   }
215   if (aCommand == fSizeConditionVectorCmd) {
216     fInference->SetSizeConditionVector(std::stoi(aNewValue));
217   }
218   if (aCommand == fModelPathNameCmd) {
219     fInference->SetModelPathName(aNewValue);
220   }
221   if (aCommand == fProfileFlagCmd) {
222     fInference->SetProfileFlag(std::stoi(aNewValue));
223   }
224   if (aCommand == fOptimizationFlagCmd) {
225     fInference->SetOptimizationFlag(std::stoi(aNewValue));
226   }
227   else if (aCommand == fMeshNbRhoCellsCmd) {
228     fInference->SetMeshNbOfCells(0, fMeshNbRhoCellsCmd->GetNewIntValue(aNewValue));
229   }
230   else if (aCommand == fMeshNbPhiCellsCmd) {
231     fInference->SetMeshNbOfCells(1, fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
232     fInference->SetMeshSizeOfCells(1,
233                                    2. * CLHEP::pi / fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
234   }
235   else if (aCommand == fMeshNbZCellsCmd) {
236     fInference->SetMeshNbOfCells(2, fMeshNbZCellsCmd->GetNewIntValue(aNewValue));
237   }
238   else if (aCommand == fMeshSizeRhoCellsCmd) {
239     fInference->SetMeshSizeOfCells(0, fMeshSizeRhoCellsCmd->GetNewDoubleValue(aNewValue));
240   }
241   else if (aCommand == fMeshSizeZCellsCmd) {
242     fInference->SetMeshSizeOfCells(2, fMeshSizeZCellsCmd->GetNewDoubleValue(aNewValue));
243   }
244   /// Onnx Runtime Execution Provider Flags
245   /// Cuda
246   if (aCommand == fCudaFlagCmd) {
247     fInference->SetCudaFlag(std::stoi(aNewValue));
248   }
249   if (aCommand == fCudaDeviceIdCmd) {
250     fInference->SetCudaDeviceId(aNewValue);
251   }
252   else if (aCommand == fCudaGpuMemLimitCmd) {
253     fInference->SetCudaGpuMemLimit(aNewValue);
254   }
255   else if (aCommand == fCudaArenaExtendedStrategyCmd) {
256     fInference->SetCudaArenaExtendedStrategy(aNewValue);
257   }
258   else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
259     fInference->SetCudaCudnnConvAlgoSearch(aNewValue);
260   }
261   else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
262     fInference->SetCudaDoCopyInDefaultStream(aNewValue);
263   }
264   else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
265     fInference->SetCudaCudnnConvUseMaxWorkspace(aNewValue);
266   }
267 }
268 
269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
270 
271 G4String Par04InferenceMessenger::GetCurrentValue(G4UIcommand* aCommand)
272 {
273   G4String cv;
274 
275   if (aCommand == fInferenceLibraryCmd) {
276     cv = fInferenceLibraryCmd->ConvertToString(fInference->GetInferenceLibrary());
277   }
278   if (aCommand == fSizeLatentVectorCmd) {
279     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetSizeLatentVector());
280   }
281   if (aCommand == fSizeConditionVectorCmd) {
282     cv = fSizeConditionVectorCmd->ConvertToString(fInference->GetSizeConditionVector());
283   }
284   if (aCommand == fModelPathNameCmd) {
285     cv = fModelPathNameCmd->ConvertToString(fInference->GetModelPathName());
286   }
287   if (aCommand == fProfileFlagCmd) {
288     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetProfileFlag());
289   }
290   if (aCommand == fOptimizationFlagCmd) {
291     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetOptimizationFlag());
292   }
293   else if (aCommand == fMeshNbRhoCellsCmd) {
294     cv = fMeshNbRhoCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[0]);
295   }
296   else if (aCommand == fMeshNbPhiCellsCmd) {
297     cv = fMeshNbPhiCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[1]);
298   }
299   else if (aCommand == fMeshNbZCellsCmd) {
300     cv = fMeshNbZCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[2]);
301   }
302   else if (aCommand == fMeshSizeRhoCellsCmd) {
303     cv = fMeshSizeRhoCellsCmd->ConvertToString(fInference->GetMeshSizeOfCells()[0]);
304   }
305   else if (aCommand == fMeshSizeZCellsCmd) {
306     cv = fMeshSizeZCellsCmd->ConvertToString(fInference->GetMeshSizeOfCells()[2]);
307   }
308   /// Onnx Runtime Execution Provider Flags
309   /// Cuda
310   if (aCommand == fCudaDeviceIdCmd) {
311     cv = fCudaDeviceIdCmd->ConvertToString(fInference->GetCudaDeviceId());
312   }
313   else if (aCommand == fCudaGpuMemLimitCmd) {
314     cv = fCudaGpuMemLimitCmd->ConvertToString(fInference->GetCudaGpuMemLimit());
315   }
316   else if (aCommand == fCudaArenaExtendedStrategyCmd) {
317     cv = fCudaArenaExtendedStrategyCmd->ConvertToString(fInference->GetCudaArenaExtendedStrategy());
318   }
319   else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
320     cv = fCudaCudnnConvAlgoSearchCmd->ConvertToString(fInference->GetCudaCudnnConvAlgoSearch());
321   }
322   else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
323     cv = fCudaDoCopyInDefaultStreamCmd->ConvertToString(fInference->GetCudaDoCopyInDefaultStream());
324   }
325   else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
326     cv = fCudaCudnnConvUseMaxWorkspaceCmd->ConvertToString(
327       fInference->GetCudaCudnnConvUseMaxWorkspace());
328   }
329 
330   return cv;
331 }
332 
333 #endif
334