aboutsummaryrefslogtreecommitdiffstats
path: root/src/many-async.cc
blob: 557c8aec01b959b463a9a1078f032a060b2249a0 (plain) (blame)
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
#include <iostream>
#include <fstream>
#include <future>
#include <vector>

#include "NameGame.h"

constexpr int group_size = 1000, T = 200;
constexpr int cmsize_min = 200, cmsize_max = 400;
constexpr int trials = 100;
constexpr int workers = 6;
constexpr const char* csv_file = "many-async-T200-exp.csv";

using RunVec = std::vector<std::pair<int, int>>;

RunVec run_trials(int cmsize_start, int cmsize_end) {
	std::random_device seeder;
	tp::NameGame namegame(group_size, 0, 12);
	RunVec runs;
	for (int i = cmsize_start; i < cmsize_end; ++i) {
		for (int j = 0; j < trials; ++j) {
			namegame.setCMsize(i);
			namegame.initMemory();
			namegame.clearRecord();
			int a = namegame.run(group_size * T);
			runs.push_back({i, a});
		}
		namegame.seed(seeder());
		std::cout << "finished cmsize " << i << std::endl;
	}
	return runs;
}

int main(int argc, char* argv[]) {
	RunVec all_runs;
	std::vector<std::future<RunVec>> futs;
	for (int i = 0; i < workers; ++i) {
		int range_size = cmsize_max - cmsize_min + 1;
		int worker_range_size = range_size / workers;
		int worker_range_start = cmsize_min + worker_range_size * i;
		int worker_range_end = worker_range_start + worker_range_size
			+ (i == workers - 1 ? range_size % workers : 0);
		std::cout << "worker " << i << " will work on range " << worker_range_start
			<< " to " << worker_range_end << std::endl;
		futs.push_back(
			std::async(run_trials, worker_range_start, worker_range_end
		));
	}
	for (int i = 0; i < workers; ++i) {
		RunVec v = futs[i].get();
		all_runs.insert(all_runs.end(), v.begin(), v.end());
	}
	std::ofstream f(csv_file);
	if (!f) {
		std::cerr << "ERROR: opening many-async.csv" << std::endl;
		return 1;
	}
	f << "cmsize,adopters\n";
	for (const auto& p : all_runs) {
		f << p.first << ',' << p.second << '\n';
	}
	return 0;
}