Loading...
Searching...
No Matches
G4RootMpiPNtupleManager.cc
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
27// Author: Ivana Hrivnacova, 21/11/2018 (ivana@ipno.in2p3.fr)
28
30#include "G4RootFileManager.hh"
31#include "G4AnalysisUtilities.hh"
32
33#include "tools/wroot/file"
34#include "tools/wroot/mpi_ntuple_row_wise"
35#include "tools/wroot/mpi_ntuple_column_wise"
36
37using namespace G4Analysis;
38
39const int kTAG_NTUPLE = 1004; // This constant is defined in G4MPImanager
40 // (should be passed from the application)
41namespace {
42
43//_____________________________________________________________________________
44void NotExistException(const G4String& what, G4int id, const G4String& functionName)
45{
46 G4String inFunction = "G4RootMpiPNtupleManager::";
47 inFunction += functionName;
48 G4ExceptionDescription description;
49 description << what << " id= " << id << " does not exist.";
50 G4Exception(inFunction, "Analysis_W011", JustWarning, description);
51}
52
53}
54
55//_____________________________________________________________________________
57 const G4AnalysisManagerState& state,
58 tools::impi* impi, G4int mpiRank, G4int destinationRank)
59 : G4BaseNtupleManager(state),
60 fNtupleVector(),
61 fImpi(impi),
62 fMpiRank(mpiRank),
63 fDestinationRank(destinationRank)
64{
65}
66
67//_____________________________________________________________________________
69{
70 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
71 delete ntupleDescription;
72 }
73}
74
75//
76// private functions
77//
78
79//_____________________________________________________________________________
82 G4int id, G4String functionName, G4bool warn) const
83{
84 auto index = id - fFirstId;
85 if ( index < 0 || index >= G4int(fNtupleDescriptionVector.size()) ) {
86 if ( warn) {
87 NotExistException("ntuple description", id, functionName);
88 }
89 return nullptr;
90 }
91
92 return fNtupleDescriptionVector[index];
93}
94
95//_____________________________________________________________________________
97 G4int id, G4String functionName, G4bool warn) const
98{
99 auto ntupleDescription = GetNtupleDescriptionInFunction(id, functionName);
100 if ( ! ntupleDescription ) return nullptr;
101
102 if ( ! ntupleDescription->GetBasePNtuple() ) {
103 if ( warn ) {
104 NotExistException("ntuple", id, functionName);
105 }
106 return nullptr;
107 }
108 return ntupleDescription->GetBasePNtuple();
109}
110
111//
112// protected functions
113//
114
115//_____________________________________________________________________________
117{
118 Message(kVL4, "create from booking", "mpi pntuple",
119 ntupleDescription->GetDescription().GetNtupleBooking().name());
120
121 // Wait for the ntuple data from main
122
123 G4bool verbose = IsVerbose(kVL2);
124
125 G4cout << "Go to wait_buffer from " << fDestinationRank << G4endl;
126 fImpi->pack_reset();
127 int probe_src;
128 if ( ! fImpi->wait_buffer(fMpiRank, fDestinationRank, kTAG_NTUPLE, probe_src, verbose)) {
129 G4cerr << "G4RootMpiPNtupleManager::CreateNtuple: wait_buffer() failed."<< G4endl;
130 return;
131 }
132 G4cout << "After wait_buffer with " << fImpi << G4endl;
133
134 tools::uint32 mainNtupleId;
135 bool rowWise;
136 bool byteSwap;
137 unsigned int compression;
138 tools::wroot::seek seekDirectory;
139 tools::uint32 basketSize;
140 bool rowMode;
141 std::vector<tools::uint32> basketSizes;
142 unsigned int basketEntries;
143
144 if ( ! fImpi->unpack(mainNtupleId) ) {
145 G4cerr << "bunpack(byteSwap) failed."<< G4endl;
146 return;
147 }
148 // G4cout << "unpack 1" << G4endl;
149
150 if ( ! fImpi->bunpack(rowWise) ) {
151 G4cerr << "bunpack(rowWise) failed."<< G4endl;
152 return;
153 }
154 // G4cout << "unpack 1/2" << G4endl;
155
156 if ( ! fImpi->bunpack(byteSwap) ) {
157 G4cerr << "bunpack(byteSwap) failed."<< G4endl;
158 return;
159 }
160 // G4cout << "unpack 2" << G4endl;
161
162 if ( ! fImpi->unpack(compression) ) {
163 G4cerr << "unpack(compression) failed."<< G4endl;
164 return;
165 }
166 // G4cout << "unpack 3" << G4endl;
167
168 if ( ! fImpi->unpack(seekDirectory) ) {
169 G4cerr << "unpack(seek) failed."<< G4endl;
170 return;
171 }
172 // G4cout << "unpack 4" << G4endl;
173
174 if (rowWise) {
175 if ( ! fImpi->unpack(basketSize) ) {
176 G4cerr << "unpack(basketSize) failed."<< G4endl;
177 return;
178 }
179 } else {
180 if ( ! fImpi->bunpack(rowMode) ) {
181 G4cerr << "bpack(rowMode) failed." << G4endl;
182 return;
183 }
184 if ( ! fImpi->vunpack(basketSizes) ) {
185 G4cerr << "vunpack(basketSizes) failed."<< G4endl;
186 return;
187 }
188 if( ! fImpi->unpack(basketEntries) ) {
189 G4cerr << "unpack(basketEntries) failed." << G4endl;
190 return;
191 }
192 }
193 // G4cout << "unpack 5" << G4endl;
194
195 // G4cout << "rank = " << fMpiRank
196 // << ", verbose = " << verbose
197 // << ", mainNtupleId = " << mainNtupleId
198 // << ", byteSwap = " << byteSwap
199 // << ", compression = " << compression
200 // << ", seekDirectory = " << seekDirectory
201 // << ", basketSizes.size() = " << basketSizes.size()
202 // << G4endl;
203
204 // Create MPI pntuple
205 if ( rowWise ) {
206 tools::wroot::mpi_ntuple_row_wise* ntuple
207 = new tools::wroot::mpi_ntuple_row_wise(
208 mainNtupleId, G4cout, byteSwap, compression, seekDirectory,
209 basketSize, ntupleDescription->GetDescription().GetNtupleBooking(), verbose);
210 ntupleDescription->SetNtuple(ntuple);
211 ntupleDescription->SetBasePNtuple(ntuple);
212 } else {
213 tools::wroot::mpi_ntuple_column_wise* ntuple
214 = new tools::wroot::mpi_ntuple_column_wise(
215 mainNtupleId, G4cout, byteSwap, compression, seekDirectory,
216 basketSizes, ntupleDescription->GetDescription().GetNtupleBooking(),
217 rowMode, basketEntries, verbose);
218 ntupleDescription->SetNtuple(ntuple);
219 ntupleDescription->SetBasePNtuple(ntuple);
220 }
221
222 ntupleDescription->GetDescription().SetIsNtupleOwner(true);
223 ntupleDescription->SetImpi(fImpi);
224 // should be not needed
225 // pntuple object is not deleted automatically
226 fNtupleVector.push_back(ntupleDescription->GetNtuple());
227
228 Message(kVL3, "create from booking", "mpi pntuple",
229 ntupleDescription->GetDescription().GetNtupleBooking().name());
230}
231
232//_____________________________________________________________________________
234 const std::vector<G4NtupleBooking*>& ntupleBookings)
235{
236// Create ntuple from ntuple_booking & the buffer from main rank
237
238 // G4cout << "Start G4RootMpiPNtupleManager::CreateNtuplesFromBooking" << G4endl;
239
240 // Do not create ntuples if NtupleVector is not empty
241 if ( fNtupleVector.size() ) return;
242
243 // Create pntuple descriptions from ntuple booking.
244 // auto g4NtupleBookings = fBookingManager->GetNtupleBookingVector();
245 for ( auto g4NtupleBooking : ntupleBookings ) {
246 auto ntupleDescription = new G4RootMpiPNtupleDescription(g4NtupleBooking);
247 ntupleDescription->SetMainNtupleRank(fDestinationRank);
248 fNtupleDescriptionVector.push_back(ntupleDescription);
249 }
250
251 // Create mpi ntuples
252 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
253
254 // Do not create ntuple if it is inactivated
255 if ( fState.GetIsActivation() && ( ! ntupleDescription->GetDescription().GetActivation() ) ) continue;
256
257 // Do not create ntuple if it already exists
258 if ( ntupleDescription->GetNtuple() ) continue;
259
260 Message(kVL4, "create from booking", "mpi pntuple",
261 ntupleDescription->GetDescription().GetNtupleBooking().name());
262
263 // create ntuple
264 CreateNtuple(ntupleDescription);
265
266 // finish created ntuple
267 // (nothing to be done ?)
268 // FinishTNtuple(ntupleDescription);
269
270 Message(kVL3, "create from booking", "mpi pntuple",
271 ntupleDescription->GetDescription().GetNtupleBooking().name());
272 }
273
274}
275
276//_____________________________________________________________________________
278 // const G4String& name, const G4String& title)
279{
280// Create pntuple description from g4 ntuple booking
281// Nothing to be done here.
282
283 return G4Analysis::kInvalidId;
284}
285
286//_____________________________________________________________________________
288 G4int ntupleId, G4int columnId, G4int value)
289{
290 return FillNtupleTColumn<int>(ntupleId, columnId, value);
291}
292
293//_____________________________________________________________________________
295 G4int ntupleId, G4int columnId, G4float value)
296{
297 return FillNtupleTColumn<float>(ntupleId, columnId, value);
298}
299
300//_____________________________________________________________________________
302 G4int ntupleId, G4int columnId, G4double value)
303{
304 return FillNtupleTColumn<double>(ntupleId, columnId, value);
305}
306
307//_____________________________________________________________________________
309 G4int ntupleId, G4int columnId, const G4String& value)
310{
311 return FillNtupleTColumn<std::string>(ntupleId, columnId, value);
312}
313
314//_____________________________________________________________________________
316{
317 if ( fState.GetIsActivation() && ( ! GetActivation(ntupleId) ) ) {
318 //G4cout << "Skipping AddNtupleRow for " << ntupleId << G4endl;
319 return false;
320 }
321
322 Message(kVL4, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
323
324 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "AddNtupleRow");
325 if ( ! ntupleDescription ) return false;
326
327 // if(!_ntuple->add_row(_impi,rank_src,tag)) {
328 auto result
329 = ntupleDescription->GetNtuple()
330 ->add_row(*fImpi, ntupleDescription->GetMainNtupleRank(), kTAG_NTUPLE );
331
332 if ( ! result ) {
333 G4ExceptionDescription description;
334 description << " " << " ntupleId " << ntupleId
335 << "adding row has failed.";
336 G4Exception("G4RootMpiPNtupleManager::AddNtupleRow()",
337 "Analysis_W002", JustWarning, description);
338 }
339
340 Message(kVL3, "add", "pntuple row", " ntupleId " + to_string(ntupleId));
341
342 return true;
343}
344
345//_____________________________________________________________________________
347{
348
349 // G4cout << "Start G4RootMpiPNtupleManager::Merge" << G4endl;
350
351 auto finalResult = true;
352
353 for ( auto ntupleDescription : fNtupleDescriptionVector) {
354
355 // skip inactivated ntuples
356 if ( ! ntupleDescription->GetDescription().GetActivation() ) continue;
357
358 // skip if ntuple was already merged and deleted
359 // (this happend when the main rank re-opens a new file for merged data)
360 if ( ! ntupleDescription->GetNtuple() ) continue;
361
362 Message(kVL4, "merge", "pntuple", ntupleDescription->GetDescription().GetNtupleBooking().name());
363
364 // G4cout << "call end_fill " << fImpi
365 // << " to rank " << ntupleDescription->GetMainNtupleRank()
366 // << " pntuple: " << ntupleDescription->GetNtuple() << G4endl;
367 auto result
368 = ntupleDescription->GetNtuple()
369 ->end_fill(*fImpi, ntupleDescription->GetMainNtupleRank(), kTAG_NTUPLE);
370
371 if ( ! result ) {
372 G4ExceptionDescription description;
373 description << " " << " ntuple " << ntupleDescription->GetDescription().GetNtupleBooking().name()
374 << "end fill has failed.";
375 G4Exception("G4RootMpiPNtupleManager::Merge()",
376 "Analysis_W002", JustWarning, description);
377 }
378
379 delete ntupleDescription->GetNtuple();
380 ntupleDescription->SetNtuple(nullptr);
381
382 Message(kVL3, "merge", "pntuple", ntupleDescription->GetDescription().GetNtupleBooking().name());
383 }
384
385 return finalResult;
386}
387
388//_____________________________________________________________________________
390{
391 // Reset ntuple description, this will delete ntuple if present and
392 // we have its ownership.
393 // The ntuples will be recreated with new cycle or new open file.
394
395 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
396 ntupleDescription->GetDescription().Reset();
397 }
398
399 fNtupleVector.clear();
400
401 return true;
402}
403
404//_____________________________________________________________________________
406{
407 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
408 delete ntupleDescription->GetNtuple();
409 }
410
412 fNtupleVector.clear();
413
414 Message(kVL2, "clear", "pntuples");
415}
416
417//_____________________________________________________________________________
419{
420 if ( IsVerbose(G4Analysis::kVL4) ) {
421 Message(G4Analysis::kVL4, "delete", "pntuple ntupleId " + to_string(id));
422 }
423
424 auto ntupleDescription = GetNtupleDescriptionInFunction(id, "Delete", true);
425
426 if (ntupleDescription == nullptr) return false;
427
428 // Delete/clear ntuple data
429 delete ntupleDescription->GetNtuple();
430 ntupleDescription->SetNtuple(nullptr);
431 ntupleDescription->SetBasePNtuple(nullptr);
432 ntupleDescription->GetMainBranches().clear();
433
434 // Update ntuple vector
435 auto index = id - GetFirstId();
436 fNtupleVector[index] = nullptr;
437
438 Message(G4Analysis::kVL2, "delete", "pntuple ntupleId " + to_string(id));
439
440 return true;
441}
442
443//_____________________________________________________________________________
444
446 G4bool activation)
447{
448 for ( auto ntupleDescription : fNtupleDescriptionVector ) {
449 ntupleDescription->GetDescription().SetActivation(activation);
450 }
451}
452
453//_____________________________________________________________________________
454
456 G4int ntupleId, G4bool activation)
457{
458 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetActivation");
459 if ( ! ntupleDescription ) return;
460
461 ntupleDescription->GetDescription().SetActivation(activation);
462}
463
464//_____________________________________________________________________________
466 G4int ntupleId) const
467{
468 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "GetActivation");
469 if ( ! ntupleDescription ) return false;
470
471 return ntupleDescription->GetDescription().GetActivation();
472}
473
474//_____________________________________________________________________________
476{
477 fNewCycle = value;
478}
479
480//_____________________________________________________________________________
482{
483 return fNewCycle;
484}
485
486//_____________________________________________________________________________
488{
489 return fNtupleVector.size();
490}
const int kTAG_NTUPLE
const int kTAG_NTUPLE
void SetNtuple(tools::wroot::impi_ntuple *intuple)
void SetBasePNtuple(tools::wroot::base_pntuple *basePNtuple)
tools::wroot::impi_ntuple * GetNtuple() const
virtual G4bool GetActivation(G4int ntupleId) const final
virtual G4bool FillNtupleDColumn(G4int ntupleId, G4int columnId, G4double value) final
virtual G4bool AddNtupleRow(G4int ntupleId) final
virtual G4bool FillNtupleIColumn(G4int ntupleId, G4int columnId, G4int value) final
std::vector< tools::wroot::impi_ntuple * > fNtupleVector
tools::wroot::base_pntuple * GetNtupleInFunction(G4int id, G4String function, G4bool warn=true) const
G4RootMpiPNtupleManager(const G4AnalysisManagerState &state, tools::impi *impi, G4int mpiRank, G4int destinationRank)
void CreateNtuple(G4RootMpiPNtupleDescription *ntupleDescription)
G4RootMpiPNtupleDescription * GetNtupleDescriptionInFunction(G4int id, G4String function, G4bool warn=true) const
virtual G4bool FillNtupleSColumn(G4int ntupleId, G4int columnId, const G4String &value) final
virtual G4bool FillNtupleFColumn(G4int ntupleId, G4int columnId, G4float value) final
virtual void SetActivation(G4bool activation) final
std::vector< G4RootMpiPNtupleDescription * > fNtupleDescriptionVector
virtual G4int GetNofNtuples() const final
void SetNewCycle(G4bool value) final
void CreateNtuplesFromBooking(const std::vector< G4NtupleBooking * > &ntupleBookings)

Applications | User Support | Publications | Collaboration