Compare commits

..

No commits in common. "ec1d2cdd46a7c8fb5d1fb72946d9b3744adeb89c" and "510d1e4e4be65bc6d4227ea9ff952a8ef4048a5b" have entirely different histories.

2 changed files with 42 additions and 49 deletions

View File

@ -3,7 +3,6 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <cmath>
#include <mergesort.h> #include <mergesort.h>
#include <mergesort_mt.h> #include <mergesort_mt.h>
@ -40,18 +39,17 @@ auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
auto main(int argc, char *argv[]) -> int { auto main(int argc, char *argv[]) -> int {
try { try {
const auto path = "dataset.dat"; std::ifstream file("dataset.dat", std::ios_base::in);
std::ifstream file(path, std::ios_base::in);
if (!file.is_open()) { if (!file.is_open()) {
fmt::print("\nError opening file"); fmt::print("Error opening file");
return -1; return -1;
} }
fmt::print("\nOpened file {} sucessfully", path); fmt::print("Opened file {} sucessfully\n", "dummy");
std::vector<int32_t> dataset; std::vector<int32_t> dataset;
parse_file(file, dataset); parse_file(file, dataset);
fmt::print("\nRead {} values from {}", dataset.size(), path); fmt::print("Read {} values from {}\n", dataset.size(), "dummy");
auto dataset_par = dataset; auto dataset_par = dataset;
auto dataset_seq = dataset; auto dataset_seq = dataset;
@ -62,36 +60,37 @@ auto main(int argc, char *argv[]) -> int {
}, 0); }, 0);
auto t2 = std::chrono::high_resolution_clock::now(); auto t2 = std::chrono::high_resolution_clock::now();
auto t_seq = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); auto delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("\nSorted {} entries within {} ms in sequential", dataset_seq.size(), t_seq.count()); fmt::print("Sorted {} entries within {} ms in sequential\n", dataset_seq.size(), delay_ms.count());
const int threads = std::thread::hardware_concurrency(); //const int max_depth = std::thread::hardware_concurrency();
const int max_depth = std::log(threads); const int max_depth = 4;
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t > ms([](int32_t a, int32_t b) { MergeSorterMT<int> ms([](int a, int b) {
return (a>b); return (a>b);
}, max_depth); });
ms.sort(dataset_par); std::span t = dataset_par;
int mdepth = 4;
std::recursive_mutex mut;
ms.split(t, 0, mdepth, mut);
// algo::MergeSort_mt::sort(dataset_par, [](int32_t a, int32_t b) {
// return (a > b);
// }, max_depth);
t2 = std::chrono::high_resolution_clock::now(); t2 = std::chrono::high_resolution_clock::now();
auto t_par = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1); delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("\nSorted {} entries within {} ms in parallel using {} threads and a recursion depth of {}", dataset_seq.size(), t_par.count(), threads, max_depth); fmt::print("Sorted {} entries within {} ms in parallel using {} threads\n", dataset_seq.size(), delay_ms.count(), max_depth);
auto eq = (dataset_seq == dataset_par); auto eq = (dataset_seq == dataset_par);
fmt::print("\nCheck whether sorted arrays are equal: {}", (eq)? "Equal" : "not equal"); fmt::print("Equality: {}\n", eq);
fmt::print("Parallel dataset: {}; Sequential dataset: {}\n", dataset_par.size(), dataset_seq.size());
fmt::print("\n\n------------Summary------------"); //fmt::print("Created {} recurstions", algo::MergeSort_v1::get_recursions());
fmt::print("\nt_seq = {: > 5.2f} ms", static_cast<float>(t_seq.count()));
fmt::print("\nt_par = {: > 5.2f} ms", static_cast<float>(t_par.count()));
fmt::print("\nspeedup = {: > 5.2f}", (1.0*t_seq/t_par));
fmt::print("\nDelta_t = {: > 5.2f} ms", static_cast<float>(t_seq.count() - t_par.count()));
fmt::print("\n-------------------------------");
std::ofstream ofile("dataset.out.dat", std::ios_base::out); std::ofstream ofile("dataset.out.dat", std::ios_base::out);
if(!ofile.is_open()) { if(!ofile.is_open()) {
fmt::print("\nError writing to file"); fmt::print("Error writing to file");
return -1; return -1;
} }
@ -103,12 +102,12 @@ auto main(int argc, char *argv[]) -> int {
ofile.flush(); ofile.flush();
ofile.close(); ofile.close();
fmt::print("\nWritten to output file"); fmt::print("Written to output file\n");
return 0; return 0;
} catch (std::exception &e) { } catch (std::exception &e) {
fmt::print("\nError occured: {}", e.what()); fmt::print("Error occured: {}", e.what());
return -1; return -1;
} }

View File

@ -8,16 +8,14 @@ class MergeSorterMT {
public: public:
template<typename C> template<typename C>
MergeSorterMT(C cmp, int max_depth) : cmp(cmp), max_depth(max_depth) { MergeSorterMT(C cmp) : cmp(cmp){
static_assert(std::is_same<std::invoke_result_t<C, T, T>, bool>(), "C must be a function that returns a bool"); static_assert(std::is_same<std::invoke_result_t<C, T, T>, bool>(), "C must be a function that returns a bool");
} }
auto sort(std::vector<T> &data) -> void {
std::span<T> sortable(data);
split(sortable, 0, max_depth, mut);
}
private: std::function<bool(T, T)> cmp;
std::recursive_mutex mut;
auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void { auto merge(std::span<T> &output, std::span<T> left, std::span<T> right) -> void {
std::vector<T> buf; std::vector<T> buf;
buf.reserve(left.size() + right.size()); buf.reserve(left.size() + right.size());
@ -48,13 +46,12 @@ private:
} }
{ {
//todo: is a lock guard necessary? std::lock_guard<std::recursive_mutex> lock(mut);
//std::lock_guard<std::recursive_mutex> lock(mut);
std::move(buf.begin(), buf.end(), output.begin()); std::move(buf.begin(), buf.end(), output.begin());
} }
} }
auto split(std::span<T> &data, int depth, const int &mdepth, std::recursive_mutex &mutex) -> void { auto split(std::span<T> &data, int depth, int &max_depth, std::recursive_mutex &mut) -> void {
if(std::distance(data.begin(), data.end()) <= 1) { if(std::distance(data.begin(), data.end()) <= 1) {
return; return;
} }
@ -65,26 +62,23 @@ private:
std::span<T> left(data.begin(), mid); std::span<T> left(data.begin(), mid);
std::span<T> right(mid, data.end()); std::span<T> right(mid, data.end());
if (depth < mdepth) { if(depth < max_depth) {
//todo: fix lambda call //std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, max_depth, mut);
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, mdepth, mutex); //std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, max_depth, mut);
//std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, mdepth, mutex); std::thread left_thread([&](){split(left, depth + 1, max_depth, mut);});
std::thread left_thread([&]() { split(left, depth + 1, mdepth, mutex); }); std::thread right_thread([&](){split(right, depth + 1, max_depth, mut);});
std::thread right_thread([&]() { split(right, depth + 1, mdepth, mutex); });
left_thread.join(); left_thread.join();
right_thread.join(); right_thread.join();
} else { } else {
split(left, depth + 1, mdepth, mutex); split(left, depth + 1, max_depth, mut);
split(right, depth + 1, mdepth, mutex); split(right, depth + 1, max_depth, mut);
} }
merge(data, left, right); merge(data, left, right);
} }
private:
std::function<bool(T, T)> cmp;
const int max_depth;
std::recursive_mutex mut;
}; };