Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VoxelisationHelper.hh
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// G4VoxelisationHelper
27//
28// Class description:
29//
30// A helper class to undertake voxelisation in parallel, aiding
31// and off-loading the work of G4GeometryManager.
32//
33// Only one instance must exist, and it must be owned by G4GeometryManager
34// -- by the master thread's instance for the time being.
35//
36// It is designed to be thread safe, and its methods re-entrant.
37// The following methods are expected to be called asynchronously
38// by threads safely.
39
40// Author: John Apostolakis (CERN), 10.02.2025
41// -------------------------------------------------------------------
42#ifndef G4VOXELISATIONHELPER_HH
43#define G4VOXELISATIONHELPER_HH
44
45#include <vector>
46
47#include "G4Types.hh"
48#include "G4SmartVoxelStat.hh"
49#include "G4ios.hh"
50
52class G4Timer;
53
54/**
55 * @brief G4VoxelisationHelper is a helper class to undertake voxelisation
56 * in parallel, aiding and off-loading the work of G4GeometryManager.
57 */
58
60{
61 public:
62
63 /**
64 * Constructor & Destructor.
65 */
68
69 /**
70 * Key method that creates a list of volumes and resets the state
71 * to prepare for the parallel optimisation.
72 */
73 void PrepareParallelOptimisation(G4bool allOpts, G4bool verbose);
74
75 /**
76 * Contributes to voxel optimisation until all work is done.
77 * To be called by a worker thread initialisation, not by the user.
78 */
80
81 // Methods that can be used by this Helper, Geometry Manager and others
82 // --------------------------------------------------------------------
83
84 /**
85 * Methods to report statistics on the voxelisation process.
86 */
87 static void ReportVoxelStats( std::vector<G4SmartVoxelStat>& stats,
88 G4double totalCpuTime,
89 std::ostream &os = G4cout );
90 void ReportVoxelInfo(G4LogicalVolume* logVolume, std::ostream& os);
91
92 /**
93 * Sets verbosity mode.
94 */
95 inline void SetVerbosity(G4bool verbose) { fVerboseParallel = verbose; }
96
97 /**
98 * Returns true if all workers are finished (or all work is done).
99 */
101
102 // Auxiliary method - may be useful elsewhere
103
104 /**
105 * Check that volumes marked to optimise are done, and report number
106 * of those that are missing voxel header.
107 */
109
110 private: // Methods used to implement the parallel optimisation
111
112 /**
113 * Builds a vector of relevant volumes.
114 */
115 void CreateListOfVolumesToOptimise(G4bool allOpts, G4bool verbose);
116
117 /**
118 * Returns a pointer to the logical volume to optimise.
119 */
120 G4LogicalVolume* ObtainVolumeToOptimise();
121
122 /**
123 * Prepares for doing the work in parallel.
124 * Called in preparation (not MT safe).
125 */
126 void ReSetParallelOptimisation(G4bool verbose);
127
128 /**
129 * Resets (empties) the list of candidate volumes for optimisation.
130 * Must be called when optimisation is finished.
131 */
132 void ResetListOfVolumesToOptimise();
133
134 /**
135 * Thread-safe method for a worker to report it's finished its work.
136 * It counts the number of workers that finished, and returns count.
137 * It counts the number of volumes optimised; if all workers have
138 * reported, it results in a 'Finished' state.
139 */
140 G4int ReportWorkerIsDoneOptimising(unsigned int numVolumesOptimised);
141
142 /**
143 * Method called when all work is done -- all workers are finished.
144 */
145 void RecordOptimisationIsFinished(G4bool verbose);
146
147 /**
148 * Waits until the voxelisation is all done.
149 */
150 void WaitForVoxelisationFinish(G4bool verbose = false);
151
152 private:
153
154 /** The list of volumes which threads need to optimise. */
155 std::vector<G4LogicalVolume*> fVolumesToOptimise;
156
157 /** Iterator used by UndertakeOptimisation(). */
158 std::vector<G4LogicalVolume*>::const_iterator fLogVolumeIterator;
159
160 /** Statistics container shared by all workers. */
161 std::vector<G4SmartVoxelStat> fGlobVoxelStats;
162
163 // Flags for parallel initialization
164 // ---------------------------------
165
166 G4bool fVerboseParallel = false;
167 G4bool fParallelVoxelOptimisationUnderway = false; // It has started
168 G4bool fParallelVoxelOptimisationFinished = false; // It is done
169
170 // Statistics for parallel Optimisation - used in 'verbose' mode
171 // ------------------------------------
172
173 /** Counters. */
174 G4double fSumVoxelTime = -9999999.9999;
175 G4int fNumberThreadsReporting = -99999; // Must be set correctly later
176 unsigned long fTotalNumberVolumesOptimised = -9999999;
177
178 /** For Wall Clock time in parallel mode ... */
179 G4Timer* fWallClockTimer = nullptr;
180 G4bool fWallClockStarted = false;
181};
182
183#endif
double G4double
Definition G4Types.hh:83
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4GLOB_DLL std::ostream G4cout
G4LogicalVolume represents a leaf node or unpositioned subtree in the geometry hierarchy....
G4VPhysicalVolume is an abstract base class for the representation of a positioned volume....
void ReportVoxelInfo(G4LogicalVolume *logVolume, std::ostream &os)
void SetVerbosity(G4bool verbose)
static void ReportVoxelStats(std::vector< G4SmartVoxelStat > &stats, G4double totalCpuTime, std::ostream &os=G4cout)
void PrepareParallelOptimisation(G4bool allOpts, G4bool verbose)