code cleanup: moved parallel search into template class

This commit is contained in:
Karsten Laux 2021-12-15 18:07:21 +01:00
parent 6f3a548bc7
commit 363580cdf8
4 changed files with 66 additions and 81 deletions

View File

@ -14,14 +14,13 @@
{ {
"name": "x64-Release", "name": "x64-Release",
"generator": "Ninja", "generator": "Ninja",
"configurationType": "RelWithDebInfo", "configurationType": "Release",
"buildRoot": "${projectDir}\\out\\build\\${name}", "buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}", "installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "", "cmakeCommandArgs": "",
"buildCommandArgs": "", "buildCommandArgs": "",
"ctestCommandArgs": "", "ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ], "inheritEnvironments": [ "msvc_x64_x64" ]
"variables": []
} }
] ]
} }

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <iostream>
#include <thread>
#include "SearchJob.hpp" #include "SearchJob.hpp"
template <typename Iterator> template <typename Iterator>
@ -11,32 +13,66 @@ class ParallelSearch
public: public:
// Type of the search result // Type of the search result
typedef std::vector<Iterator> ResultList; typedef std::vector<Iterator> ResultList;
// underlying search jobs
typedef std::vector<SearchJob<Iterator>> JobList;
// Create partial search job for provided range // construct parallel search by splitting the search range across <workerCount> SearchJob objects
SearchJob(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern) ParallelSearch(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern)
{ {
std::size_t totalCount = end - begin;
std::size_t bucketSize = totalCount / workerCount;
std::size_t remains = totalCount % workerCount;
std::size_t offset = 0;
while (offset < totalCount)
{
// compute [first; last( iterators for sub-range
std::size_t size = bucketSize + remains;
Iterator first = begin + offset;
Iterator last = first + bucketSize + remains;
// add appropriate search job
jobList_.push_back(SearchJob<Iterator>(first, last, pattern));
// increment offset
offset += size;
// clear remains (it has been added to first bucket)
remains = 0;
}
} }
// Destructor // Destructor
~SearchJob() ~ParallelSearch()
{ {
} }
// run the search job and collect results internally // run the search workers and collect results
void run() void run()
{ {
// start thread for each job
std::vector<std::thread> threads;
for (SearchJob<Iterator>& job : jobList_)
{
threads.push_back(std::thread(&SearchJob<Iterator>::run, &job));
}
// wait for all threads to join
for (std::thread& th : threads)
{
th.join();
}
// collec the results
for (SearchJob<Iterator>& job : jobList_)
{
result_.insert(result_.end(), job.get_result().begin(), job.get_result().end());
}
} }
// return the result // return the result
const ResultList& GetResult() const { return result_; }; const ResultList& get_result() const { return result_; };
private: private:
// jobs to execute // jobs to execute
JobList jobList_; std::vector<SearchJob<Iterator>> jobList_;
// result of the search // result of the search
ResultList result_; ResultList result_;
}; };

View File

@ -39,7 +39,7 @@ public:
} }
// return the result // return the result
const ResultList& GetResult() const { return result_; }; const ResultList& get_result() const { return result_; };
private: private:
// pattern to search for // pattern to search for

View File

@ -6,16 +6,16 @@
#include <thread> #include <thread>
#include "SearchJob.hpp" #include "SearchJob.hpp"
#include "ParallelSearch.hpp"
#include "TestRunner.hpp" #include "TestRunner.hpp"
// Type of our hay stack: a list of strings // Type of our hay stack: a list of strings
typedef std::vector<std::string> WordList; typedef std::vector<std::string> WordList;
typedef SearchJob<WordList::const_iterator> WordSearchJob; typedef SearchJob<WordList::const_iterator> WordSearch;
typedef std::vector<WordSearchJob> SearchJobList;
typedef ParallelSearch<WordList::const_iterator> ParallelWordSearch;
void create_testdata(WordList& words) void create_testdata(WordList& words)
{ {
@ -37,87 +37,37 @@ void shuffle_testdata(WordList& words)
std::shuffle(words.begin(), words.end(), rng); std::shuffle(words.begin(), words.end(), rng);
} }
void run_wordsearch(WordSearchJob& job) void run_wordsearch(WordSearch& search)
{ {
job.run(); search.run();
} }
void run_parallel_wordsearch(std::vector<WordSearchJob>& jobList) void run_parallel_wordsearch(ParallelWordSearch& search)
{ {
std::vector<std::thread> threads; search.run();
for (WordSearchJob& job : jobList)
{
threads.push_back(std::thread(&WordSearchJob::run, &job));
}
for (std::thread& th : threads)
{
th.join();
}
} }
int main() int main()
{ {
double time_span; double time_span;
std::string pattern = "ABC"; std::string pattern = "ABC";
unsigned workerCount = std::thread::hardware_concurrency();
WordList test_data; WordList test_data;
std::cout << "Hello World." << std::endl;
time_span = TestRunner(create_testdata, test_data); time_span = TestRunner(create_testdata, test_data);
std::cout << "It took me " << time_span << " seconds to create " << test_data.size() << " words." << std::endl; std::cout << "created test_data (" << test_data.size() << " words) " << time_span << " seconds" << std::endl;
time_span = TestRunner(shuffle_testdata, test_data); time_span = TestRunner(shuffle_testdata, test_data);
std::cout << "It took me " << time_span << " seconds to shuffle " << test_data.size() << " words." << std::endl; std::cout << "shuffled test_data " << time_span << " seconds" << std::endl;
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), pattern); WordSearch wordSearch(test_data.begin(), test_data.end(), pattern);
time_span = TestRunner(run_wordsearch, job);
std::cout << "It took me " << time_span << " seconds to find " << job.GetResult().size() << " matches." << std::endl;
std::vector<WordSearchJob> jobList;
unsigned coreCount = std::thread::hardware_concurrency();
std::size_t bucketSize = test_data.size() / coreCount;
std::size_t remains = test_data.size() % coreCount;
std::cout << "Creating " << coreCount << " search jobs, bucketSize = " << bucketSize << std::endl;
std::size_t offset = 0;
while(offset < test_data.size())
{
// compute [first; last( iterators for sub-range
std::size_t size = bucketSize + remains;
WordList::const_iterator first = test_data.begin() + offset;
WordList::const_iterator last = first + bucketSize + remains;
std::cout << "offset = " << offset << " size = " << size << std::endl;
// add appropriate search job
jobList.push_back(WordSearchJob(first, last, pattern));
// increment offset
offset += size;
// clear remains (it has been added to first bucket)
remains = 0;
}
time_span = TestRunner(run_parallel_wordsearch, jobList);
// collect search results ...
unsigned found = 0;
for (WordSearchJob& job : jobList)
{
found += job.GetResult().size();
}
std::cout << "It took me " << time_span << " seconds to find " << found << " matches." << std::endl;
time_span = TestRunner(run_wordsearch, wordSearch);
std::cout << "linear search found " << wordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
ParallelWordSearch parallelWordSearch(workerCount, test_data.begin(), test_data.end(), pattern);
time_span = TestRunner(run_parallel_wordsearch, parallelWordSearch);
std::cout << "linear search found " << parallelWordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
std::cout << std::endl; std::cout << std::endl;