Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
G4RootMpiNtupleManager Class Reference

#include <G4RootMpiNtupleManager.hh>

Inheritance diagram for G4RootMpiNtupleManager:
G4RootNtupleManager G4TNtupleManager< tools::wroot::ntuple, G4RootFile > G4BaseNtupleManager G4VNtupleManager G4BaseAnalysisManager

Public Member Functions

 G4RootMpiNtupleManager (const G4AnalysisManagerState &state, std::shared_ptr< G4NtupleBookingManager > bookingManger, G4bool rowWise, G4bool rowMode, tools::impi *impi, G4int mpiSize)
 
virtual ~G4RootMpiNtupleManager ()
 
virtual void CreateNtuplesFromBooking (const std::vector< G4NtupleBooking * > &ntupleBookings) final
 
virtual G4bool Merge () final
 

Private Member Functions

G4RootFile * GetNtupleFile (G4int id)
 
G4bool Send (G4int id, RootNtupleDescription *ntupleDescription)
 
G4bool InitializeRanks ()
 
G4bool WaitBuffer ()
 

Private Attributes

tools::impi * fImpi
 
std::vector< G4int > fSlaveRanks
 
G4int fMainRank
 

Detailed Description

Definition at line 43 of file G4RootMpiNtupleManager.hh.

Constructor & Destructor Documentation

◆ G4RootMpiNtupleManager()

G4RootMpiNtupleManager::G4RootMpiNtupleManager ( const G4AnalysisManagerState state,
std::shared_ptr< G4NtupleBookingManager bookingManger,
G4bool  rowWise,
G4bool  rowMode,
tools::impi *  impi,
G4int  mpiSize 
)

Definition at line 45 of file G4RootMpiNtupleManager.cc.

50 : G4RootNtupleManager(state, bookingManager, 0, 0, rowWise, rowMode),
51 fImpi(impi),
53 fMainRank(0)
54{
55 for ( G4int rank = 0; rank < mpiSize; rank++ ) {
56 fSlaveRanks.push_back(rank);
57 }
58 fMainRank = mpiSize;
59}
std::vector< G4int > fSlaveRanks

◆ ~G4RootMpiNtupleManager()

G4RootMpiNtupleManager::~G4RootMpiNtupleManager ( )
virtual

Definition at line 62 of file G4RootMpiNtupleManager.cc.

63{}

Member Function Documentation

◆ CreateNtuplesFromBooking()

void G4RootMpiNtupleManager::CreateNtuplesFromBooking ( const std::vector< G4NtupleBooking * > &  ntupleBookings)
finalvirtual

Definition at line 302 of file G4RootMpiNtupleManager.cc.

304{
305 // G4cout << "G4RootMpiNtupleManager::CreateNtuplesFromBooking()" << G4endl;
306
307 // Call base class method
309 ntupleBookings);
310
311 // Initialize ranks
312 if ( ! InitializeRanks() ) {
313 G4cerr << "InitializeRanks failed." << G4endl;
314 }
315
316 // Go to wait buffer mode
317 if ( ! WaitBuffer() ) {
318 G4cerr << "WaitBuffer failed." << G4endl;
319 }
320}

◆ Merge()

G4bool G4RootMpiNtupleManager::Merge ( )
finalvirtual

Definition at line 323 of file G4RootMpiNtupleManager.cc.

324{
325 // G4cout << "G4RootMpiNtupleManager::Merge()" << G4endl;
326
327 auto finalResult = true;
328
329 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
330
331 // Do not create ntuple if it is inactivated
332 if ( fState.GetIsActivation() && ( ! ntupleDescription->GetActivation() ) ) continue;
333
334 // G4cout << "Go to call merge_number_of_entries" << G4endl;
335 ntupleDescription->GetNtuple()->merge_number_of_entries();
336 }
337
338 return finalResult;
339}

◆ GetNtupleFile()

G4RootFile * G4RootMpiNtupleManager::GetNtupleFile ( G4int  id)
private

◆ Send()

G4bool G4RootMpiNtupleManager::Send ( G4int  id,
RootNtupleDescription ntupleDescription 
)
private

Definition at line 70 of file G4RootMpiNtupleManager.cc.

