-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadBalancer.cpp
More file actions
51 lines (45 loc) · 1.49 KB
/
Copy pathLoadBalancer.cpp
File metadata and controls
51 lines (45 loc) · 1.49 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
#include <string>
#include <fstream>
#include <iostream>
#include "vector"
#include "LoadBalancer.h"
#include "fileoutstream.h"
std::vector<int> LoadBalancer::balance(int num_threads, int max_num) {
// Calculate start and end for each thread
// Return an array of start and end per thread
std::vector <int> res = {};
res.resize(num_threads * 2);
int tasks_per_thread = max_num / num_threads;
int n = 0;
for (int i = 0; i < num_threads; i++) {
int start = n + 1;
int end = n + tasks_per_thread;
n = end;
res[i * 2] = start;
res[i * 2 + 1] = end;
}
return res;
}
void LoadBalancer::get_final_result(int num_threads) {
// Concatenate all main*.tmp files into main.txt
std::ofstream main_file("main.txt");
for (int i = 0; i < num_threads; i++) {
std::ifstream tmp_file(tmp_filename(i));
std::string line;
if (tmp_file.is_open() and main_file.is_open()) {
while (getline(tmp_file, line)) {
std::cout << line << '\n';
main_file << line << std::endl;
}
tmp_file.close();
std::remove(LoadBalancer::tmp_filename(i).c_str());
} else {
throw std::runtime_error("Unable to open file, check os settings");
}
}
}
std::string LoadBalancer::tmp_filename(int i) {
// Get filename for tmp file with thread work results
std::string filename = "main" + std::to_string(i) + ".tmp";
return filename;
}