feat: automatically detect thread count in parallel finder

This commit is contained in:
mandlm 2024-03-20 16:35:57 +01:00
parent ab0613e845
commit 21ca48b9c2
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
3 changed files with 13 additions and 13 deletions

View File

@ -10,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) {
@ -70,8 +69,7 @@ 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_);

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

@ -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,