Cleanup code
This commit is contained in:
parent
f0b4789e58
commit
75eb7efa11
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
cmake-build-debug
|
cmake-build-*
|
||||||
cmake-build-release
|
|
||||||
.idea
|
.idea
|
||||||
task1/dataset.dat
|
task1/dataset.dat
|
@ -1,16 +1,12 @@
|
|||||||
cmake_minimum_required(VERSION 3.0)
|
cmake_minimum_required(VERSION 3.0)
|
||||||
project(task1)
|
project(aca-tasks)
|
||||||
|
|
||||||
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
|
||||||
|
@ -17,7 +17,6 @@ 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
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <mergesort.h>
|
|
||||||
#include <mergesort_mt.h>
|
#include <mergesort_mt.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -30,9 +29,7 @@ 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +55,7 @@ auto main(int argc, char *argv[]) -> int {
|
|||||||
|
|
||||||
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) {
|
MergeSorterMT<int32_t> msst([](int32_t a, int32_t b) {
|
||||||
return (a>b);
|
return (a > b);
|
||||||
}, 0);
|
}, 0);
|
||||||
msst.sort(dataset_seq);
|
msst.sort(dataset_seq);
|
||||||
auto t2 = std::chrono::high_resolution_clock::now();
|
auto t2 = std::chrono::high_resolution_clock::now();
|
||||||
@ -71,32 +68,34 @@ 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> msmt([](int32_t a, int32_t b) {
|
||||||
return (a>b);
|
return (a > b);
|
||||||
}, max_depth);
|
}, max_depth);
|
||||||
msmt.sort(dataset_par);
|
msmt.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 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);
|
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("\n\n------------Summary------------");
|
||||||
fmt::print("\nt_seq = {: > 5.2f} ms", static_cast<float>(t_seq.count()));
|
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("\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("\nDelta_t = {: > 5.2f} ms", static_cast<float>(t_seq.count() - t_par.count()));
|
||||||
fmt::print("\n-------------------------------");
|
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("\nError writing to file");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto &element : dataset_seq) {
|
for (auto &element: dataset_seq) {
|
||||||
ofile << std::to_string(element) << '\n';
|
ofile << std::to_string(element) << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <span>
|
#include <span>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class MergeSorterMT {
|
class MergeSorterMT {
|
||||||
@ -14,7 +15,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, mut);
|
split(sortable, 0, max_depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -47,14 +48,10 @@ 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, 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) {
|
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) {
|
||||||
@ -71,17 +68,14 @@ private:
|
|||||||
std::span<T> right(mid, data.end());
|
std::span<T> right(mid, data.end());
|
||||||
|
|
||||||
if (depth < mdepth) {
|
if (depth < mdepth) {
|
||||||
//todo: fix lambda call
|
std::thread left_thread([&]() { split(left, depth + 1, mdepth); });
|
||||||
//std::thread left_thread(&MergeSorterMT::split, this, left, depth + 1, mdepth, mutex);
|
std::thread right_thread([&]() { split(right, depth + 1, mdepth); });
|
||||||
//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, mutex);
|
split(left, depth + 1, mdepth);
|
||||||
split(right, depth + 1, mdepth, mutex);
|
split(right, depth + 1, mdepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
merge(data, left, right);
|
merge(data, left, right);
|
||||||
@ -91,5 +85,4 @@ 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;
|
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user