Cleanup code

This commit is contained in:
Robin Dietzel 2023-11-07 11:51:34 +01:00
parent f0b4789e58
commit 75eb7efa11
6 changed files with 21 additions and 35 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
cmake-build-debug
cmake-build-release
cmake-build-*
.idea
task1/dataset.dat

View File

@ -1,16 +1,12 @@
cmake_minimum_required(VERSION 3.0)
project(task1)
project(aca-tasks)
set(CMAKE_CXX_STANDARD 20)
#set(CMAKE_AUTOMOC ON)
#set(CMAKE_AUTORCC ON)
#set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/installed)
if (${CMAKE_BUILD_TYPE} STREQUAL "Release")
#set(CMAKE_)
add_compile_options(
-Wall
-Wpedantic

View File

@ -17,7 +17,6 @@ add_custom_target(task1_7_dataset
${CMAKE_CURRENT_BINARY_DIR}/dataset.dat)
add_executable(task1_7 main.cpp
mergesort.h
mergesort_mt.h)
target_link_libraries(task1_7 PRIVATE

View File

@ -5,7 +5,6 @@
#include <chrono>
#include <cmath>
#include <mergesort.h>
#include <mergesort_mt.h>
/*
@ -30,9 +29,7 @@ auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
T convbuf;
while (std::getline(stream, buf)) {
convbuf = static_cast<T>(std::stoul(buf));
vec.emplace_back(std::move(convbuf));
}
@ -58,7 +55,7 @@ auto main(int argc, char *argv[]) -> int {
auto t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t> msst([](int32_t a, int32_t b) {
return (a>b);
return (a > b);
}, 0);
msst.sort(dataset_seq);
auto t2 = std::chrono::high_resolution_clock::now();
@ -68,35 +65,37 @@ auto main(int argc, char *argv[]) -> int {
const int threads = std::thread::hardware_concurrency();
const int max_depth = std::log(threads);
const int max_depth = std::log(threads);
t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t > msmt([](int32_t a, int32_t b) {
return (a>b);
MergeSorterMT<int32_t> msmt([](int32_t a, int32_t b) {
return (a > b);
}, max_depth);
msmt.sort(dataset_par);
t2 = std::chrono::high_resolution_clock::now();
auto t_par = 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("\nSorted {} entries within {} ms in parallel on a system having {} threads and a recursion depth of {}"
"\nresulting in a total count of {} threads",
dataset_seq.size(), t_par.count(), threads, max_depth, std::pow(2, max_depth));
auto eq = (dataset_seq == dataset_par);
fmt::print("\nCheck whether sorted arrays are equal: {}", (eq)? "Equal" : "not equal");
fmt::print("\nCheck whether sorted arrays are equal: {}", (eq) ? "Equal" : "not equal");
fmt::print("\n\n------------Summary------------");
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("\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);
if(!ofile.is_open()) {
if (!ofile.is_open()) {
fmt::print("\nError writing to file");
return -1;
}
for(auto &element : dataset_seq) {
for (auto &element: dataset_seq) {
ofile << std::to_string(element) << '\n';
}

View File

View File

@ -2,6 +2,7 @@
#include <span>
#include <thread>
#include <mutex>
#include <functional>
template<typename T>
class MergeSorterMT {
@ -14,7 +15,7 @@ public:
auto sort(std::vector<T> &data) -> void {
std::span<T> sortable(data);
split(sortable, 0, max_depth, mut);
split(sortable, 0, max_depth);
}
private:
@ -47,14 +48,10 @@ private:
r++;
}
{
//todo: is a lock guard necessary?
//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, const int &mdepth) -> void {
if (std::distance(data.begin(), data.end()) <= 1) {
return;
} else if (std::distance(data.begin(), data.end()) == 2) {
@ -71,17 +68,14 @@ private:
std::span<T> right(mid, data.end());
if (depth < mdepth) {
//todo: fix lambda call
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, mdepth, mutex);
//std::thread right_thread(&MergeSorterMT::split, this, right, depth + 1, mdepth, mutex);
std::thread left_thread([&]() { split(left, depth + 1, mdepth, mutex); });
std::thread right_thread([&]() { split(right, depth + 1, mdepth, mutex); });
std::thread left_thread([&]() { split(left, depth + 1, mdepth); });
std::thread right_thread([&]() { split(right, depth + 1, mdepth); });
left_thread.join();
right_thread.join();
} else {
split(left, depth + 1, mdepth, mutex);
split(right, depth + 1, mdepth, mutex);
split(left, depth + 1, mdepth);
split(right, depth + 1, mdepth);
}
merge(data, left, right);
@ -91,5 +85,4 @@ private:
private:
std::function<bool(T, T)> cmp;
const int max_depth;
std::recursive_mutex mut;
};