1 /*------------------------------------------------------------------------
2 Junction: Concurrent data structures in C++
3 Copyright (c) 2016 Jeff Preshing
5 Distributed under the Simplified BSD License.
6 Original location: https://github.com/preshing/junction
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
13 #include <junction/Core.h>
14 #include <turf/CPUTimer.h>
15 #include <turf/Util.h>
16 #include <turf/extra/UniqueSequence.h>
17 #include <turf/extra/JobDispatcher.h>
18 #include <turf/extra/Options.h>
19 #include <junction/extra/MapAdapter.h>
23 using namespace turf::intTypes;
24 typedef junction::extra::MapAdapter MapAdapter;
26 static const ureg NumKeysPerThread = 2000;
27 static const ureg DefaultReadsPerWrite = 4;
28 static const ureg DefaultItersPerChunk = 10000;
29 static const ureg DefaultChunks = 200;
30 static const u32 Prime = 0x4190ab09;
35 ureg numKeysPerThread;
39 turf::extra::SpinKicker spinKicker;
40 turf::Atomic<u32> doneFlag;
42 SharedState(MapAdapter& adapter, ureg numKeysPerThread, ureg readsPerWrite, ureg itersPerChunk)
43 : adapter(adapter), map(NULL), numKeysPerThread(numKeysPerThread), readsPerWrite(readsPerWrite), itersPerChunk(itersPerChunk) {
44 doneFlag.storeNonatomic(0);
51 SharedState& m_shared;
52 MapAdapter::ThreadContext m_threadCtx;
69 Stats& operator+=(const Stats& other) {
70 mapOpsDone += other.mapOpsDone;
71 duration += other.duration;
75 bool operator<(const Stats& other) const {
76 return duration < other.duration;
82 ThreadState(SharedState& shared, ureg threadIndex, u32 rangeLo, u32 rangeHi) : m_shared(shared), m_threadCtx(shared.adapter, threadIndex) {
83 m_threadIndex = threadIndex;
87 m_removeIndex = rangeLo;
90 void registerThread() {
91 m_threadCtx.registerThread();
94 void unregisterThread() {
95 m_threadCtx.unregisterThread();
98 void initialPopulate() {
99 TURF_ASSERT(m_addIndex == m_removeIndex);
100 MapAdapter::Map *map = m_shared.map;
101 for (ureg i = 0; i < m_shared.numKeysPerThread; i++) {
102 u32 key = m_addIndex * Prime;
104 map->insert(key, (void*) uptr(key));
105 if (++m_addIndex == m_rangeHi)
106 m_addIndex = m_rangeLo;
111 MapAdapter::Map *map = m_shared.map;
112 turf::CPUTimer::Converter converter;
114 ureg lookupIndex = m_rangeLo;
115 ureg remaining = m_shared.itersPerChunk;
116 if (m_threadIndex == 0)
117 m_shared.spinKicker.kick(m_shared.numThreads - 1);
120 m_shared.spinKicker.waitForKick();
124 turf::CPUTimer::Point start = turf::CPUTimer::get();
125 for (; remaining > 0; remaining--) {
127 if (m_shared.doneFlag.load(turf::Relaxed))
129 u32 key = m_addIndex * Prime;
131 map->insert(key, (void*) uptr(key));
134 if (++m_addIndex == m_rangeHi)
135 m_addIndex = m_rangeLo;
138 if (s32(lookupIndex - m_removeIndex) < 0)
139 lookupIndex = m_removeIndex;
140 for (ureg l = 0; l < m_shared.readsPerWrite; l++) {
141 if (m_shared.doneFlag.load(turf::Relaxed))
143 key = lookupIndex * Prime;
145 volatile void* value = map->get(key);
149 if (++lookupIndex == m_rangeHi)
150 lookupIndex = m_rangeLo;
151 if (lookupIndex == m_addIndex)
152 lookupIndex = m_removeIndex;
156 if (m_shared.doneFlag.load(turf::Relaxed))
158 key = m_removeIndex * Prime;
163 if (++m_removeIndex == m_rangeHi)
164 m_removeIndex = m_rangeLo;
167 if (s32(lookupIndex - m_removeIndex) < 0)
168 lookupIndex = m_removeIndex;
169 for (ureg l = 0; l < m_shared.readsPerWrite; l++) {
170 if (m_shared.doneFlag.load(turf::Relaxed))
172 key = lookupIndex * Prime;
174 volatile void* value = map->get(key);
178 if (++lookupIndex == m_rangeHi)
179 lookupIndex = m_rangeLo;
180 if (lookupIndex == m_addIndex)
181 lookupIndex = m_removeIndex;
184 if (m_threadIndex == 0)
185 m_shared.doneFlag.store(1, turf::Relaxed);
186 m_threadCtx.update();
187 turf::CPUTimer::Point end = turf::CPUTimer::get();
190 stats.duration = converter.toSeconds(end - start);
195 static const turf::extra::Option Options[] = {
196 { "readsPerWrite", 'r', true, "number of reads per write" },
197 { "itersPerChunk", 'i', true, "number of iterations per chunk" },
198 { "chunks", 'c', true, "number of chunks to execute" },
199 { "keepChunkFraction", 'k', true, "threshold fraction of chunk timings to keep" },
202 int main(int argc, const char** argv) {
203 turf::extra::Options options(Options, TURF_STATIC_ARRAY_SIZE(Options));
204 options.parse(argc, argv);
205 ureg readsPerWrite = options.getInteger("readsPerWrite", DefaultReadsPerWrite);
206 ureg itersPerChunk = options.getInteger("itersPerChunk", DefaultItersPerChunk);
207 ureg chunks = options.getInteger("chunks", DefaultChunks);
208 double keepChunkFraction = options.getDouble("keepChunkFraction", 1.0);
210 turf::extra::JobDispatcher dispatcher;
211 ureg numCores = dispatcher.getNumPhysicalCores();
212 TURF_ASSERT(numCores > 0);
213 MapAdapter adapter(numCores);
215 // Create shared state and register first thread
216 SharedState shared(adapter, NumKeysPerThread, readsPerWrite, itersPerChunk);
217 std::vector<ThreadState> threads;
218 threads.reserve(numCores);
219 for (ureg t = 0; t < numCores; t++) {
220 u32 rangeLo = 0xffffffffu / numCores * t + 1;
221 u32 rangeHi = 0xffffffffu / numCores * (t + 1) + 1;
222 threads.emplace_back(shared, t, rangeLo, rangeHi);
224 dispatcher.kickOne(0, &ThreadState::registerThread, threads[0]);
227 // Create the map and populate it entirely from main thread
228 MapAdapter::Map map(MapAdapter::getInitialCapacity(numCores * NumKeysPerThread));
230 for (ureg t = 0; t < numCores; t++) {
231 threads[t].initialPopulate();
235 printf("'mapType': '%s',\n", MapAdapter::MapName);
236 printf("'population': %d,\n", (int) (numCores * NumKeysPerThread));
237 printf("'readsPerWrite': %d,\n", (int) readsPerWrite);
238 printf("'itersPerChunk': %d,\n", (int) itersPerChunk);
239 printf("'chunks': %d,\n", (int) chunks);
240 printf("'keepChunkFraction': %f,\n", keepChunkFraction);
241 printf("'labels': ('numThreads', 'mapOpsDone', 'totalTime'),\n"),
242 printf("'points': [\n");
243 for (shared.numThreads = 1; shared.numThreads <= numCores; shared.numThreads++) {
244 if (shared.numThreads > 1) {
245 // Spawn and register a new thread
246 dispatcher.kickOne(shared.numThreads - 1, &ThreadState::registerThread, threads[shared.numThreads - 1]);
249 std::vector<ThreadState::Stats> kickTotals;
250 for (ureg c = 0; c < chunks; c++) {
251 shared.doneFlag.storeNonatomic(false);
252 dispatcher.kickMulti(&ThreadState::run, &threads[0], shared.numThreads);
254 ThreadState::Stats kickTotal;
255 for (ureg t = 0; t < shared.numThreads; t++)
256 kickTotal += threads[t].m_stats;
257 kickTotals.push_back(kickTotal);
260 std::sort(kickTotals.begin(), kickTotals.end());
261 ThreadState::Stats totals;
262 for (ureg t = 0; t < ureg(kickTotals.size() * keepChunkFraction); t++) {
263 totals += kickTotals[t];
266 printf(" (%d, %d, %f),\n",
267 int(shared.numThreads),
268 int(totals.mapOpsDone),
277 dispatcher.kickMulti(&ThreadState::unregisterThread, &threads[0], threads.size());