aboutsummaryrefslogtreecommitdiffstats
path: root/src/many-async.cc
diff options
context:
space:
mode:
authorAiden Woodruff <woodra@rpi.edu>2025-11-28 22:29:47 -0500
committerAiden Woodruff <woodra@rpi.edu>2025-11-28 22:29:47 -0500
commitc598445677c7911370ddb23b1fdedc1810159ccf (patch)
tree7ce08e85d7ed251aef2ca5ec18b563f56098bdad /src/many-async.cc
parent80ca2b9a2499b4353685055dbf8bd49ca7521803 (diff)
downloadtipping-points-c598445677c7911370ddb23b1fdedc1810159ccf.tar.gz
tipping-points-c598445677c7911370ddb23b1fdedc1810159ccf.tar.bz2
tipping-points-c598445677c7911370ddb23b1fdedc1810159ccf.zip
add multithreaded executable
- src/many-async.cc: leverage std::async to run concurrent experiments. - CMakeLists.txt: add memory list option and make it default to ON. - src/CMakeLists.txt: make the NameGame.cc into a library now that it's being used by three different executables. - add compile definition with tipping points cmake option. - add many-async executable. Signed-off-by: Aiden Woodruff <woodra@rpi.edu>
Diffstat (limited to 'src/many-async.cc')
-rw-r--r--src/many-async.cc61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/many-async.cc b/src/many-async.cc
new file mode 100644
index 0000000..9df475b
--- /dev/null
+++ b/src/many-async.cc
@@ -0,0 +1,61 @@
1#include <iostream>
2#include <fstream>
3#include <future>
4#include <vector>
5
6#include "NameGame.h"
7
8constexpr int group_size = 1000, T = 100;
9constexpr int cmsize_min = 200, cmsize_max = 600;
10constexpr int trials = 100;
11constexpr int workers = 6;
12
13using RunVec = std::vector<std::pair<int, int>>;
14
15RunVec run_trials(int cmsize_start, int cmsize_end) {
16 tp::NameGame namegame(group_size, 0, 12);
17 RunVec runs;
18 for (int i = cmsize_start; i < cmsize_end; ++i) {
19 for (int j = 0; j < trials; ++j) {
20 namegame.setCMsize(i);
21 namegame.initMemory();
22 namegame.clearRecord();
23 int a = namegame.run(group_size * T);
24 runs.push_back({i, a});
25 }
26 std::cout << "finished cmsize " << i << std::endl;
27 }
28 return runs;
29}
30
31int main(int argc, char* argv[]) {
32 RunVec all_runs;
33 std::vector<std::future<RunVec>> futs;
34 for (int i = 0; i < workers; ++i) {
35 int range_size = cmsize_max - cmsize_min + 1;
36 int worker_range_size = range_size / workers;
37 int worker_range_start = cmsize_min + worker_range_size * i;
38 int worker_range_end = worker_range_start + worker_range_size
39 + (i == workers - 1 ? range_size % workers : 0);
40 std::cout << "worker " << i << " will work on range " << worker_range_start
41 << " to " << worker_range_end << std::endl;
42 futs.push_back(
43 std::async(run_trials, worker_range_start, worker_range_end
44 ));
45 }
46 for (int i = 0; i < workers; ++i) {
47 RunVec v = futs[i].get();
48 all_runs.insert(all_runs.end(), v.begin(), v.end());
49 }
50 std::ofstream f("many-async.csv");
51 if (!f) {
52 std::cerr << "ERROR: opening many-async.csv" << std::endl;
53 return 1;
54 }
55 f << "cmsize,adopters\n";
56 for (const auto& p : all_runs) {
57 f << p.first << ',' << p.second << '\n';
58 }
59 return 0;
60}
61