Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:31:54

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 #ifdef USE_INFERENCE
0027 #  include "Par04InferenceMessenger.hh"
0028 
0029 #  include "Par04InferenceSetup.hh"  // for Par04InferenceSetup
0030 
0031 #  include "G4UIcmdWithADoubleAndUnit.hh"  // for G4UIcmdWithADoubleAndUnit
0032 #  include "G4UIcmdWithAString.hh"  // for G4UIcmdWithAString
0033 #  include "G4UIcmdWithAnInteger.hh"  // for G4UIcmdWithAnInteger
0034 #  include "G4UIdirectory.hh"  // for G4UIdirectory
0035 
0036 #  include <CLHEP/Units/SystemOfUnits.h>  // for pi
0037 #  include <G4ApplicationState.hh>  // for G4State_Idle
0038 #  include <G4ThreeVector.hh>  // for G4ThreeVector
0039 #  include <G4UImessenger.hh>  // for G4UImessenger
0040 #  include <string>  // for stoi
0041 class G4UIcommand;
0042 
0043 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0044 
0045 Par04InferenceMessenger::Par04InferenceMessenger(Par04InferenceSetup* aInference)
0046   : G4UImessenger(), fInference(aInference)
0047 {
0048   fExampleDir = new G4UIdirectory("/Par04/");
0049   fExampleDir->SetGuidance("UI commands specific to this example");
0050 
0051   fInferenceDir = new G4UIdirectory("/Par04/inference/");
0052   fInferenceDir->SetGuidance("Inference construction UI commands");
0053 
0054   fInferenceLibraryCmd = new G4UIcmdWithAString("/Par04/inference/setInferenceLibrary", this);
0055   fInferenceLibraryCmd->SetGuidance("Inference library.");
0056   fInferenceLibraryCmd->SetParameterName("InferenceLibrary", false);
0057   fInferenceLibraryCmd->AvailableForStates(G4State_Idle);
0058   fInferenceLibraryCmd->SetToBeBroadcasted(true);
0059 
0060   fSizeLatentVectorCmd = new G4UIcmdWithAnInteger("/Par04/inference/setSizeLatentVector", this);
0061   fSizeLatentVectorCmd->SetGuidance("Set size of the latent space vector.");
0062   fSizeLatentVectorCmd->SetParameterName("SizeLatentVector", false);
0063   fSizeLatentVectorCmd->SetRange("SizeLatentVector>0");
0064   fSizeLatentVectorCmd->AvailableForStates(G4State_Idle);
0065   fSizeLatentVectorCmd->SetToBeBroadcasted(true);
0066 
0067   fSizeConditionVectorCmd =
0068     new G4UIcmdWithAnInteger("/Par04/inference/setSizeConditionVector", this);
0069   fSizeConditionVectorCmd->SetGuidance("Set size of the condition vector.");
0070   fSizeConditionVectorCmd->SetParameterName("SizeConditionVector", false);
0071   fSizeConditionVectorCmd->SetRange("SizeConditionVector>0");
0072   fSizeConditionVectorCmd->AvailableForStates(G4State_Idle);
0073   fSizeConditionVectorCmd->SetToBeBroadcasted(true);
0074 
0075   fModelPathNameCmd = new G4UIcmdWithAString("/Par04/inference/setModelPathName", this);
0076   fModelPathNameCmd->SetGuidance("Model path and name.");
0077   fModelPathNameCmd->SetParameterName("Name", false);
0078   fModelPathNameCmd->AvailableForStates(G4State_Idle);
0079   fModelPathNameCmd->SetToBeBroadcasted(true);
0080 
0081   fModelTypeCmd = new G4UIcmdWithAString("/Par04/inference/setModelType", this);
0082   fModelTypeCmd->SetGuidance("Model type");
0083   fModelTypeCmd->SetParameterName("Name", false);
0084   fModelTypeCmd->AvailableForStates(G4State_Idle);
0085   fModelTypeCmd->SetToBeBroadcasted(true);
0086 
0087   fProfileFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setProfileFlag", this);
0088   fProfileFlagCmd->SetGuidance("Flag to save a json file for model execution profiling.");
0089   fProfileFlagCmd->SetParameterName("ProfileFlag", false);
0090   fProfileFlagCmd->SetRange("ProfileFlag>-1");
0091   fProfileFlagCmd->AvailableForStates(G4State_Idle);
0092   fProfileFlagCmd->SetToBeBroadcasted(true);
0093 
0094   fOptimizationFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setOptimizationFlag", this);
0095   fOptimizationFlagCmd->SetGuidance("Set optimization flag");
0096   fOptimizationFlagCmd->SetParameterName("OptimizationFlag", false);
0097   fOptimizationFlagCmd->SetRange("OptimizationFlag>-1");
0098   fOptimizationFlagCmd->AvailableForStates(G4State_Idle);
0099   fOptimizationFlagCmd->SetToBeBroadcasted(true);
0100 
0101   fMeshNbRhoCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfRhoCells", this);
0102   fMeshNbRhoCellsCmd->SetGuidance("Set number of rho cells in the cylindrical mesh readout.");
0103   fMeshNbRhoCellsCmd->SetParameterName("NbRhoCells", false);
0104   fMeshNbRhoCellsCmd->SetRange("NbRhoCells>0");
0105   fMeshNbRhoCellsCmd->AvailableForStates(G4State_Idle);
0106   fMeshNbRhoCellsCmd->SetToBeBroadcasted(true);
0107 
0108   fMeshNbPhiCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfPhiCells", this);
0109   fMeshNbPhiCellsCmd->SetGuidance("Set number of phi cells in the cylindrical mesh readout.");
0110   fMeshNbPhiCellsCmd->SetParameterName("NbPhiCells", false);
0111   fMeshNbPhiCellsCmd->SetRange("NbPhiCells>0");
0112   fMeshNbPhiCellsCmd->AvailableForStates(G4State_Idle);
0113   fMeshNbPhiCellsCmd->SetToBeBroadcasted(true);
0114 
0115   fMeshNbZCellsCmd = new G4UIcmdWithAnInteger("/Par04/inference/setNbOfZCells", this);
0116   fMeshNbZCellsCmd->SetGuidance("Set number of z cells in the cylindrical mesh readout.");
0117   fMeshNbZCellsCmd->SetParameterName("NbZCells", false);
0118   fMeshNbZCellsCmd->SetRange("NbZCells>0");
0119   fMeshNbZCellsCmd->AvailableForStates(G4State_Idle);
0120   fMeshNbZCellsCmd->SetToBeBroadcasted(true);
0121 
0122   fMeshSizeRhoCellsCmd = new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfRhoCells", this);
0123   fMeshSizeRhoCellsCmd->SetGuidance("Set size of rho cells in the cylindrical readout mesh");
0124   fMeshSizeRhoCellsCmd->SetParameterName("Size", false);
0125   fMeshSizeRhoCellsCmd->SetRange("Size>0.");
0126   fMeshSizeRhoCellsCmd->SetUnitCategory("Length");
0127   fMeshSizeRhoCellsCmd->AvailableForStates(G4State_Idle);
0128   fMeshSizeRhoCellsCmd->SetToBeBroadcasted(true);
0129 
0130   fMeshSizeZCellsCmd = new G4UIcmdWithADoubleAndUnit("/Par04/inference/setSizeOfZCells", this);
0131   fMeshSizeZCellsCmd->SetGuidance("Set size of z cells in the cylindrical readout mesh");
0132   fMeshSizeZCellsCmd->SetParameterName("Size", false);
0133   fMeshSizeZCellsCmd->SetRange("Size>0.");
0134   fMeshSizeZCellsCmd->SetUnitCategory("Length");
0135   fMeshSizeZCellsCmd->AvailableForStates(G4State_Idle);
0136   fMeshSizeZCellsCmd->SetToBeBroadcasted(true);
0137 
0138   // Onnx Runtime Execution Provider flag commands
0139   fCudaFlagCmd = new G4UIcmdWithAnInteger("/Par04/inference/setCudaFlag", this);
0140   G4cout << "f CudaFlagCmd " << fCudaFlagCmd << G4endl;
0141   fCudaFlagCmd->SetGuidance("Whether to use CUDA Execution Provider for Onnx Runtime or not");
0142   fCudaFlagCmd->SetParameterName("CudaFlag", false);
0143   fCudaFlagCmd->SetRange("CudaFlag>-1");
0144   fCudaFlagCmd->AvailableForStates(G4State_Idle);
0145   fCudaFlagCmd->SetToBeBroadcasted(true);
0146 
0147   /// OnnxRuntime Execution Provider Options
0148   /// Cuda
0149   fCudaOptionsDir = new G4UIdirectory("/Par04/inference/cuda/");
0150   fCudaOptionsDir->SetGuidance("Commands for setting options for Cuda execution provider");
0151 
0152   fCudaDeviceIdCmd = new G4UIcmdWithAString("/Par04/inference/cuda/setDeviceId", this);
0153   fCudaDeviceIdCmd->SetGuidance("Device ID of Device on which to run CUDA code");
0154   fCudaDeviceIdCmd->SetParameterName("CudaDeviceId", false);
0155   fCudaDeviceIdCmd->AvailableForStates(G4State_Idle);
0156   fCudaDeviceIdCmd->SetToBeBroadcasted(true);
0157 
0158   fCudaGpuMemLimitCmd = new G4UIcmdWithAString("/Par04/inference/cuda/setGpuMemLimit", this);
0159   fCudaGpuMemLimitCmd->SetGuidance("GPU Memory limit for CUDA");
0160   fCudaGpuMemLimitCmd->SetParameterName("CudaGpuMemLimit", false);
0161   fCudaGpuMemLimitCmd->AvailableForStates(G4State_Idle);
0162   fCudaGpuMemLimitCmd->SetToBeBroadcasted(true);
0163 
0164   fCudaArenaExtendedStrategyCmd =
0165     new G4UIcmdWithAString("/Par04/inference/cuda/setArenaExtendedStrategy", this);
0166   fCudaArenaExtendedStrategyCmd->SetGuidance(
0167     "Strategy for extending the device memory arena for CUDA");
0168   fCudaArenaExtendedStrategyCmd->SetParameterName("CudaArenaExtendedStrategy", false);
0169   fCudaArenaExtendedStrategyCmd->AvailableForStates(G4State_Idle);
0170   fCudaArenaExtendedStrategyCmd->SetToBeBroadcasted(true);
0171 
0172   fCudaCudnnConvAlgoSearchCmd =
0173     new G4UIcmdWithAString("/Par04/inference/cuda/setCudnnConvAlgoSearch", this);
0174   fCudaCudnnConvAlgoSearchCmd->SetGuidance("Set which cuDNN Convolution Operation to use");
0175   fCudaCudnnConvAlgoSearchCmd->SetParameterName("CudaCudnnConvAlgoSearch", false);
0176   fCudaCudnnConvAlgoSearchCmd->AvailableForStates(G4State_Idle);
0177   fCudaCudnnConvAlgoSearchCmd->SetToBeBroadcasted(true);
0178 
0179   fCudaDoCopyInDefaultStreamCmd =
0180     new G4UIcmdWithAString("/Par04/inference/cuda/setDoCopyInDefaultStream", this);
0181   fCudaDoCopyInDefaultStreamCmd->SetGuidance("Whether to use same stream for copying");
0182   fCudaDoCopyInDefaultStreamCmd->SetParameterName("CudaDoCopyInDefaultStream", false);
0183   fCudaDoCopyInDefaultStreamCmd->AvailableForStates(G4State_Idle);
0184   fCudaDoCopyInDefaultStreamCmd->SetToBeBroadcasted(true);
0185 
0186   fCudaCudnnConvUseMaxWorkspaceCmd =
0187     new G4UIcmdWithAString("/Par04/inference/cuda/setCudnnConvUseMaxWorkspace", this);
0188   fCudaCudnnConvUseMaxWorkspaceCmd->SetGuidance("Memory Limit for cuDNN convolution operations");
0189   fCudaCudnnConvUseMaxWorkspaceCmd->SetParameterName("CudaCudnnConvUseMaxWorkspace", false);
0190   fCudaCudnnConvUseMaxWorkspaceCmd->AvailableForStates(G4State_Idle);
0191   fCudaCudnnConvUseMaxWorkspaceCmd->SetToBeBroadcasted(true);
0192 }
0193 
0194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0195 
0196 Par04InferenceMessenger::~Par04InferenceMessenger()
0197 {
0198   delete fInferenceLibraryCmd;
0199   delete fSizeLatentVectorCmd;
0200   delete fSizeConditionVectorCmd;
0201   delete fModelPathNameCmd;
0202   delete fModelTypeCmd;
0203   delete fProfileFlagCmd;
0204   delete fOptimizationFlagCmd;
0205   delete fMeshNbRhoCellsCmd;
0206   delete fMeshNbPhiCellsCmd;
0207   delete fMeshNbZCellsCmd;
0208   delete fMeshSizeRhoCellsCmd;
0209   delete fMeshSizeZCellsCmd;
0210 }
0211 
0212 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0213 
0214 void Par04InferenceMessenger::SetNewValue(G4UIcommand* aCommand, G4String aNewValue)
0215 {
0216   if (aCommand == fInferenceLibraryCmd) {
0217     fInference->SetInferenceLibrary(aNewValue);
0218   }
0219   if (aCommand == fSizeLatentVectorCmd) {
0220     fInference->SetSizeLatentVector(std::stoi(aNewValue));
0221   }
0222   if (aCommand == fSizeConditionVectorCmd) {
0223     fInference->SetSizeConditionVector(std::stoi(aNewValue));
0224   }
0225   if (aCommand == fModelPathNameCmd) {
0226     fInference->SetModelPathName(aNewValue);
0227   }
0228   if (aCommand == fModelTypeCmd) {
0229     fInference->SetModelType(aNewValue);
0230   }
0231   if (aCommand == fProfileFlagCmd) {
0232     fInference->SetProfileFlag(std::stoi(aNewValue));
0233   }
0234   if (aCommand == fOptimizationFlagCmd) {
0235     fInference->SetOptimizationFlag(std::stoi(aNewValue));
0236   }
0237   else if (aCommand == fMeshNbRhoCellsCmd) {
0238     fInference->SetMeshNbOfCells(0, fMeshNbRhoCellsCmd->GetNewIntValue(aNewValue));
0239   }
0240   else if (aCommand == fMeshNbPhiCellsCmd) {
0241     fInference->SetMeshNbOfCells(1, fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
0242     fInference->SetMeshSizeOfCells(1,
0243                                    2. * CLHEP::pi / fMeshNbPhiCellsCmd->GetNewIntValue(aNewValue));
0244   }
0245   else if (aCommand == fMeshNbZCellsCmd) {
0246     fInference->SetMeshNbOfCells(2, fMeshNbZCellsCmd->GetNewIntValue(aNewValue));
0247   }
0248   else if (aCommand == fMeshSizeRhoCellsCmd) {
0249     fInference->SetMeshSizeOfCells(0, fMeshSizeRhoCellsCmd->GetNewDoubleValue(aNewValue));
0250   }
0251   else if (aCommand == fMeshSizeZCellsCmd) {
0252     fInference->SetMeshSizeOfCells(2, fMeshSizeZCellsCmd->GetNewDoubleValue(aNewValue));
0253   }
0254   /// Onnx Runtime Execution Provider Flags
0255   /// Cuda
0256   if (aCommand == fCudaFlagCmd) {
0257     fInference->SetCudaFlag(std::stoi(aNewValue));
0258   }
0259   if (aCommand == fCudaDeviceIdCmd) {
0260     fInference->SetCudaDeviceId(aNewValue);
0261   }
0262   else if (aCommand == fCudaGpuMemLimitCmd) {
0263     fInference->SetCudaGpuMemLimit(aNewValue);
0264   }
0265   else if (aCommand == fCudaArenaExtendedStrategyCmd) {
0266     fInference->SetCudaArenaExtendedStrategy(aNewValue);
0267   }
0268   else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
0269     fInference->SetCudaCudnnConvAlgoSearch(aNewValue);
0270   }
0271   else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
0272     fInference->SetCudaDoCopyInDefaultStream(aNewValue);
0273   }
0274   else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
0275     fInference->SetCudaCudnnConvUseMaxWorkspace(aNewValue);
0276   }
0277 }
0278 
0279 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0280 
0281 G4String Par04InferenceMessenger::GetCurrentValue(G4UIcommand* aCommand)
0282 {
0283   G4String cv;
0284 
0285   if (aCommand == fInferenceLibraryCmd) {
0286     cv = fInferenceLibraryCmd->ConvertToString(fInference->GetInferenceLibrary());
0287   }
0288   if (aCommand == fSizeLatentVectorCmd) {
0289     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetSizeLatentVector());
0290   }
0291   if (aCommand == fSizeConditionVectorCmd) {
0292     cv = fSizeConditionVectorCmd->ConvertToString(fInference->GetSizeConditionVector());
0293   }
0294   if (aCommand == fModelPathNameCmd) {
0295     cv = fModelPathNameCmd->ConvertToString(fInference->GetModelPathName());
0296   }
0297   if (aCommand == fModelTypeCmd) {
0298     cv = fModelTypeCmd->ConvertToString(fInference->GetModelType());
0299   }
0300   if (aCommand == fProfileFlagCmd) {
0301     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetProfileFlag());
0302   }
0303   if (aCommand == fOptimizationFlagCmd) {
0304     cv = fSizeLatentVectorCmd->ConvertToString(fInference->GetOptimizationFlag());
0305   }
0306   else if (aCommand == fMeshNbRhoCellsCmd) {
0307     cv = fMeshNbRhoCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[0]);
0308   }
0309   else if (aCommand == fMeshNbPhiCellsCmd) {
0310     cv = fMeshNbPhiCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[1]);
0311   }
0312   else if (aCommand == fMeshNbZCellsCmd) {
0313     cv = fMeshNbZCellsCmd->ConvertToString(fInference->GetMeshNbOfCells()[2]);
0314   }
0315   else if (aCommand == fMeshSizeRhoCellsCmd) {
0316     cv = fMeshSizeRhoCellsCmd->ConvertToString(fInference->GetMeshSizeOfCells()[0]);
0317   }
0318   else if (aCommand == fMeshSizeZCellsCmd) {
0319     cv = fMeshSizeZCellsCmd->ConvertToString(fInference->GetMeshSizeOfCells()[2]);
0320   }
0321   /// Onnx Runtime Execution Provider Flags
0322   /// Cuda
0323   if (aCommand == fCudaDeviceIdCmd) {
0324     cv = fCudaDeviceIdCmd->ConvertToString(fInference->GetCudaDeviceId());
0325   }
0326   else if (aCommand == fCudaGpuMemLimitCmd) {
0327     cv = fCudaGpuMemLimitCmd->ConvertToString(fInference->GetCudaGpuMemLimit());
0328   }
0329   else if (aCommand == fCudaArenaExtendedStrategyCmd) {
0330     cv = fCudaArenaExtendedStrategyCmd->ConvertToString(fInference->GetCudaArenaExtendedStrategy());
0331   }
0332   else if (aCommand == fCudaCudnnConvAlgoSearchCmd) {
0333     cv = fCudaCudnnConvAlgoSearchCmd->ConvertToString(fInference->GetCudaCudnnConvAlgoSearch());
0334   }
0335   else if (aCommand == fCudaDoCopyInDefaultStreamCmd) {
0336     cv = fCudaDoCopyInDefaultStreamCmd->ConvertToString(fInference->GetCudaDoCopyInDefaultStream());
0337   }
0338   else if (aCommand == fCudaCudnnConvUseMaxWorkspaceCmd) {
0339     cv = fCudaCudnnConvUseMaxWorkspaceCmd->ConvertToString(
0340       fInference->GetCudaCudnnConvUseMaxWorkspace());
0341   }
0342 
0343   return cv;
0344 }
0345 
0346 #endif