71{
72// Pack and send the main ntuple data to the slave ranks
73
74 // G4cout << "Going to send main ntuple data " << G4endl;
75 // G4cout << "ntupleDescription: " << ntupleDescription << G4endl;
76 // G4cout << "ntuple: " << ntupleDescription->fNtuple << G4endl;
77
78 // Get ntuple
79 auto ntuple = ntupleDescription->GetNtuple();
80
81 // Get basket sizes
82 std::vector<tools::wroot::branch*> mainBranches;
83 ntuple->get_branches(mainBranches);
84 std::vector<tools::uint32> basketSizes;
85 tools_vforcit(tools::wroot::branch*, mainBranches, it) {
86 basketSizes.push_back((*it)->basket_size());
87 }
88
89 auto g4RootFile = fFileManager->CreateNtupleFile(ntupleDescription);
90 auto ntupleFile = std::get<0>(*g4RootFile);
91
92 tools::uint32 basketSize = fFileManager->GetBasketSize();
93 unsigned int basketEntries = fFileManager->GetBasketEntries();
94
95 for ( auto slaveRank : fSlaveRanks ) {
96 // G4cout << "Going to send main ntuple data to slave rank " << slaveRank << G4endl;
97
98 fImpi->pack_reset();
99 if ( ! fImpi->pack(id)) {
100 G4cerr << "pack(id) failed." << G4endl;
101 return false;
102 }
103
104 if ( ! fImpi->bpack(fRowWise) ) {
105 G4cerr << "bpack(fRowWise) failed."<< G4endl;
106 return false;
107 }
108
109 if ( ! fImpi->bpack(ntupleFile->byte_swap())) {
110 G4cerr << "bpack(byte_swap) failed." << G4endl;
111 return false;
112 }
113
114 if ( ! fImpi->pack(ntupleFile->compression()) ) {
115 G4cerr << "pack(compression) failed." << G4endl;
116 return false;
117 }
118
119 if ( ! fImpi->pack(ntupleFile->dir().seek_directory())) {
120 // Should be used fNtupleDirectory ??
121 G4cerr << "pack(seek) failed." << G4endl;
122 return false;
123 }
124
125 if ( fRowWise ) {
126 if ( ! fImpi->pack(basketSize) ) {
127 G4cerr << "pack(basketSize) failed." << G4endl;
128 return false;
129 }
130 } else {
131 if ( ! fImpi->bpack(fRowMode) ) {
132 G4cerr << "bpack(fRowMode) failed." << G4endl;
133 return false;
134 }
135 if ( ! fImpi->vpack(basketSizes) ) {
136 G4cerr << "vpack(basketSizes) failed." << G4endl;
137 return false;
138 }
139 if ( ! fImpi->pack(basketEntries) ) {
140 G4cerr << "pack(basketEntries) failed." << G4endl;
141 return false;
142 }
143 }
144
145 if ( ! fImpi->send_buffer(slaveRank, kTAG_NTUPLE )) {
146 G4cerr << "send_buffer() failed." << G4endl;
147 return false;
148 }
149 fImpi->pack_reset();
150
151 G4cout << "Sent ntuple description to slave on rank " << slaveRank << G4endl;
152 }
153
154 // G4cout << "Done: send main ntuple data " << G4endl;
155 return true;
156}
const int kTAG_NTUPLE

◆ InitializeRanks()

G4bool G4RootMpiNtupleManager::InitializeRanks ( )
private

Definition at line 159 of file G4RootMpiNtupleManager.cc.

160{
161 // G4cout << "G4RootMpiNtupleManager::InitializeRanks" << G4endl;
162
163 auto finalResult = true;
164
165 auto counter = 0;
166 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
167
168 // Do not create ntuple if it is inactivated
169 if ( fState.GetIsActivation() && ( ! ntupleDescription->GetActivation() ) ) continue;
170
171 auto result = Send(counter++, ntupleDescription);
172 finalResult = finalResult && result;
173 }
174
175 return finalResult;
176}
G4bool Send(G4int id, RootNtupleDescription *ntupleDescription)

◆ WaitBuffer()

G4bool G4RootMpiNtupleManager::WaitBuffer ( )
private

Definition at line 179 of file G4RootMpiNtupleManager.cc.

