forked from nikitadanilov/usched
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c++main.cpp
71 lines (63 loc) · 1.82 KB
/
c++main.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
/* gcc -O -fcoroutines -Wall coro.cpp -lcppcoro -lstdc++ -pthread */
/* https://github.com/andreasbuhr/cppcoro.git */
#include <cppcoro/shared_task.hpp>
#include <cppcoro/single_consumer_event.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include <cppcoro/static_thread_pool.hpp>
#include <iostream>
#include <chrono>
static int n;
static int r;
static int m;
static int d;
static uint32_t p;
static std::vector<cppcoro::shared_task<void>> t;
static cppcoro::single_consumer_event *e;
static cppcoro::static_thread_pool *tp;
static cppcoro::shared_task<void> pingpong(int idx)
{
auto next = idx / n * n + (idx + 1) % n;
if (p > 1) {
co_await tp->schedule();
}
for (int i = 0; i < m; ++i) {
if (idx % n == i % n) {
// std::cout << i << " * " << idx << " send\n";
e[next].set();
// std::cout << i << " * " << idx << " wait\n";
co_await e[idx];
} else {
// std::cout << i << " " << idx << " wait\n";
co_await e[idx];
// std::cout << i << " " << idx << " send\n";
e[next].set();
}
}
}
static cppcoro::shared_task<int> work()
{
co_await cppcoro::when_all(t);
co_return 0;
}
using namespace std::chrono;
int main(int argc, char **argv)
{
n = std::atoi(argv[1]); /* Cycle length. */
r = std::atoi(argv[2]); /* Number of cycles. */
m = std::atoi(argv[3]); /* Number of rounds. */
d = std::atoi(argv[4]); /* Additional stack depth. */
p = std::atoi(argv[5]); /* Number of processors. */
cppcoro::static_thread_pool pool{p};
tp = &pool;
e = new cppcoro::single_consumer_event[n * r];
for (int i = 0; i < n * r; ++i) {
t.push_back(pingpong(i));
}
auto t0 = system_clock::now();
cppcoro::sync_wait(work());
auto t1 = system_clock::now();
printf("%f\n", duration_cast<nanoseconds>(t1 - t0).count() / 1000000000.);
delete[] e;
}