Skip to content

Commit 2b537f0

Browse files
committed
Create lazy version of challenges input generation.
Changes sample job script files. Fixed sum challenge template.
1 parent 92ccde3 commit 2b537f0

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

challenges/distributed-computing/hello/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
```sh
66
"Hello, world! from processor <name>, rank <ID> out of <total> processors
7+
8+
# Repeated for however many nodes you allocated.
79
```

challenges/distributed-computing/job.slurm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#SBATCH --job-name=
44
#SBATCH --time=0:05:00
5+
#SBATCH --mem=1GB
56
#SBATCH --ntasks=1
67
#SBATCH --ntasks-per-node=1
78
#SBATCH --cpus-per-task=1

challenges/distributed-computing/sum/sum.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(int argc, char* argv[])
1313
unsigned N = atoi(argv[1]);
1414
double* input = (double*)malloc(N * sizeof(double));
1515

16-
MPI_INIT(&argc, &argv[0]);
16+
MPI_Init(&argc, &argv);
1717

1818
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
1919
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

challenges/parallel-computing/job.slurm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#SBATCH --job-name=
44
#SBATCH --time=0:05:00
5+
#SBATCH --mem=1GB
56
#SBATCH --ntasks=1
7+
#SBATCH --ntasks-per-node=1
68
#SBATCH --cpus-per-task=1
79
#SBATCH --partition=
810
#SBATCH --output=slurm-%x.out

generate-lazy.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) Tyler Swann 2023
2+
// Made in partnership with Monash DeepNeuron
3+
// for Monash DeepNeuron's HPC Training.
4+
5+
// Details
6+
// This program will generate the input files
7+
// containing the random list of numbers used
8+
// by in some of the challenges.
9+
//
10+
// Build
11+
// $ g++ -std=c++20 -o bin/generate generate.cpp
12+
//
13+
// Run
14+
// $ bin/generate
15+
16+
#include <algorithm>
17+
#include <concepts>
18+
#include <filesystem>
19+
#include <fstream>
20+
#include <iostream>
21+
#include <numeric>
22+
#include <random>
23+
#include <ranges>
24+
#include <string_view>
25+
#include <vector>
26+
27+
namespace fs = std::filesystem;
28+
29+
constexpr std::size_t default_problem_size = 1'000'000uL;
30+
31+
fs::path main_path = fs::current_path() / "challenges" / "distributed-computing";
32+
fs::path sum_path = main_path / "sum";
33+
fs::path mergesort_path = main_path / "mergesort";
34+
35+
auto gen = std::mt19937 { std::random_device {}() };
36+
auto distrib = std::uniform_real_distribution { -1.0e5, 1.0e5 };
37+
auto generate = std::views::transform([]([[maybe_unused]] auto x) -> double { return distrib(gen); });
38+
39+
template <std::ranges::range Range>
40+
auto write(const fs::path& filename, Range&& data) -> void
41+
{
42+
auto file = std::fstream { filename, std::ios::in | std::ios::out | std::ios::trunc };
43+
44+
if (file.is_open()) {
45+
std::ranges::copy(data, std::ostream_iterator<double>(file, "\n"));
46+
} else
47+
std::clog << "Failed to open file" << filename << std::endl;
48+
}
49+
50+
auto write(const fs::path& filename, const double& data) -> void
51+
{
52+
auto file = std::fstream { filename, std::ios::in | std::ios::out | std::ios::trunc };
53+
54+
if (file.is_open()) {
55+
file << data << std::endl;
56+
} else
57+
std::clog << "Failed to open file" << filename << std::endl;
58+
}
59+
60+
namespace make {
61+
62+
auto sum(std::size_t problem_size) -> void
63+
{
64+
auto nums = std::views::iota(0)
65+
| generate
66+
| std::views::take(problem_size)
67+
| std::views::common;
68+
69+
auto sum = std::accumulate(nums.begin(), nums.end(), double {});
70+
71+
write(sum_path / "input.txt", nums);
72+
write(sum_path / "output.txt", sum);
73+
}
74+
75+
// auto mergesort(std::size_t problem_size) -> void
76+
// {
77+
// auto nums = std::views::iota(0) | generate | std::views::take(problem_size);
78+
// write(mergesort_path / "unsorted.txt", nums);
79+
80+
// std::ranges::sort(nums);
81+
82+
// if (!std::ranges::is_sorted(nums)) {
83+
// std::clog << "Didn't sort input" << std::endl;
84+
// return;
85+
// }
86+
87+
// write(mergesort_path / "sorted.txt", nums);
88+
// }
89+
90+
} // namespace make
91+
92+
int main(int argc, char* argv[])
93+
{
94+
std::size_t problem_size = default_problem_size;
95+
96+
if (argc > 1) {
97+
problem_size = std::atoll(argv[1]);
98+
problem_size = std::ranges::clamp(problem_size, default_problem_size, std::vector<double> {}.max_size());
99+
}
100+
101+
// auto buffer = std::vector<double>(problem_size, 0.0);
102+
103+
make::sum(problem_size);
104+
// make::mergesort(problem_size);
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)