Compare commits

..

No commits in common. "75eb7efa114baea76f84212482b17ba4160680e7" and "5981d6d6d9c7718b65dab4ebe840efe8557a2ae0" have entirely different histories.

6 changed files with 36 additions and 23 deletions

3
.gitignore vendored
View File

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

View File

@ -1,12 +1,16 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(aca-tasks) project(task1)
set(CMAKE_CXX_STANDARD 20) 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_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/installed) set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/installed)
if (${CMAKE_BUILD_TYPE} STREQUAL "Release") if (${CMAKE_BUILD_TYPE} STREQUAL "Release")
#set(CMAKE_)
add_compile_options( add_compile_options(
-Wall -Wall
-Wpedantic -Wpedantic

View File

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

View File

@ -5,6 +5,7 @@
#include <chrono> #include <chrono>
#include <cmath> #include <cmath>
#include <mergesort.h>
#include <mergesort_mt.h> #include <mergesort_mt.h>
/* /*
@ -29,7 +30,9 @@ auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
T convbuf; T convbuf;
while (std::getline(stream, buf)) { while (std::getline(stream, buf)) {
convbuf = static_cast<T>(std::stoul(buf)); convbuf = static_cast<T>(std::stoul(buf));
vec.emplace_back(std::move(convbuf)); vec.emplace_back(std::move(convbuf));
} }
@ -54,10 +57,9 @@ auto main(int argc, char *argv[]) -> int {
auto dataset_seq = dataset; auto dataset_seq = dataset;
auto t1 = std::chrono::high_resolution_clock::now(); auto t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t> msst([](int32_t a, int32_t b) { algo::MergeSort_mt::sort(dataset_seq, [](int32_t a, int32_t b) {
return (a > b); return (a > b);
}, 0); }, 0);
msst.sort(dataset_seq);
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 t_seq = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
@ -68,16 +70,14 @@ auto main(int argc, char *argv[]) -> int {
const int max_depth = std::log(threads); const int max_depth = std::log(threads);
t1 = std::chrono::high_resolution_clock::now(); t1 = std::chrono::high_resolution_clock::now();
MergeSorterMT<int32_t> msmt([](int32_t a, int32_t b) { MergeSorterMT<int32_t > ms([](int32_t a, int32_t b) {
return (a>b); return (a>b);
}, max_depth); }, max_depth);
msmt.sort(dataset_par); ms.sort(dataset_par);
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); auto t_par = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fmt::print("\nSorted {} entries within {} ms in parallel on a system having {} threads and a recursion depth of {}" fmt::print("\nSorted {} entries within {} ms in parallel using {} threads and a recursion depth of {}", dataset_seq.size(), t_par.count(), threads, max_depth);
"\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); 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");

0
task1/mergesort.cpp Normal file
View File

View File

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