Compare commits

...

3 Commits

Author SHA1 Message Date
mandlm 6a67e5685f
refactor: extract test runs 2024-03-20 16:37:00 +01:00
mandlm 21ca48b9c2
feat: automatically detect thread count in parallel finder 2024-03-20 16:36:56 +01:00
mandlm ab0613e845
feat: add grouped finder 2024-03-20 16:35:37 +01:00
7 changed files with 135 additions and 40 deletions

View File

@ -2,6 +2,7 @@
#include "./ui_mainwindow.h" #include "./ui_mainwindow.h"
#include "finder.h" #include "finder.h"
#include "grouped_finder.h"
#include "linear_finder.h" #include "linear_finder.h"
#include "parallel_finder.h" #include "parallel_finder.h"
#include "timer.h" #include "timer.h"
@ -9,7 +10,6 @@
#include "word_list_generator.h" #include "word_list_generator.h"
#include <sstream> #include <sstream>
#include <thread>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) { : QMainWindow(parent), ui(new Ui::MainWindow) {
@ -28,6 +28,7 @@ void MainWindow::setupAlgorithmSelector() {
search_algorithms_.appendRow(new QStandardItem("Linear search")); search_algorithms_.appendRow(new QStandardItem("Linear search"));
search_algorithms_.appendRow(new QStandardItem("Parallel search")); search_algorithms_.appendRow(new QStandardItem("Parallel search"));
search_algorithms_.appendRow(new QStandardItem("Tree search")); search_algorithms_.appendRow(new QStandardItem("Tree search"));
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
} }
void MainWindow::generateWordList() { void MainWindow::generateWordList() {
@ -68,12 +69,14 @@ void MainWindow::createSelectedFinder() {
finder_ = std::make_unique<LinearFinder>(word_list_); finder_ = std::make_unique<LinearFinder>(word_list_);
break; break;
case 1: case 1:
finder_ = std::make_unique<ParallelFinder>( finder_ = std::make_unique<ParallelFinder>(word_list_);
word_list_, std::thread::hardware_concurrency());
break; break;
case 2: case 2:
finder_ = std::make_unique<TreeFinder>(word_list_); finder_ = std::make_unique<TreeFinder>(word_list_);
break; break;
case 3:
finder_ = std::make_unique<GroupedFinder>(word_list_);
break;
} }
} }

View File

@ -16,7 +16,9 @@ add_library(
src/parallel_finder.cpp src/parallel_finder.cpp
include/parallel_finder.h include/parallel_finder.h
src/tree_finder.cpp src/tree_finder.cpp
include/tree_finder.h) include/tree_finder.h
src/grouped_finder.cpp
include/grouped_finder.h)
target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include) target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include)

View File

@ -0,0 +1,17 @@
#pragma once
#include "finder.h"
#include <map>
#include <vector>
class GroupedFinder : public Finder {
private:
std::map<char, std::vector<const std::string *>> groups_;
public:
GroupedFinder(const std::vector<string> &word_list);
virtual std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override;
};

View File

