-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.cpp
More file actions
executable file
·133 lines (108 loc) · 4.62 KB
/
generate.cpp
File metadata and controls
executable file
·133 lines (108 loc) · 4.62 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Tyler Swann 2023
// Made in partnership with Monash DeepNeuron
// for Monash DeepNeuron's HPC Training.
// Details
// This program will generate the input files
// containing the random list of numbers used
// by in some of the challenges.
//
// Build
// $ module load gcc/10.2.0
// $ g++ -std=c++20 -o bin/generate generate.cpp
//
// Run
// $ bin/generate 1000000000
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <numeric>
#include <random>
#include <ranges>
#include <string_view>
#include <vector>
namespace fs = std::filesystem;
using clock_type = std::chrono::high_resolution_clock;
constexpr std::size_t default_problem_size = 1'000'000uL;
fs::path main_path = fs::current_path() / "challenges" / "distributed-computing";
fs::path sum_path = main_path / "sum";
fs::path mergesort_path = main_path / "mergesort";
auto gen = std::mt19937 { std::random_device {}() };
auto distrib = std::uniform_real_distribution { -1.0e5, 1.0e5 };
auto generate = [](auto& container) { std::ranges::generate(container, [&]() -> double { return distrib(gen); }); };
auto write(const fs::path& filename, const std::vector<double>& data) -> void
{
auto file = std::fstream { filename, std::ios::in | std::ios::out | std::ios::trunc };
if (file.is_open()) {
std::ranges::copy(data, std::ostream_iterator<double>(file, "\n"));
} else
std::clog << "Failed to open file" << filename << std::endl;
file.close();
}
auto write(const fs::path& filename, const double& data) -> void
{
auto file = std::fstream { filename, std::ios::in | std::ios::out | std::ios::trunc };
if (file.is_open()) {
file << data << std::endl;
} else
std::clog << "Failed to open file" << filename << std::endl;
file.close();
}
namespace make {
auto sum(std::vector<double>& buffer) -> void
{
auto start_gen = clock_type::now();
generate(buffer);
auto sum = std::accumulate(buffer.cbegin(), buffer.cend(), double {});
auto end_gen = clock_type::now();
auto time_gen = std::chrono::duration_cast<std::chrono::seconds>(end_gen - start_gen).count();
std::clog << "Finished Generating [Sum]. Took: " << time_gen << "s" << std::endl;
auto start_write = clock_type::now();
write(sum_path / "input.txt", buffer);
write(sum_path / "output.txt", sum);
auto end_write = clock_type::now();
auto time_write = std::chrono::duration_cast<std::chrono::seconds>(end_write - start_write).count();
std::clog << "Finished Writing [Sum]. Took: " << time_write << "s" << std::endl;
}
auto mergesort(std::vector<double>& buffer) -> void
{
auto start_input_gen = clock_type::now();
generate(buffer);
auto end_input_gen = clock_type::now();
auto time_input_gen = std::chrono::duration_cast<std::chrono::seconds>(end_input_gen - start_input_gen).count();
std::clog << "Finished Generating [mergesort - input]. Took: " << time_input_gen << "s" << std::endl;
auto start_input_write = clock_type::now();
write(mergesort_path / "unsorted.txt", buffer);
auto end_input_write = clock_type::now();
auto time_input_write = std::chrono::duration_cast<std::chrono::seconds>(end_input_write - start_input_write).count();
std::clog << "Finished Writing [mergesort - input]. Took: " << time_input_write << "s" << std::endl;
auto start_output_gen = clock_type::now();
std::ranges::sort(buffer);
if (!std::ranges::is_sorted(buffer)) {
std::clog << "Didn't sort input properly" << std::endl;
return;
}
auto end_output_gen = clock_type::now();
auto time_output_gen = std::chrono::duration_cast<std::chrono::seconds>(end_output_gen - start_output_gen).count();
std::clog << "Finished Generating [mergesort - input]. Took: " << time_output_gen << "s" << std::endl;
auto start_output_write = clock_type::now();
write(mergesort_path / "sorted.txt", buffer);
auto end_output_write = clock_type::now();
auto time_output_write = std::chrono::duration_cast<std::chrono::seconds>(end_output_write - start_output_write).count();
std::clog << "Finished Writing [mergesort - input]. Took: " << time_output_write << "s" << std::endl;
}
} // namespace make
int main(int argc, char* argv[])
{
std::size_t problem_size = default_problem_size;
if (argc > 1) {
problem_size = std::atoll(argv[1]);
problem_size = std::ranges::clamp(problem_size, default_problem_size, std::vector<double> {}.max_size());
}
auto buffer = std::vector<double>(problem_size, 0.0);
std::clog << "MDN HPC Training Generator" << std::endl;
make::sum(buffer);
make::mergesort(buffer);
return 0;
}