made SearchJob a sub-class
This commit is contained in:
parent
5c85ef13d4
commit
f8729011e8
@ -6,6 +6,6 @@ cmake_minimum_required (VERSION 3.8)
|
|||||||
project ("ParallelSearch")
|
project ("ParallelSearch")
|
||||||
|
|
||||||
# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
|
# Fügen Sie der ausführbaren Datei dieses Projekts eine Quelle hinzu.
|
||||||
add_executable (ParallelSearch "main.cpp" "SearchJob.hpp" "TestRunner.hpp" "ParallelSearch.hpp")
|
add_executable (ParallelSearch "main.cpp" "TestRunner.hpp" "ParallelSearch.hpp")
|
||||||
|
|
||||||
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.
|
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.
|
||||||
|
|||||||
@ -5,75 +5,128 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "SearchJob.hpp"
|
// #include "SearchJob.hpp"
|
||||||
|
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
class ParallelSearch
|
class ParallelSearch
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Type of the search result
|
// Type of the search result
|
||||||
typedef std::vector<Iterator> ResultList;
|
typedef std::vector<Iterator> ResultList;
|
||||||
|
|
||||||
|
class SearchJob
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// initially reserve n entries in result-list (to cut down number of reallocations)
|
||||||
|
enum { INITIAL_RESERVE = 128 };
|
||||||
|
|
||||||
|
// Create partial search job for provided range
|
||||||
|
SearchJob(Iterator begin, Iterator end, const std::string& pattern);
|
||||||
|
// Destructor
|
||||||
|
~SearchJob() {}
|
||||||
|
|
||||||
|
// run the search job and collect results internally
|
||||||
|
void execute();
|
||||||
|
|
||||||
|
// return the result
|
||||||
|
const ResultList& get_result() const { return result_; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
// pattern to search for
|
||||||
|
std::string pattern_;
|
||||||
|
// start of search range
|
||||||
|
Iterator begin_;
|
||||||
|
// end of search range
|
||||||
|
Iterator end_;
|
||||||
|
// result of the search
|
||||||
|
ResultList result_;
|
||||||
|
};
|
||||||
|
|
||||||
// construct parallel search by splitting the search range across <workerCount> SearchJob objects
|
// construct parallel search by splitting the search range across <workerCount> SearchJob objects
|
||||||
ParallelSearch(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 = begin + offset + size;
|
|
||||||
|
|
||||||
// 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
|
||||||
~ParallelSearch()
|
~ParallelSearch() {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// run the search workers and collect results
|
// 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// collect 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& get_result() const { return result_; };
|
const ResultList& get_result() const { return result_; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// jobs to execute
|
// jobs to execute
|
||||||
std::vector<SearchJob<Iterator>> jobList_;
|
std::vector<SearchJob> jobList_;
|
||||||
// result of the search
|
// result of the search
|
||||||
ResultList result_;
|
ResultList result_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
ParallelSearch<Iterator>::SearchJob::SearchJob(Iterator begin, Iterator end, const std::string& pattern) : begin_(begin), end_(end), pattern_(pattern)
|
||||||
|
{
|
||||||
|
result_.reserve(INITIAL_RESERVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
void ParallelSearch<Iterator>::SearchJob::execute()
|
||||||
|
{
|
||||||
|
for (Iterator itr = begin_; itr != end_; itr++)
|
||||||
|
{
|
||||||
|
if (0 == itr->compare(0, pattern_.size(), pattern_))
|
||||||
|
{
|
||||||
|
// found a match, save the index
|
||||||
|
result_.push_back(itr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
ParallelSearch<Iterator>::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 = begin + offset + size;
|
||||||
|
|
||||||
|
// add appropriate search job
|
||||||
|
jobList_.push_back(SearchJob(first, last, pattern));
|
||||||
|
|
||||||
|
// increment offset
|
||||||
|
offset += size;
|
||||||
|
// clear remains (it has been added to first bucket)
|
||||||
|
remains = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Iterator>
|
||||||
|
void ParallelSearch<Iterator>::run()
|
||||||
|
{
|
||||||
|
// start thread for each job
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
for (SearchJob& job : jobList_)
|
||||||
|
{
|
||||||
|
threads.push_back(std::thread(&SearchJob::execute, &job));
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for all threads to join
|
||||||
|
for (std::thread& th : threads)
|
||||||
|
{
|
||||||
|
th.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
// collect the results
|
||||||
|
for (SearchJob& job : jobList_)
|
||||||
|
{
|
||||||
|
result_.insert(result_.end(), job.get_result().begin(), job.get_result().end());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -1,54 +0,0 @@
|
|||||||
#ifndef SEARCHJOB_HPP
|
|
||||||
#define SEARCHJOB_HPP
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
template <typename Iterator>
|
|
||||||
class SearchJob
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// initially reserve n entries in result-list (to cut down number of reallocations)
|
|
||||||
enum { INITIAL_RESERVE = 128 };
|
|
||||||
|
|
||||||
// Type of the search result
|
|
||||||
typedef std::vector<Iterator> ResultList;
|
|
||||||
|
|
||||||
// Create partial search job for provided range
|
|
||||||
SearchJob(Iterator begin, Iterator end, const std::string& pattern) : begin_(begin), end_(end), pattern_(pattern)
|
|
||||||
{
|
|
||||||
result_.reserve(INITIAL_RESERVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructor
|
|
||||||
~SearchJob()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// run the search job and collect results internally
|
|
||||||
void run()
|
|
||||||
{
|
|
||||||
for (Iterator itr = begin_; itr != end_; itr++)
|
|
||||||
{
|
|
||||||
if (0 == itr->compare(0, pattern_.size(), pattern_))
|
|
||||||
{
|
|
||||||
// found a match, save the index
|
|
||||||
result_.push_back(itr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the result
|
|
||||||
const ResultList& get_result() const { return result_; };
|
|
||||||
|
|
||||||
private:
|
|
||||||
// pattern to search for
|
|
||||||
std::string pattern_;
|
|
||||||
// start of search range
|
|
||||||
Iterator begin_;
|
|
||||||
// end of search range
|
|
||||||
Iterator end_;
|
|
||||||
// result of the search
|
|
||||||
ResultList result_;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
14
main.cpp
14
main.cpp
@ -5,16 +5,12 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "SearchJob.hpp"
|
|
||||||
#include "ParallelSearch.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> WordSearch;
|
|
||||||
|
|
||||||
typedef ParallelSearch<WordList::const_iterator> ParallelWordSearch;
|
typedef ParallelSearch<WordList::const_iterator> ParallelWordSearch;
|
||||||
|
|
||||||
void create_testdata(WordList& words)
|
void create_testdata(WordList& words)
|
||||||
@ -37,15 +33,11 @@ void shuffle_testdata(WordList& words)
|
|||||||
std::shuffle(words.begin(), words.end(), rng);
|
std::shuffle(words.begin(), words.end(), rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_wordsearch(WordSearch& search)
|
void run_wordsearch(ParallelWordSearch& search)
|
||||||
{
|
{
|
||||||
search.run();
|
search.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_parallel_wordsearch(ParallelWordSearch& search)
|
|
||||||
{
|
|
||||||
search.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@ -60,13 +52,13 @@ int main()
|
|||||||
time_span = TestRunner(shuffle_testdata, test_data);
|
time_span = TestRunner(shuffle_testdata, test_data);
|
||||||
std::cout << "shuffled test_data " << time_span << " seconds" << std::endl;
|
std::cout << "shuffled test_data " << time_span << " seconds" << std::endl;
|
||||||
|
|
||||||
WordSearch wordSearch(test_data.begin(), test_data.end(), pattern);
|
ParallelWordSearch wordSearch(1, test_data.begin(), test_data.end(), pattern);
|
||||||
|
|
||||||
time_span = TestRunner(run_wordsearch, wordSearch);
|
time_span = TestRunner(run_wordsearch, wordSearch);
|
||||||
std::cout << "linear search found " << wordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
|
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);
|
ParallelWordSearch parallelWordSearch(workerCount, test_data.begin(), test_data.end(), pattern);
|
||||||
time_span = TestRunner(run_parallel_wordsearch, parallelWordSearch);
|
time_span = TestRunner(run_wordsearch, parallelWordSearch);
|
||||||
std::cout << "parallel search found " << parallelWordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
|
std::cout << "parallel search found " << parallelWordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
|
||||||
|
|
||||||
// TODO add validation of results
|
// TODO add validation of results
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user