diff options
| author | Aiden Woodruff <woodra@rpi.edu> | 2025-11-28 22:29:47 -0500 |
|---|---|---|
| committer | Aiden Woodruff <woodra@rpi.edu> | 2025-11-28 22:29:47 -0500 |
| commit | c598445677c7911370ddb23b1fdedc1810159ccf (patch) | |
| tree | 7ce08e85d7ed251aef2ca5ec18b563f56098bdad /src | |
| parent | 80ca2b9a2499b4353685055dbf8bd49ca7521803 (diff) | |
| download | tipping-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')
| -rw-r--r-- | src/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | src/many-async.cc | 61 |
2 files changed, 72 insertions, 8 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7e0e575..f681e54 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt | |||
| @@ -1,10 +1,13 @@ | |||
| 1 | add_executable(one | 1 | add_library(tipping-points |
| 2 | one.cc | ||
| 3 | NameGame.cc | 2 | NameGame.cc |
| 4 | ) | 3 | ) |
| 5 | target_link_libraries(one PRIVATE SNAP::SNAP) | 4 | target_link_libraries(tipping-points PUBLIC SNAP::SNAP) |
| 6 | add_executable(many | 5 | if(TIPPINGPOINTS_MEMLIST) |
| 7 | many.cc | 6 | target_compile_definitions(tipping-points PUBLIC TP_MEMLIST) |
| 8 | NameGame.cc | 7 | endif() |
| 9 | ) | 8 | add_executable(one one.cc) |
| 10 | target_link_libraries(many PRIVATE SNAP::SNAP) | 9 | target_link_libraries(one PRIVATE tipping-points) |
| 10 | add_executable(many many.cc) | ||
| 11 | target_link_libraries(many PRIVATE tipping-points) | ||
| 12 | add_executable(many-async many-async.cc) | ||
| 13 | target_link_libraries(many-async PRIVATE tipping-points) | ||
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 | |||
| 8 | constexpr int group_size = 1000, T = 100; | ||
| 9 | constexpr int cmsize_min = 200, cmsize_max = 600; | ||
| 10 | constexpr int trials = 100; | ||
| 11 | constexpr int workers = 6; | ||
| 12 | |||
| 13 | using RunVec = std::vector<std::pair<int, int>>; | ||
| 14 | |||
| 15 | RunVec 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 | |||
| 31 | int 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 | |||
