Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 #ifdef USE_INFERENCE 26 #ifdef USE_INFERENCE 27 # ifndef PAR04INFEERENCESETUP_HH << 27 #ifndef PAR04INFEERENCESETUP_HH 28 # define PAR04INFEERENCESETUP_HH << 28 #define PAR04INFEERENCESETUP_HH 29 29 30 # include "CLHEP/Units/SystemOfUnits.h" // << 30 #include <G4String.hh> // for G4String 31 << 31 #include <G4SystemOfUnits.hh> // for mm 32 # include "G4ThreeVector.hh" // for G4Thre << 32 #include <G4Types.hh> // for G4int, G4double, G4bool, G4f... 33 << 33 #include <memory> // for unique_ptr 34 # include <G4String.hh> // for G4String << 34 #include <vector> // for vector 35 # include <G4SystemOfUnits.hh> // for mm << 35 #include "CLHEP/Units/SystemOfUnits.h" // for mm 36 # include <G4Types.hh> // for G4int, G4dou << 36 #include "G4ThreeVector.hh" // for G4ThreeVector 37 # include <memory> // for unique_ptr << 38 # include <vector> // for vector << 39 class Par04DetectorConstruction; 37 class Par04DetectorConstruction; 40 class Par04InferenceInterface; 38 class Par04InferenceInterface; 41 class Par04InferenceMessenger; 39 class Par04InferenceMessenger; 42 40 43 /** 41 /** 44 * @brief Inference setup. 42 * @brief Inference setup. 45 * 43 * 46 * Constructs the input vector of size b+c to << 44 * Constructs the input vector of size b+c to run the inference, b represents the size of 47 * the size of the latent space (or the encode << 45 * the latent space (or the encoded space in a Variational Autoencoder based model), 48 * Autoencoder based model), c represents the << 46 * c represents the size of the conditional vector. The b values of the input vector 49 *The b values of the input vector are randoml << 47 * are randomly sampled from b-dimensional Gaussian distribution. The c values 50 *Gaussian distribution. The c values represen << 48 * represent respectively the condition values of the particle energy, angle and 51 *values of the particle energy, angle and det << 49 * detector geometry. These condition values are user-specific application. 52 *values are user-specific application. The en << 50 * The energy rescaling is used to retrieve the original energy scale in MeV. 53 *the original energy scale in MeV. Computes t << 51 * Computes the cell position in the detector of each inferred energy value. 54 *of each inferred energy value. << 55 * 52 * 56 **/ 53 **/ 57 54 58 class Par04InferenceSetup 55 class Par04InferenceSetup 59 { 56 { 60 public: << 57 public: 61 Par04InferenceSetup(); << 58 Par04InferenceSetup(); 62 ~Par04InferenceSetup(); << 59 ~Par04InferenceSetup(); 63 << 60 64 /// Geometry setup << 61 /// Geometry setup 65 /// Check if inference should be performed << 62 /// Check if inference should be performed for the particle 66 /// @param[in] aEnergy Particle's energy << 63 /// @param[in] aEnergy Particle's energy 67 G4bool IfTrigger(G4double aEnergy); << 64 G4bool IfTrigger(G4double aEnergy); 68 /// Set mesh size. << 65 /// Set mesh size. 69 /// @param aSize (x,y,x) size for Carthesi << 66 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 70 /// cylindrical coordinates. << 67 /// cylindrical coordinates. 71 inline void SetMeshSize(const G4ThreeVecto << 68 inline void SetMeshSize(const G4ThreeVector& aSize) { fMeshSize = aSize; }; 72 /// Get mesh size. << 69 /// Get mesh size. 73 /// @return G4ThreeVector (x,y,x) size for << 70 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 74 /// z) for cylindrical coordinates. << 71 /// z) for cylindrical coordinates. 75 inline G4ThreeVector GetMeshSize() const { << 72 inline G4ThreeVector GetMeshSize() const { return fMeshSize; }; 76 /// Set number of mesh cells. << 73 /// Set number of mesh cells. 77 /// @param aSize (x,y,x) size for Carthesi << 74 /// @param aSize (x,y,x) size for Carthesian coordinates, or (R, phi, z) for 78 /// cylindrical coordinates. << 75 /// cylindrical coordinates. 79 inline void SetMeshNumber(const G4ThreeVec << 76 inline void SetMeshNumber(const G4ThreeVector& aSize) { fMeshNumber = aSize; }; 80 /// Get number of mesh cells. << 77 /// Get number of mesh cells. 81 /// @return G4ThreeVector (x,y,x) size for << 78 /// @return G4ThreeVector (x,y,x) size for Carthesian coordinates, or (R, phi, 82 /// z) for cylindrical coordinates. << 79 /// z) for cylindrical coordinates. 83 inline G4ThreeVector GetMeshNumber() const << 80 inline G4ThreeVector GetMeshNumber() const { return fMeshNumber; }; 84 /// Set size of the condition vector << 81 /// Set size of the condition vector 85 inline void SetSizeConditionVector(G4int a << 82 inline void SetSizeConditionVector(G4int aNumber) { fSizeConditionVector = aNumber; }; 86 /// Get size of the condition vector << 83 /// Get size of the condition vector 87 inline G4int GetSizeConditionVector() cons << 84 inline G4int GetSizeConditionVector() const { return fSizeConditionVector; }; 88 /// Set size of the latent space vector << 85 /// Set size of the latent space vector 89 inline void SetSizeLatentVector(G4int aNum << 86 inline void SetSizeLatentVector(G4int aNumber) { fSizeLatentVector = aNumber; }; 90 /// Get size of the latent space vector << 87 /// Get size of the latent space vector 91 inline G4int GetSizeLatentVector() const { << 88 inline G4int GetSizeLatentVector() const { return fSizeLatentVector; }; 92 /// Set path and name of the model << 89 /// Set path and name of the model 93 inline void SetModelPathName(G4String aNam << 90 inline void SetModelPathName(G4String aName) { fModelPathName = aName; }; 94 /// Get path and name of the model << 91 /// Get path and name of the model 95 inline G4String GetModelPathName() const { << 92 inline G4String GetModelPathName() const { return fModelPathName; }; 96 /// Set profiling flag << 93 /// Set profiling flag 97 inline void SetProfileFlag(G4int aNumber) << 94 inline void SetProfileFlag(G4int aNumber) { fProfileFlag = aNumber; }; 98 /// Get profiling flag << 95 /// Get profiling flag 99 inline G4int GetProfileFlag() const { retu << 96 inline G4int GetProfileFlag() const { return fProfileFlag; }; 100 /// Set optimization flag << 97 /// Set optimization flag 101 inline void SetOptimizationFlag(G4int aNum << 98 inline void SetOptimizationFlag(G4int aNumber) { fOptimizationFlag = aNumber; }; 102 /// Get optimization flag << 99 /// Get optimization flag 103 inline G4int GetOptimizationFlag() const { << 100 inline G4int GetOptimizationFlag() const { return fOptimizationFlag; }; 104 /// Get name of the inference library << 101 /// Get name of the inference library 105 inline G4String GetInferenceLibrary() cons << 102 inline G4String GetInferenceLibrary() const { return fInferenceLibrary; }; 106 /// Set name of the inference library and << 103 /// Set name of the inference library and create a pointer to chosen inference interface 107 /// interface << 104 void SetInferenceLibrary(G4String aName); 108 void SetInferenceLibrary(G4String aName); << 105 /// Check settings of the inference library 109 /// Check settings of the inference librar << 106 void CheckInferenceLibrary(); 110 void CheckInferenceLibrary(); << 107 /// Set number of Mesh cells in cylindrical coordinates (r, phi, z) 111 /// Set number of Mesh cells in cylindrica << 108 inline void SetMeshNbOfCells(G4ThreeVector aNb) { fMeshNumber = aNb; }; 112 inline void SetMeshNbOfCells(G4ThreeVector << 109 /// Set number of Mesh cells in cylindrical coordinates 113 /// Set number of Mesh cells in cylindrica << 110 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 114 /// @param[in] aIndex index of cylindrical << 111 inline void SetMeshNbOfCells(G4int aIndex, G4double aNb) { fMeshNumber[aIndex] = aNb; }; 115 inline void SetMeshNbOfCells(G4int aIndex, << 112 /// Get number of Mesh cells in cylindrical coordinates (r, phi, z) 116 /// Get number of Mesh cells in cylindrica << 113 inline G4ThreeVector GetMeshNbOfCells() const { return fMeshNumber; }; 117 inline G4ThreeVector GetMeshNbOfCells() co << 114 /// Set size of Mesh cells in cylindrical coordinates (r, phi, z) 118 /// Set size of Mesh cells in cylindrical << 115 inline void SetMeshSizeOfCells(G4ThreeVector aNb) { fMeshSize = aNb; }; 119 inline void SetMeshSizeOfCells(G4ThreeVect << 116 /// Set size of Mesh cells in cylindrical coordinates 120 /// Set size of Mesh cells in cylindrical << 117 /// @param[in] aIndex index of cylindrical axis (0,1,2) = (r, phi, z) 121 /// @param[in] aIndex index of cylindrical << 118 inline void SetMeshSizeOfCells(G4int aIndex, G4double aNb) { fMeshSize[aIndex] = aNb; }; 122 inline void SetMeshSizeOfCells(G4int aInde << 119 /// Get size of Mesh cells in cylindrical coordinates (r, phi, z) 123 /// Get size of Mesh cells in cylindrical << 120 inline G4ThreeVector GetMeshSizeOfCells() const { return fMeshSize; }; 124 inline G4ThreeVector GetMeshSizeOfCells() << 121 125 /// Setting execution providers flags << 122 /// Execute inference 126 /// GPU << 123 /// @param[out] aDepositsEnergies of inferred energies deposited in the 127 inline void SetCudaFlag(G4int aNumber) { f << 124 /// detector 128 inline G4int GetCudaFlag() const { return << 125 /// @param[in] aParticleEnergy Energy of initial particle 129 /// Setting execution providers Options << 126 void GetEnergies(std::vector<G4double>& aEnergies, G4double aParticleEnergy, 130 /// Cuda << 127 G4float aInitialAngle); 131 inline void SetCudaDeviceId(G4String aNumb << 128 132 inline G4String GetCudaDeviceId() const { << 129 /// Calculate positions 133 inline void SetCudaGpuMemLimit(G4String aN << 130 /// @param[out] aDepositsPositions Vector of positions corresponding to 134 inline G4String GetCudaGpuMemLimit() const << 131 /// energies deposited in the detector 135 inline void SetCudaArenaExtendedStrategy(G << 132 /// @param[in] aParticlePosition Initial particle position which is centre of 136 { << 133 /// transverse plane of the mesh 137 fCudaArenaExtendedStrategy = aNumber; << 134 /// and beginning of the mesh in the longitudinal direction 138 }; << 135 /// @param[in] aParticleDirection Initial particle direction for the mesh 139 inline G4String GetCudaArenaExtendedStrate << 136 /// rotation 140 inline void SetCudaCudnnConvAlgoSearch(G4S << 137 void GetPositions(std::vector<G4ThreeVector>& aDepositsPositions, G4ThreeVector aParticlePosition, 141 { << 138 G4ThreeVector aParticleDirection); 142 fCudaCudnnConvAlgoSearch = aNumber; << 139 143 }; << 140 private: 144 inline G4String GetCudaCudnnConvAlgoSearch << 141 /// Cell's size: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 145 inline void SetCudaDoCopyInDefaultStream(G << 142 /// coordinates Can be changed with UI command `/example/mesh/size <x y z>/<r 146 { << 143 /// phi z> <unit>`. For cylindrical coordinates phi is ignored and calculated 147 fCudaDoCopyInDefaultStream = aNumber; << 144 /// from fMeshNumber. 148 }; << 145 G4ThreeVector fMeshSize = G4ThreeVector(2.325 * CLHEP::mm, 1, 3.4 * CLHEP::mm); 149 inline G4String GetCudaDoCopyInDefaultStre << 146 /// Number of cells: (x,y,x) for Carthesian, and (R, phi, z) for cylindrical 150 inline void SetCudaCudnnConvUseMaxWorkspac << 147 /// coordinates. Can be changed with UI command `/example/mesh/number <Nx Ny 151 { << 148 /// Nz>/<Nr Nphi Nz>` 152 fCudaCudnnConvUseMaxWorkspace = aNumber; << 149 G4ThreeVector fMeshNumber = G4ThreeVector(18, 50, 45); 153 }; << 150 /// Inference interface 154 inline G4String GetCudaCudnnConvUseMaxWork << 151 std::unique_ptr<Par04InferenceInterface> fInferenceInterface; 155 { << 152 /// Inference messenger 156 return fCudaCudnnConvUseMaxWorkspace; << 153 Par04InferenceMessenger* fInferenceMessenger; 157 }; << 154 /// Maximum particle energy value (in MeV) in the training range 158 << 155 float fMaxEnergy = 1024000.0; 159 /// Execute inference << 156 /// Maximum particle angle (in degrees) in the training range 160 /// @param[out] aDepositsEnergies of infer << 157 float fMaxAngle = 90.0; 161 /// detector << 158 /// Name of the inference library 162 /// @param[in] aParticleEnergy Energy of i << 159 G4String fInferenceLibrary = "ONNX"; 163 void GetEnergies(std::vector<G4double>& aE << 160 /// Size of the latent space vector 164 G4float aInitialAngle); << 161 G4int fSizeLatentVector = 10; 165 << 162 /// Size of the condition vector 166 /// Calculate positions << 163 G4int fSizeConditionVector = 4; 167 /// @param[out] aDepositsPositions Vector << 164 /// Name of the inference library 168 /// energies deposited in the detector << 165 G4String fModelPathName = "MLModels/Generator.onnx"; 169 /// @param[in] aParticlePosition Initial p << 166 /// ONNX specific 170 /// transverse plane of the mesh << 167 /// Profiling flag 171 /// and beginning of the mesh i << 168 G4bool fProfileFlag = false; 172 /// @param[in] aParticleDirection Initial << 169 /// Optimization flag 173 /// rotation << 170 G4bool fOptimizationFlag = false; 174 void GetPositions(std::vector<G4ThreeVecto << 171 /// Intra-operation number of threads 175 G4ThreeVector aParticleP << 172 G4int fIntraOpNumThreads = 1; 176 << 177 private: << 178 /// Cell's size: (x,y,x) for Carthesian, a << 179 /// coordinates Can be changed with UI com << 180 /// phi z> <unit>`. For cylindrical coordi << 181 /// from fMeshNumber. << 182 G4ThreeVector fMeshSize = G4ThreeVector(2. << 183 /// Number of cells: (x,y,x) for Carthesia << 184 /// coordinates. Can be changed with UI co << 185 /// Nz>/<Nr Nphi Nz>` << 186 G4ThreeVector fMeshNumber = G4ThreeVector( << 187 /// Inference interface << 188 std::unique_ptr<Par04InferenceInterface> f << 189 /// Inference messenger << 190 Par04InferenceMessenger* fInferenceMesseng << 191 /// Maximum particle energy value (in MeV) << 192 float fMaxEnergy = 1024000.0; << 193 /// Maximum particle angle (in degrees) in << 194 float fMaxAngle = 90.0; << 195 /// Name of the inference library << 196 G4String fInferenceLibrary = "ONNX"; << 197 /// Size of the latent space vector << 198 G4int fSizeLatentVector = 10; << 199 /// Size of the condition vector << 200 G4int fSizeConditionVector = 4; << 201 /// Name of the inference library << 202 G4String fModelPathName = "MLModels/Genera << 203 /// ONNX specific << 204 /// Profiling flag << 205 G4bool fProfileFlag = false; << 206 /// Optimization flag << 207 G4bool fOptimizationFlag = false; << 208 /// Optimization file << 209 G4String fModelSavePath = "MLModels/Optimi << 210 /// Profiling file << 211 G4String fProfilingOutputSavePath = "opt.j << 212 /// Intra-operation number of threads << 213 G4int fIntraOpNumThreads = 1; << 214 /// Flags for execution providers << 215 /// GPU << 216 G4bool fCudaFlag = false; << 217 /// Execution Provider Options << 218 /// Cuda options << 219 G4String fCudaDeviceId = "0"; << 220 G4String fCudaGpuMemLimit = "2147483648"; << 221 G4String fCudaArenaExtendedStrategy = "kSa << 222 G4String fCudaCudnnConvAlgoSearch = "DEFAU << 223 G4String fCudaDoCopyInDefaultStream = "1"; << 224 G4String fCudaCudnnConvUseMaxWorkspace = " << 225 std::vector<const char*> cuda_keys{ << 226 "device_id", << 227 "gpu_mem_limit", << 228 "arena_extend_strategy", << 229 "cudnn_conv_algo_search", << 230 "do_copy_in_default_stream", << 231 "cudnn_conv_use_max_workspace", << 232 }; << 233 std::vector<const char*> cuda_values{ << 234 fCudaDeviceId.c_str(), << 235 fCudaGpuMemLimit.c_str(), << 236 fCudaArenaExtendedStrategy.c_str(), << 237 fCudaCudnnConvAlgoSearch.c_str(), << 238 fCudaDoCopyInDefaultStream.c_str(), << 239 fCudaCudnnConvUseMaxWorkspace.c_str(), << 240 }; << 241 }; 173 }; 242 174 243 # endif /* PAR04INFEERENCESETUP_HH */ << 175 #endif /* PAR04INFEERENCESETUP_HH */ 244 #endif 176 #endif 245 177