@ -6,11 +6,10 @@
class ParallelFinder : public Finder { class ParallelFinder : public Finder {
private: private:
const size_t thread_count_;
const std::vector<string> &word_list_; const std::vector<string> &word_list_;
public: public:
ParallelFinder(const std::vector<string> &word_list, size_t thread_count); ParallelFinder(const std::vector<string> &word_list);
std::forward_list<const std::string *> std::forward_list<const std::string *>
find_prefix(std::string_view search_term) const override; find_prefix(std::string_view search_term) const override;

View File

@ -0,0 +1,64 @@
#include "grouped_finder.h"
#include <mutex>
#include <thread>
#include <vector>
using std::mutex, std::vector, std::thread, std::lock_guard;
GroupedFinder::GroupedFinder(const std::vector<string> &word_list) {
for (const auto &word : word_list) {
groups_[word.front()].push_back(&word);
}
}
std::forward_list<const std::string *>
GroupedFinder::find_prefix(std::string_view search_term) const {
const auto group = groups_.find(search_term.front());
if (group == groups_.cend()) {
return {};
}
const auto word_list = group->second;
const auto word_list_size = word_list.size();
const auto thread_count = std::thread::hardware_concurrency();
forward_list<const string *> matching_words;
mutex matching_words_mutex;
vector<thread> search_threads;
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
const size_t first_word_index =
thread_index * (word_list_size / thread_count);
const size_t last_word_index =
(thread_index == thread_count - 1)
? word_list_size
: (thread_index + 1) * (word_list_size / thread_count);
search_threads.emplace_back(
[](const vector<const string *> &word_list,
const string_view &search_term, forward_list<const string *> &result,
size_t start_index, size_t end_index, mutex &result_mutex) {
forward_list<const string *> thread_results;
for (size_t index = start_index; index < end_index; ++index) {
const auto &current_word = word_list[index];
if (current_word->starts_with(search_term)) {
thread_results.push_front(current_word);
}
}
if (!thread_results.empty()) {
const lock_guard<mutex> lock(result_mutex);
result.merge(thread_results);
}
},
cref(word_list), cref(search_term), ref(matching_words),
first_word_index, last_word_index, ref(matching_words_mutex));
}
for (auto &thread : search_threads) {
thread.join();
}
return matching_words;
}

View File

@ -5,23 +5,26 @@
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list; using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list;
ParallelFinder::ParallelFinder(const vector<string> &word_list, ParallelFinder::ParallelFinder(const vector<string> &word_list)
size_t thread_count) : word_list_(word_list) {}
: word_list_(word_list), thread_count_(thread_count) {}
forward_list<const string *> forward_list<const string *>
ParallelFinder::find_prefix(string_view search_term) const { ParallelFinder::find_prefix(string_view search_term) const {
forward_list<const string *> result; forward_list<const string *> result;
mutex result_mutex; mutex result_mutex;
const auto word_list_size = word_list_.size();
const size_t thread_count = thread::hardware_concurrency();
vector<thread> threads; vector<thread> threads;
for (size_t thread_index = 0; thread_index < thread_count_; ++thread_index) { for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
const size_t first_word_index = const size_t first_word_index =
thread_index * (word_list_.size() / thread_count_); thread_index * (word_list_size / thread_count);
const size_t last_word_index = const size_t last_word_index =
(thread_index == thread_count_ - 1) (thread_index == thread_count - 1)
? word_list_.size() ? word_list_size
: (thread_index + 1) * (word_list_.size() / thread_count_); : (thread_index + 1) * (word_list_size / thread_count);
threads.emplace_back( threads.emplace_back(
[](const vector<string> &word_list, const string_view &search_term, [](const vector<string> &word_list, const string_view &search_term,

View File

@ -1,3 +1,4 @@
#include "grouped_finder.h"
#include "linear_finder.h" #include "linear_finder.h"
#include "parallel_finder.h" #include "parallel_finder.h"
#include "timer.h" #include "timer.h"
@ -24,36 +25,37 @@ vector<string> generate_word_list() {
return word_list; return word_list;
} }
void test_finder_search(Finder &finder, std::string_view name,
std::string_view search_term) {
Timer find_timer;
auto result = finder.find_prefix(search_term);
find_timer.stop();
cout << name << "(" << search_term << ") took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
}
void test_finder(Finder &finder, std::string_view name) {
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
test_finder_search(finder, name, search_term);
}
}
void test_linear_finder(const vector<string> &word_list) { void test_linear_finder(const vector<string> &word_list) {
cout << "\nrunning linear finder" << endl; cout << "\nrunning linear finder" << endl;
LinearFinder linear_finder(word_list); LinearFinder linear_finder(word_list);
Timer find_timer; test_finder(linear_finder, "linear finder");
auto result = linear_finder.find_prefix("ABCD");
find_timer.stop();
cout << "linear finder took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
} }
void test_parallel_finder(const vector<string> &word_list) { void test_parallel_finder(const vector<string> &word_list) {
cout << "\nrunning parallel finder" << endl; cout << "\nrunning parallel finder" << endl;
const size_t thread_count = thread::hardware_concurrency(); ParallelFinder parallel_finder(word_list);
cout << "using " << thread_count << " threads" << endl; test_finder(parallel_finder, "parallel finder");
ParallelFinder parallel_finder(word_list, thread_count);
Timer find_timer;
auto result = parallel_finder.find_prefix("ABCD");
find_timer.stop();
cout << "parallel finder took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
} }
void test_tree_finder(const vector<string> &word_list) { void test_tree_finder(const vector<string> &word_list) {
@ -62,16 +64,20 @@ void test_tree_finder(const vector<string> &word_list) {
Timer constructor_timer; Timer constructor_timer;
TreeFinder tree_finder(word_list); TreeFinder tree_finder(word_list);
constructor_timer.stop(); constructor_timer.stop();
cout << "tree finder constructor took " << constructor_timer << endl; cout << "tree finder constructor took " << constructor_timer << endl;
Timer find_timer; test_finder(tree_finder, "tree finder");
auto result = tree_finder.find_prefix("ABCD"); }
find_timer.stop();
cout << "tree finder took " << find_timer << endl; void test_grouped_finder(const vector<string> &word_list) {
cout << "result list is " << std::distance(result.cbegin(), result.cend()) cout << "\nrunning grouped finder" << endl;
<< " element(s) long" << endl;
Timer constructor_timer;
GroupedFinder grouped_finder(word_list);
constructor_timer.stop();
cout << "grouped finder constructor took " << constructor_timer << endl;
test_finder(grouped_finder, "grouped_finder");
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -82,6 +88,7 @@ int main(int argc, char *argv[]) {
test_linear_finder(word_list); test_linear_finder(word_list);
test_parallel_finder(word_list); test_parallel_finder(word_list);
test_tree_finder(word_list); test_tree_finder(word_list);
test_grouped_finder(word_list);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }