ndmspc 0.20240624.0
Loading...
Searching...
No Matches
HnSparseStress.cxx
1#include <TAxis.h>
2#include <TRandom.h>
3
4#include "ndmspc.h"
5#include "HnSparse.h"
6
7#include "HnSparseStress.h"
8
10ClassImp(NDH::HnSparseStress);
12
13namespace NDH {
14
15HnSparseStress::HnSparseStress() : TObject() {}
16
17Bool_t HnSparseStress::Generate(THnSparse * h, Long64_t size, Long64_t start)
18{
22
23 if (h == nullptr) return kFALSE;
24
25 if (size == 0) fNFilledMax = kMaxLong64;
26
27 fNFilledMax = size;
28 if (fDebugLevel > 0)
29 Printf("dimensions=%d chunkSize=%d nFillMax=%lld start=%lld", h->GetNdimensions(), h->GetChunkSize(),
30 fNFilledMax, start);
31
32 Double_t cCenter[h->GetNdimensions()];
33 Int_t cStart[h->GetNdimensions()];
34
35 if (start > 0) {
36 Long64_t allBins = 1;
37 for (Int_t i = 0; i < h->GetNdimensions(); ++i) {
38
39 if (allBins > kMaxLong64 / h->GetAxis(i)->GetNbins()) {
40 Printf("Error: Product of all bins is higer then %lld !!! Do not use --start in this case !!!",
41 kMaxLong64);
42 return kFALSE;
43 }
44 allBins *= h->GetAxis(i)->GetNbins();
45 }
46 if (fDebugLevel > 0) Printf("MaxNumberOfBins=%lld", allBins);
47
48 Long64_t startIndex = start;
49 Int_t bx = 0;
50 for (Int_t i = h->GetNdimensions() - 1; i >= 0; --i) {
51 allBins /= h->GetAxis(i)->GetNbins();
52
53 bx = startIndex / allBins;
54 startIndex -= (bx * allBins);
55 cStart[i] = bx;
56 if (fDebugLevel > 0)
57 Printf("i=%d x=%d startIndex=%lld allBins=%lld cStart[%d]=%d", i, bx, startIndex, allBins, i,
58 cStart[i]);
59 }
60
61 if (fDebugLevel > 0)
62 for (Int_t i = h->GetNdimensions() - 1; i >= 0; --i) {
63 Printf("i=%d %d", i, cStart[i]);
64 }
65 }
66 else {
67 for (Int_t i = 0; i < h->GetNdimensions(); ++i) {
68 cStart[i] = 0;
69 }
70 }
71
72 fTimerTotal.Start();
73 fTimer.Start();
74 Printf("fNFilledMax=%lld filled=%lld", fNFilledMax, h->GetNbins());
75 GenerateRecursiveLoop(h, h->GetNdimensions() - 1, cCenter, cStart);
76 fTimer.Stop();
77 fTimerTotal.Stop();
78 fTimerTotal.Print("m");
79
80 return kTRUE;
81}
82
83bool HnSparseStress::GenerateRecursiveLoop(THnSparse * h, Int_t iDim, Double_t * coord, Int_t * start)
84{
88
89 if (iDim < 0) return true;
90 for (Int_t iBin = start[iDim] + 1; iBin <= h->GetAxis(iDim)->GetNbins(); iBin++) {
91 coord[iDim] = h->GetAxis(iDim)->GetBinCenter(iBin);
92 if (fDebugLevel > 1) Printf("iDim=%d iBin=%d center=%f", iDim, iBin, coord[iDim]);
93
94 if (iDim == 0) {
95 if (fRandomFill) {
96 for (Int_t iAxis = 0; iAxis < h->GetNdimensions(); iAxis++) {
97 coord[iAxis] = gRandom->Uniform(h->GetAxis(iAxis)->GetXmin(), h->GetAxis(iAxis)->GetXmax());
98 }
99 }
100 h->Fill(coord);
101 Long64_t filled = h->GetNbins();
102 if (fPrintRefresh > 0 && filled % fPrintRefresh == 0)
103 PrintBin(h->GetNdimensions(), coord,
104 TString::Format("%03.2f MB filled=%e [chunkSize=%e nChunks=%d]",
105 sizeof(Double_t) * (Double_t)filled / (1024 * 1024), (Double_t)filled,
106 (Double_t)h->GetChunkSize(), h->GetNChunks())
107 .Data());
108 if (fNFilledMax > 0 && filled >= fNFilledMax) return true;
109 }
110 else {
111 if (fDebugLevel > 1) Printf("iDim=%d", iDim);
112 coord[iDim] = h->GetAxis(iDim)->GetBinCenter(iBin);
113 bool finished = GenerateRecursiveLoop(h, iDim - 1, coord, start);
114 if (finished) return true;
115 }
116
117 if (iBin == h->GetAxis(iDim)->GetNbins()) start[iDim] = 0;
118 }
119
120 return false;
121}
122
123bool HnSparseStress::StressRecursiveLoop(HnSparse * h, Int_t & iDim, Int_t * coord)
124{
128
129 Long64_t nBinsSizeBytes = sizeof(Double_t) * h->GetNbins();
130 if (fNBytesMax > 0 && nBinsSizeBytes > fNBytesMax) return true;
131
132 if (iDim >= h->GetNdimensions()) return true;
133 if (coord[h->GetNdimensions() - 1] > h->GetAxis(h->GetNdimensions() - 1)->GetNbins()) {
134 return true;
135 }
136
137 if (nBinsSizeBytes > 0 && nBinsSizeBytes % (10 * 1024 * 1024) == 0) {
138 Printf("%03.2f MB [chunks=%d binsFilled=%lld]", (Double_t)nBinsSizeBytes / (1024 * 1024), h->GetNChunks(),
139 h->GetNbins());
140 fTimer.Stop();
141 fTimer.Print("m");
142 fTimer.Reset();
143 fTimer.Start();
144 }
145
146 // Printf("iDim=%d nBins=%d", iDim, GetAxis(iDim)->GetNbins());
147 if (coord[iDim] < h->GetAxis(iDim)->GetNbins()) {
148 coord[iDim]++;
149 coord[iDim - 1] = 0;
150 iDim = 0;
151 return false;
152 }
153
154 return StressRecursiveLoop(h, ++iDim, coord);
155}
156Bool_t HnSparseStress::Stress(HnSparse * h, Long64_t size, bool bytes)
157{
161
162 if (h == nullptr) return kFALSE;
163
164 Long64_t nFill = size;
165 if (bytes) {
166 fNBytesMax = size;
167 nFill = kMaxLong64;
168 }
169 Printf("dimensions=%d chunkSize=%d nFill=%lld maxSize=%lld", h->GetNdimensions(), h->GetChunkSize(), nFill,
170 fNBytesMax);
171
172 Int_t c[h->GetNdimensions()];
173 Double_t cCenter[h->GetNdimensions()];
174 for (Int_t i = 0; i < h->GetNdimensions(); ++i) {
175 c[i] = 100;
176 }
177
178 fTimerTotal.Start();
179 fTimer.Start();
180 Bool_t finish = kFALSE;
181 Int_t dim;
182 for (Long64_t iFill = 0; iFill < nFill; ++iFill) {
183 dim = 0;
184 finish = StressRecursiveLoop(h, dim, c);
185 if (finish) break;
186
187 PrintBin(h->GetNdimensions(), cCenter, "Hello");
188 h->GetBin(cCenter);
189 }
190 fTimer.Stop();
191 fTimerTotal.Stop();
192 fTimerTotal.Print("m");
193
194 return kTRUE;
195}
196void HnSparseStress::PrintBin(Int_t n, Double_t * c, const char * msg)
197{
201
202 std::string s = "[";
203 for (Int_t i = 0; i < n; ++i) {
204 s.append(TString::Format("%.3f,", c[i]).Data());
205 }
206 s.resize(s.size() - 1);
207 s.append("] : ");
208 s.append(msg);
209
210 Printf("%s", s.data());
211}
212
213} // namespace NDH
HnSparseStress object.
HnSparse object.
Definition HnSparse.h:19