-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtdb2.cpp
More file actions
56 lines (53 loc) · 1.24 KB
/
mtdb2.cpp
File metadata and controls
56 lines (53 loc) · 1.24 KB
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
#include "mpmc_bounded_queue.h"
#include <unordered_map>
#include <iostream>
#include <future>
#include <thread>
const int SIZE = 1000000;
struct shard_t
{
std::unordered_map<int,int> map;
mpmc_bounded_queue<int> queue;
std::future<long long> fu;
char pad[64];
};
int main(int argc, char *argv[])
{
const int thcount = (argc > 1 && atoi(argv[1]) > 0) ? atoi(argv[1]) : 1;
// assume data sharded by %thcount between threads
shard_t shards[thcount];
for (int k = 0; k < thcount; k++)
{
auto &s = shards[k];
s.map.reserve(2*SIZE/thcount);
s.fu = std::async(std::launch::async, [&](int kk)-> long long
{
auto &s = shards[kk];
long long c = 0;
int i = -1;
do
{
if (s.queue.try_pop(i))
{
if (i < 0) break;
c += s.map[i*i] += i;
}
}
while(true);
return c;
}, k);
}
// emulate requests
for (int i = 0; i < SIZE; i++)
shards[unsigned(i*i) % thcount].queue.push(i);
// done
for (auto &s : shards)
s.queue.push(-1);
// merge result
long long c = 0;
for (auto &s : shards)
c += s.fu.get();
//
std::cout << c << std::endl;
return 0;
}