53#define INT_MAX std::numeric_limits<int>::max()
69 : fValue(nullptr), fDim(
dim), fBins(bins), fNum(num), fSparse(nullptr), fHist(nullptr), fHn(nullptr)
74 Double_t GetTime(EHist hist, ETime time)
const
77 return fTime[hist][0];
78 return fTime[hist][1];
80 static void SetDebug(
Int_t lvl) { fgDebug = lvl; }
81 THnSparse *GetSparse()
const {
return fSparse; }
84 void Fill(EHist hist);
86 void SetupHist(EHist hist);
102Int_t TTimeHists::fgDebug = 0;
104TTimeHists::~TTimeHists()
112bool TTimeHists::Run()
118 for (
int h = 0;
h < 2; ++
h) {
129 check[
h] = Check((EHist)
h);
134 fTime[
h][0] = (1. * fNum * rep[
h]) / w.RealTime() / 1E6;
135 fTime[
h][1] = (1. * fNum * rep[
h]) / w.CpuTime() / 1E6;
137 if (
h == 1 && (fTime[
h][0] > 1E20 || fTime[
h][1] > 1E20)) {
145 }
while (w.RealTime() < 0.1);
147 fTime[
h][0] = (1. * fNum * rep[
h]) / w.RealTime() / 1E6;
148 fTime[
h][1] = (1. * fNum * rep[
h]) / w.CpuTime() / 1E6;
151 if (fTime[
h][0] > 1E20)
153 if (fTime[
h][1] > 1E20)
155 }
catch (std::exception &) {
156 fTime[
h][0] = fTime[
h][1] = -1.;
161 if (check[0] != check[1])
163 printf(
"ERROR: mismatch of histogram (%g) and sparse histogram (%g) for dim=%d, bins=%d!\n", check[0],
164 check[1], fDim, fBins);
168 return (check[0] == check[1]);
171void TTimeHists::NextValues()
177void TTimeHists::SetupValues()
185void TTimeHists::Fill(EHist hist)
190 printf(
"%ld: fill %s",
n, hist == kHist ? (fDim < 4 ?
"hist" :
"arr") :
"sparse");
192 printf(
"[%g]", fValue[
d]);
197 case 1: fHist->Fill(fValue[0]);
break;
198 case 2: ((
TH2F *)fHist)->Fill(fValue[0], fValue[1]);
break;
199 case 3: ((
TH3F *)fHist)->Fill(fValue[0], fValue[1], fValue[2]);
break;
200 default: fHn->Fill(fValue);
break;
203 fSparse->Fill(fValue);
208void TTimeHists::SetupHist(EHist hist)
212 case 1: fHist =
new TH1F(
"h1",
"h1", fBins, -1., 1.);
break;
213 case 2: fHist =
new TH2F(
"h2",
"h2", fBins, -1., 1., fBins, -1., 1.);
break;
214 case 3: fHist =
new TH3F(
"h3",
"h3", fBins, -1., 1., fBins, -1., 1., fBins, -1., 1.);
break;
222 throw std::bad_alloc();
226 throw std::bad_alloc();
251Double_t TTimeHists::Check(EHist hist)
256 memset(
x, 0,
sizeof(
Int_t) * fDim);
263 while (
x[0] <= fBins + 1) {
268 histidx = fHist->GetBin(
x[0],
x[1]);
270 histidx = fHist->GetBin(
x[0],
x[1],
x[2]);
271 v = fHist->GetBinContent(histidx);
273 v = fHn->GetBinContent(
x);
280 if (fgDebug > 2 || (fgDebug > 1 &&
v)) {
281 printf(
"%s%d", fDim < 4 ?
"hist" :
"arr", fDim);
283 printf(
"[%d]",
x[
d]);
284 printf(
" = %g\n",
v);
290 for (
Int_t d = fDim - 1;
d > 0; --
d) {
291 if (
x[
d] > fBins + 1) {
299 for (
Long64_t i = 0; i < fSparse->GetNbins(); ++i) {
307 printf(
"sparse%d", fDim);
309 printf(
"[%d]",
x[
d]);
310 printf(
" = %g\n",
v);
316 printf(
"check %s%d = %g\n", hist == kHist ? (fDim < 4 ?
"hist" :
"arr") :
"sparse", fDim, check);
320void hist103_THnSparse_hist()
324#if defined(__CLING__)
325 printf(
"Please run this script in compiled mode by running \".x hist103_THnSparse_hist.C+\"\n");
329 TH2F *htime[TTimeHists::kNumHist][TTimeHists::kNumTime];
330 for (
int h = 0;
h < TTimeHists::kNumHist; ++
h)
331 for (
int t = 0; t < TTimeHists::kNumTime; ++t) {
341 title.Form(
"Throughput (fill,get) %s (%s, 1M entries/sec);dim;bins;1M entries/sec",
342 h == 0 ?
"TH1/2/3/nF" :
"THnSparseF", t == 0 ?
"real" :
"CPU");
343 htime[
h][t] =
new TH2F(
name, title, 6, 0.5, 6.5, 10, 5, 105);
347 new TH2F(
"hsparse_mem",
"Fractional memory usage;dim;bins;fraction of memory used", 6, 0.5, 6.5, 10, 5, 105);
348 TH2F *hsparse_bins =
new TH2F(
"hsparse_bins",
"Fractional number of used bins;dim;bins;fraction of filled bins", 6,
349 0.5, 6.5, 10, 5, 105);
354 printf(
"Processing dimension %d", dim);
355 for (
Int_t bins = 10; bins <= 100; bins += 10) {
356 TTimeHists timer(dim, bins, 1000);
358 for (
int h = 0;
h < TTimeHists::kNumHist; ++
h) {
359 for (
int t = 0; t < TTimeHists::kNumTime; ++t) {
360 Double_t time = timer.GetTime((TTimeHists::EHist)
h, (TTimeHists::ETime)t);
362 htime[
h][t]->
Fill(dim, bins, time);
365 hsparse_mem->
Fill(dim, bins, timer.GetSparse()->GetSparseFractionMem());
366 hsparse_bins->
Fill(dim, bins, timer.GetSparse()->GetSparseFractionBins());
368 if (max < timer.GetTime(TTimeHists::kSparse, TTimeHists::kReal))
369 max = timer.GetTime(TTimeHists::kSparse, TTimeHists::kReal);
380 TH2F *htime_ratio[TTimeHists::kNumTime];
381 for (
int t = 0; t < TTimeHists::kNumTime; ++t) {
382 const char *
name = t ?
"htime_ratio" :
"htime_ratio_r";
383 htime_ratio[t] = (
TH2F *)htime[TTimeHists::kSparse][t]->Clone(
name);
385 title.Form(
"Relative speed improvement (%s, 1M entries/sec): sparse/hist;dim;bins;#Delta 1M entries/sec",
386 t == 0 ?
"real" :
"CPU");
388 htime_ratio[t]->
Divide(htime[TTimeHists::kHist][t]);
393 TFile *
f =
new TFile(
"sparsehist.root",
"RECREATE");
398 gStyle->SetPalette(8,
nullptr);
399 gStyle->SetPaintTextFormat(
".2g");
401 const char *opt =
"TEXT COL";
403 for (
int t = 0; t < TTimeHists::kNumTime; ++t) {
404 for (
int h = 0;
h < TTimeHists::kNumHist; ++
h) {
407 canv->
cd(1 +
h + 3 * t);
408 htime[
h][t]->
Draw(opt);
412 htime_ratio[t]->
Draw(opt);
414 htime_ratio[t]->
Write();
418 hsparse_mem->
Draw(opt);
420 hsparse_bins->
Draw(opt);
421 hsparse_mem->
Write();
422 hsparse_bins->
Write();
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
float Float_t
Float 4 bytes (float).
THnSparseT< TArrayF > THnSparseF
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
1-D histogram with a float per channel (see TH1 documentation)
void SetTitle(const char *title) override
Change/set the title.
virtual void SetMaximum(Double_t maximum=-1111)
void Draw(Option_t *option="") override
Draw this histogram with options.
virtual void SetMinimum(Double_t minimum=-1111)
virtual Bool_t Divide(TF1 *f1, Double_t c1=1)
Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2),...
2-D histogram with a float per channel (see TH1 documentation)
Int_t Fill(Double_t) override
Invalid Fill method.
3-D histogram with a float per channel (see TH1 documentation)
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
__device__ AFloat max(AFloat x, AFloat y)
void Fill(float *output, float value, int size)