#include "TMPIFile.h"
#ifdef TMPI_SECOND_RUN
#include <chrono>
#include <sstream>
void test_tmpi()
{
Int_t events_per_rank = 6;
std::string treename = "test_tmpi";
std::string branchname = "event";
std::stringstream smpifname;
smpifname << "/tmp/merged_output_" << getpid() << ".root";
TMPIFile *newfile = new TMPIFile(smpifname.str().c_str(), "RECREATE", N_collectors);
if (newfile->GetMPIGlobalRank() == 0) {
Info(
"test_tmpi",
" running with parallel ranks: %d", newfile->GetMPIGlobalSize());
Info(
"test_tmpi",
" running with collecting ranks: %d", N_collectors);
Info(
"test_tmpi",
" running with working ranks: %d", (newfile->GetMPIGlobalSize() - N_collectors));
Info(
"test_tmpi",
" running with sync rate: %d", sync_rate);
Info(
"test_tmpi",
" running with events per rank: %d", events_per_rank);
Info(
"test_tmpi",
" running with sleep mean: %d", sleep_mean);
Info(
"test_tmpi",
" running with sleep sigma: %d", sleep_sigma);
Info(
"test_tmpi",
" running with seed: %d",
gRandom->GetSeed());
}
if (newfile->IsCollector()) {
Info(
"Collector",
"[%d]\troot output filename = %s", newfile->GetMPIGlobalRank(), smpifname.str().c_str());
}
if (newfile->IsCollector()) {
newfile->RunCollector();
} else {
TTree *tree =
new TTree(treename.c_str(),
"Event example with Jets");
tree->
Branch(branchname.c_str(),
"JetEvent", &event, 8000, 2);
auto sync_start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < events_per_rank; i++) {
auto start = std::chrono::high_resolution_clock::now();
event->Build(jetm, trackm, hitam, hitbm);
auto evt_built = std::chrono::high_resolution_clock::now();
double build_time = std::chrono::duration_cast<std::chrono::duration<double>>(evt_built -
start).count();
Info(
"Rank",
"[%d] [%d]\tevt = %d;\tbuild_time = %f", newfile->GetMPIColor(), newfile->GetMPILocalRank(), i,
build_time);
auto adjusted_sleep = (
int)(sleep_mean - build_time);
std::this_thread::sleep_for(std::chrono::seconds(
int(
sleep)));
if ((i + 1) % sync_rate == 0) {
newfile->Sync();
auto end = std::chrono::high_resolution_clock::now();
double sync_time = std::chrono::duration_cast<std::chrono::duration<double>>(end - sync_start).count();
Info(
"Rank",
"[%d] [%d]\tevent collection time: %f", newfile->GetMPIColor(), newfile->GetMPILocalRank(),
sync_time);
sync_start = std::chrono::high_resolution_clock::now();
}
}
if (events_per_rank % sync_rate != 0) {
newfile->Sync();
}
}
Info(
"Rank",
"[%d] [%d]\tclosing file", newfile->GetMPIColor(), newfile->GetMPILocalRank());
newfile->Close();
if (newfile->GetMPILocalRank() == 0) {
TString filename = newfile->GetMPIFilename();
Info(
"Rank",
"[%d] [%d]\topening file: %s", newfile->GetMPIColor(), newfile->GetMPILocalRank(), filename.
Data());
if (file.IsOpen()) {
file.ls();
TTree *tree = (
TTree *)file.Get(treename.c_str());
if (tree)
Info(
"Rank",
"[%d] [%d]\tfile should have %d events and has %lld", newfile->GetMPIColor(),
newfile->GetMPILocalRank(), (newfile->GetMPILocalSize() - 1) * events_per_rank, tree->
GetEntries());
}
}
}
void testTMPIFile(
Bool_t secRun)
{
auto start = std::chrono::high_resolution_clock::now();
test_tmpi();
auto end = std::chrono::high_resolution_clock::now();
double time = std::chrono::duration_cast<std::chrono::duration<double>>(end -
start).count();
std::string msg = "Total elapsed time: ";
msg += std::to_string(time);
Info(
"testTMPIFile",
"%s", msg.c_str());
Info(
"testTMPIFile",
"exiting");
}
#else
void testTMPIFile()
{
MPI_Initialized(&flag);
if (!flag) {
MPI_Init(NULL, NULL);
}
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &
size);
if (rank == 0) {
gSystem->Exec(
"cp " + tutdir +
"/tree/JetEvent* .");
gROOT->ProcessLine(
".L JetEvent.cxx+");
}
MPI_Barrier(MPI_COMM_WORLD);
gROOT->ProcessLine(
"#define TMPI_SECOND_RUN yes");
gROOT->ProcessLine(
"#include \"" __FILE__
"\"");
gROOT->ProcessLine(
"testTMPIFile(true)");
MPI_Finalized(&finalized);
if (!finalized) {
MPI_Finalize();
}
}
#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
unsigned int sleep(unsigned int seconds)
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
const char * Data() const
A TTree represents a columnar dataset.
virtual Int_t Fill()
Fill all branches.
void Print(Option_t *option="") const override
Print a summary of the tree contents.
virtual Long64_t GetEntries() const
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
virtual void SetAutoFlush(Long64_t autof=-30000000)
This function may be called at the start of a program to change the default value for fAutoFlush.
RVec< PromoteType< T > > abs(const RVec< T > &v)