-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathDetSetNewTS_t.cpp
302 lines (257 loc) · 7.83 KB
/
DetSetNewTS_t.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "Utilities/Testing/interface/CppUnit_testdriver.icpp"
#include "cppunit/extensions/HelperMacros.h"
#include "DataFormats/Common/interface/DetSetNew.h"
#include "DataFormats/Common/interface/DetSetVectorNew.h"
#include "DataFormats/Common/interface/DetSetAlgorithm.h"
#include "DataFormats/Common/interface/DetSet2RangeMap.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include <vector>
#include <algorithm>
#include <mutex>
typedef std::mutex Mutex;
// typedef std::lock_guard<std::mutex> Lock;
typedef std::unique_lock<std::mutex> Lock;
namespace global {
// control cout....
Mutex coutLock;
} // namespace global
#include <iostream>
#include <atomic>
#include <thread>
template <typename T>
inline void spinlock(std::atomic<T> const& lock, T val) {
while (lock.load(std::memory_order_acquire) != val) {
}
}
template <typename T>
inline void spinlockSleep(std::atomic<T> const& lock, T val) {
while (lock.load(std::memory_order_acquire) != val) {
nanosleep(0, 0);
}
}
// syncronize all threads in a parallel section (for testing purposes)
void sync(std::atomic<int>& all, int total) {
++all;
spinlock(all, total);
}
namespace {
unsigned int number_of_threads() {
auto nThreads = std::thread::hardware_concurrency();
return nThreads == 0 ? 1 : nThreads;
}
template <typename F>
void parallel_run(F iFunc) {
std::vector<std::thread> threads;
auto nThreads = number_of_threads();
for (unsigned int i = 0; i < nThreads; ++i) {
threads.emplace_back([i, nThreads, iFunc] { iFunc(i, nThreads); });
}
for (auto& thread : threads) {
thread.join();
}
}
} // namespace
struct B {
virtual ~B() {}
virtual B* clone() const = 0;
};
struct T : public B {
T(int iv = 0) : v(iv) {}
int v;
bool operator==(T t) const { return v == t.v; }
virtual T* clone() const { return new T(*this); }
};
bool operator==(T const& t, B const& b) {
T const* p = dynamic_cast<T const*>(&b);
return p && p->v == t.v;
}
bool operator==(B const& b, T const& t) { return t == b; }
typedef edmNew::DetSetVector<T> DSTV;
typedef edmNew::DetSet<T> DST;
typedef edmNew::det_id_type det_id_type;
typedef DSTV::FastFiller FF;
typedef DSTV::TSFastFiller TSFF;
class TestDetSet : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(TestDetSet);
CPPUNIT_TEST(infrastructure);
CPPUNIT_TEST(fillSeq);
CPPUNIT_TEST(fillPar);
CPPUNIT_TEST_SUITE_END();
public:
TestDetSet();
~TestDetSet() {}
void setUp() {}
void tearDown() {}
void infrastructure();
void fillSeq();
void fillPar();
public:
int nth = 1;
std::vector<DSTV::data_type> sv;
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestDetSet);
TestDetSet::TestDetSet() : sv(10) {
DSTV::data_type v[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::copy(v, v + 10, sv.begin());
nth = number_of_threads();
}
void read(DSTV const& detsets, bool all = false) {
for (auto di = detsets.begin(false); di != detsets.end(false); ++di) {
auto ds = *di;
auto id = ds.id();
std::cout << id << ' ';
if (ds.isValid()) {
CPPUNIT_ASSERT(ds[0] == 100 * (id - 20) + 3);
CPPUNIT_ASSERT(ds[1] == -(100 * (id - 20) + 3));
}
}
std::cout << std::endl;
}
void TestDetSet::infrastructure() {
std::cout << std::endl;
for (int i = 0; i < 10; i++) {
int a = 0;
std::atomic<int> b(0);
std::atomic<int> lock(0);
std::atomic<int> nt(number_of_threads());
parallel_run([&a, &b, &lock, &nt](unsigned int, unsigned int) {
sync(lock, nt);
a++;
b.fetch_add(1, std::memory_order_acq_rel);
});
if (i == 5)
std::cout << "threads " << lock << " " << a << ' ' << b << std::endl;
CPPUNIT_ASSERT(b == nt);
a = 0;
b = 0;
parallel_run([&a, &b](unsigned int, unsigned int) {
a++;
b.fetch_add(1, std::memory_order_acq_rel);
});
if (i == 5)
std::cout << "threads " << lock << " " << a << ' ' << b << std::endl;
nth = nt;
}
}
void TestDetSet::fillSeq() {
std::cout << std::endl;
DSTV detsets(2);
// unsigned int ntot=0;
std::atomic<int> lock(0);
std::atomic<int> idet(0);
std::atomic<int> trial(0);
int maxDet = 100 * nth;
parallel_run([&lock, &idet, &trial, &detsets, maxDet](unsigned int, unsigned int numberOfThreads) {
sync(lock, numberOfThreads);
while (true) {
int ldet = idet;
if (!(ldet < maxDet))
break;
while (!idet.compare_exchange_weak(ldet, ldet + 1))
;
if (ldet >= maxDet)
break;
unsigned int id = 20 + ldet;
bool done = false;
while (!done) {
try {
{
FF ff(detsets, id); // serialize
ff.push_back(100 * ldet + 3);
CPPUNIT_ASSERT(detsets.m_data.back().v == (100 * ldet + 3));
ff.push_back(-(100 * ldet + 3));
CPPUNIT_ASSERT(detsets.m_data.back().v == -(100 * ldet + 3));
}
// read(detsets); // cannot read in parallel while filling in this case
done = true;
} catch (edm::Exception const&) {
trial++;
//read(detsets);
}
}
}
// read(detsets);
});
std::cout << idet << ' ' << detsets.size() << std::endl;
read(detsets, true);
CPPUNIT_ASSERT(int(detsets.size()) == maxDet);
std::cout << "trials " << trial << std::endl;
}
struct Getter final : public DSTV::Getter {
Getter(TestDetSet* itest) : ntot(0), test(*itest) {}
void fill(TSFF& ff) const override {
int n = ff.id() - 20;
CPPUNIT_ASSERT(n >= 0);
CPPUNIT_ASSERT(ff.size() == 0);
ff.push_back((100 * n + 3));
CPPUNIT_ASSERT(ff.size() == 1);
CPPUNIT_ASSERT(ff[0] == 100 * n + 3);
ff.push_back(-(100 * n + 3));
CPPUNIT_ASSERT(ff.size() == 2);
CPPUNIT_ASSERT(ff[1] == -(100 * n + 3));
ntot.fetch_add(1, std::memory_order_acq_rel);
}
mutable std::atomic<unsigned int> ntot;
TestDetSet& test;
};
void TestDetSet::fillPar() {
std::cout << std::endl;
auto pg = std::make_shared<Getter>(this);
Getter& g = *pg;
int maxDet = 100 * nth;
std::vector<unsigned int> v(maxDet);
int k = 20;
for (auto& i : v)
i = k++;
DSTV detsets(pg, v, 2);
detsets.reserve(maxDet, 100 * maxDet);
CPPUNIT_ASSERT(g.ntot == 0);
CPPUNIT_ASSERT(detsets.onDemand());
CPPUNIT_ASSERT(maxDet == int(detsets.size()));
std::atomic<int> lock(0);
std::atomic<int> idet(0);
std::atomic<int> count(0);
DST df31 = detsets[31];
std::cout << "start parallel section" << std::endl;
parallel_run([&lock, &detsets, &idet, maxDet, &g](unsigned int threadNumber, unsigned int numberOfThreads) {
sync(lock, numberOfThreads);
if (threadNumber % 2 == 0) {
DST df = detsets[25]; // everybody!
CPPUNIT_ASSERT(df.id() == 25);
CPPUNIT_ASSERT(df.size() == 2);
CPPUNIT_ASSERT(df[0] == 100 * (25 - 20) + 3);
CPPUNIT_ASSERT(df[1] == -(100 * (25 - 20) + 3));
}
while (true) {
if (threadNumber == 0)
read(detsets);
int ldet = idet.load(std::memory_order_acquire);
if (!(ldet < maxDet))
break;
while (!idet.compare_exchange_weak(ldet, ldet + 1, std::memory_order_acq_rel))
;
if (ldet >= maxDet)
break;
unsigned int id = 20 + ldet;
{
DST df = *detsets.find(id, true);
CPPUNIT_ASSERT(int(g.ntot) > 0);
assert(df.id() == id);
assert(df.size() == 2);
assert(df[0] == 100 * (id - 20) + 3);
assert(df[1] == -(100 * (id - 20) + 3));
}
if (threadNumber == 1)
read(detsets);
}
});
std::cout << "end parallel section" << std::endl;
CPPUNIT_ASSERT(df31.id() == 31);
CPPUNIT_ASSERT(df31.size() == 2);
CPPUNIT_ASSERT(df31[0] == 100 * (31 - 20) + 3);
CPPUNIT_ASSERT(df31[1] == -(100 * (31 - 20) + 3));
std::cout << "summary " << idet << ' ' << detsets.size() << ' ' << g.ntot << ' ' << count << std::endl;
read(detsets, true);
CPPUNIT_ASSERT(int(g.ntot) == maxDet);
CPPUNIT_ASSERT(int(detsets.size()) == maxDet);
}