From 75eb7efa114baea76f84212482b17ba4160680e7 Mon Sep 17 00:00:00 2001 From: robtor Date: Tue, 7 Nov 2023 11:51:34 +0100 Subject: [PATCH] Cleanup code --- .gitignore | 3 +-- CMakeLists.txt | 6 +----- task1/CMakeLists.txt | 1 - task1/main.cpp | 23 +++++++++++------------ task1/mergesort.cpp | 0 task1/mergesort_mt.h | 23 ++++++++--------------- 6 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 task1/mergesort.cpp diff --git a/.gitignore b/.gitignore index 5170b8d..b849c63 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -cmake-build-debug -cmake-build-release +cmake-build-* .idea task1/dataset.dat \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c285173..ee5a92b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/task1/CMakeLists.txt b/task1/CMakeLists.txt index 58a6e6d..34e9f99 100644 --- a/task1/CMakeLists.txt +++ b/task1/CMakeLists.txt @@ -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 diff --git a/task1/main.cpp b/task1/main.cpp index c201f9b..de8630f 100644 --- a/task1/main.cpp +++ b/task1/main.cpp @@ -5,7 +5,6 @@ #include #include -#include #include /* @@ -30,9 +29,7 @@ auto parse_file(std::ifstream &stream, std::vector &vec) -> void { T convbuf; while (std::getline(stream, buf)) { - convbuf = static_cast(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 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 msmt([](int32_t a, int32_t b) { - return (a>b); + MergeSorterMT 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(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(t_seq.count())); fmt::print("\nt_par = {: > 5.2f} ms", static_cast(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(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'; } diff --git a/task1/mergesort.cpp b/task1/mergesort.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/task1/mergesort_mt.h b/task1/mergesort_mt.h index 282a8e8..494b847 100644 --- a/task1/mergesort_mt.h +++ b/task1/mergesort_mt.h @@ -2,6 +2,7 @@ #include #include #include +#include template class MergeSorterMT { @@ -14,7 +15,7 @@ public: auto sort(std::vector &data) -> void { std::span 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 lock(mut); - std::move(buf.begin(), buf.end(), output.begin()); - } + std::move(buf.begin(), buf.end(), output.begin()); } - auto split(std::span &data, int depth, const int &mdepth, std::recursive_mutex &mutex) -> void { + auto split(std::span &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 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 cmp; const int max_depth; - std::recursive_mutex mut; }; \ No newline at end of file