180{
181// Receive the pntuple data from the slave ranks
182// For the time being only one ntuple
183
184 // G4cout << "G4RootMpiNtupleManager::WaitBuffer" << G4endl;
185
186 unsigned long numberOfEndFill = 0;
187
188 G4bool verbose = IsVerbose(kVL2);
189
190 while ( true ) {
191 fImpi->pack_reset();
192
193 // loop until receiving end_fill from all ranks
194 // G4cout << "G4RootMpiNtupleManager::WaitBuffer entering loop" << G4endl;
195 int probe_src;
196 if ( ! fImpi->wait_buffer(fMainRank, kTAG_NTUPLE, probe_src, verbose)) {
197 G4cerr << "!!! wait_buffer() failed." << std::endl;
198 return EXIT_FAILURE;
199 }
200
201 tools::uint32 protocol;
202 if ( ! fImpi->unpack(protocol)) {
203 G4cerr << "unpack(protocol) failed."<< G4endl;
204 return false;
205 }
206
207 if ( protocol == tools::wroot::mpi_protocol_basket() ) {
208
209 // G4cout << "G4RootMpiNtupleManager::WaitBuffer got protocol_basket" << G4endl;
210
211 // get ntuple Id
212 tools::uint32 ntupleId;
213 if ( ! fImpi->unpack(ntupleId) ) {
214 G4cerr << "unpack(ntuple_id) failed."<< std::endl;
215 return false;
216 }
217
218 if ( ntupleId >= fNtupleVector.size() ) {
219 std::cerr << "!!! unknown ntupleId " << ntupleId << std::endl;
220 return false;
221 }
222
223 // Main ntuple
224 auto mainNtuple = fNtupleVector[ntupleId];
225
226 // add basket to main ntuple
227 if ( ! mainNtuple->mpi_add_basket(*fImpi)) {
228 std::cerr << "mainNtuple->mpi_add_basket() failed." << std::endl;
229 return EXIT_FAILURE;
230 }
231 }
232 else if ( protocol == tools::wroot::mpi_protocol_baskets() ) {
233 //column_wise and row_mode only.
234
235 // G4cout << "G4RootMpiNtupleManager::WaitBuffer got protocol_baskets" << G4endl;
236
237 // get ntuple Id
238 tools::uint32 ntupleId;
239 if ( ! fImpi->unpack(ntupleId) ) {
240 G4cerr << "unpack(ntuple_id) failed."<< std::endl;
241 return false;
242 }
243
244 if ( ntupleId >= fNtupleVector.size() ) {
245 std::cerr << "!!! unknown ntupleId " << ntupleId << std::endl;
246 return false;
247 }
248
249 // Main ntuple
250 auto mainNtuple = fNtupleVector[ntupleId];
251
252 // add basket to main ntuple
253 if ( ! mainNtuple->mpi_add_baskets(*fImpi)) {
254 std::cerr << "mainNtuple->mpi_add_baskets() failed." << std::endl;
255 return EXIT_FAILURE;
256 }
257 }
258 else if ( protocol==tools::wroot::mpi_protocol_end_fill() ) {
259
260 // G4cout << "G4RootMpiNtupleManager::WaitBuffer got protocol_end_fill" << G4endl;
261
262 // get ntuple Id
263 tools::uint32 ntupleId;
264 if ( ! fImpi->unpack(ntupleId) ) {
265 G4cerr << "unpack(ntuple_id) failed."<< std::endl;
266 return false;
267 }
268
269 if ( ntupleId >= fNtupleVector.size() ) {
270 std::cerr << "!!! unknown ntupleId " << ntupleId << std::endl;
271 return false;
272 }
273
274 // Main ntuple
275 auto mainNtuple = fNtupleVector[ntupleId];
276
277 // end_fill in main ntuple
278 if ( ! mainNtuple->mpi_end_fill(*fImpi) ) {
279 G4cerr << "main_ntuple->mpi_end_fill() failed." << std::endl;
280 return false;
281 }
282
283 numberOfEndFill++;
284
285 if ( numberOfEndFill == fSlaveRanks.size() ) break;
286 }
287 else {
288 G4cerr << "unknown protocol " << protocol << G4endl;
289 return false;
290 }
291 }
292
293 return true;
294}

Member Data Documentation

◆ fImpi

tools::impi* G4RootMpiNtupleManager::fImpi
private

Definition at line 65 of file G4RootMpiNtupleManager.hh.

◆ fSlaveRanks

std::vector<G4int> G4RootMpiNtupleManager::fSlaveRanks
private

Definition at line 66 of file G4RootMpiNtupleManager.hh.

◆ fMainRank

G4int G4RootMpiNtupleManager::fMainRank
private

Definition at line 67 of file G4RootMpiNtupleManager.hh.


The documentation for this class was generated from the following files:

Applications | User Support | Publications | Collaboration