73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
#include <fmt/format.h>
|
||
#include <vector>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <chrono>
|
||
|
||
#include <mergesort.h>
|
||
|
||
/*
|
||
Create a simple sorting application that uses the mergesort algorithm to sort a
|
||
large collection (e.g., 10^7 ) of 32-bit integers. The input data and output results
|
||
should be stored in files, and the I/O operations should be considered a
|
||
sequential part of the application. Mergesort is an algorithm that is considered
|
||
appropriate for parallel execution, although it cannot be equally divided between
|
||
an arbitrary number of processors, as Amdahl’s and Gustafson-Barsis’ laws
|
||
require.
|
||
Assuming that this equal division is possible, estimate α, i.e., the part of the
|
||
program that can be parallelized, by using a profiler like gprof or valgrind to
|
||
measure the duration of mergesort’s execution relative to the overall execution
|
||
time. Use this number to estimate the predicted speedup for your program.
|
||
Does α depend on the size of the input? If it does, how should you modify
|
||
your predictions and their graphical illustration?
|
||
*/
|
||
|
||
template<typename T>
|
||
auto parse_file(std::ifstream &stream, std::vector<T> &vec) -> void {
|
||
std::string buf;
|
||
T convbuf;
|
||
|
||
while (std::getline(stream, buf)) {
|
||
|
||
convbuf = static_cast<T>(std::stoul(buf));
|
||
|
||
vec.emplace_back(std::move(convbuf));
|
||
}
|
||
|
||
}
|
||
|
||
int main(int argc, char *argv[]) {
|
||
try {
|
||
std::ifstream file("dataset.dat", std::ios_base::in);
|
||
if (!file.is_open()) {
|
||
fmt::print("Error opening file");
|
||
return -1;
|
||
}
|
||
|
||
fmt::print("Opened file {} sucessfully!\n", "dummy");
|
||
std::vector<int32_t> dataset;
|
||
|
||
parse_file(file, dataset);
|
||
fmt::print("Read {} values from {}\n", dataset.size(), "dummy");
|
||
|
||
auto t1 = std::chrono::high_resolution_clock::now();
|
||
|
||
algo::mergesort(dataset.begin(), dataset.end(), [](int32_t a, int32_t b) {
|
||
return (a > b);
|
||
});
|
||
|
||
auto t2 = std::chrono::high_resolution_clock::now();
|
||
|
||
|
||
auto delay_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
|
||
fmt::print("Sorted {} entries within {} ms.", dataset.size(), delay_ms.count());
|
||
|
||
return 0;
|
||
|
||
} catch (std::exception e) {
|
||
fmt::print("Could not open file");
|
||
return -1;
|
||
}
|
||
|
||
}
|