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")
|
||||
|
||||
# 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.
|
||||
|
||||
@ -5,17 +5,84 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include "SearchJob.hpp"
|
||||
// #include "SearchJob.hpp"
|
||||
|
||||
template <typename Iterator>
|
||||
class ParallelSearch
|
||||
{
|
||||
public:
|
||||
|
||||
// Type of the search result
|
||||
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
|
||||
ParallelSearch(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern)
|
||||
ParallelSearch(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern);
|
||||
// Destructor
|
||||
~ParallelSearch() {}
|
||||
|
||||
// run the search workers and collect results
|
||||
void run();
|
||||
|
||||
// return the result
|
||||
const ResultList& get_result() const { return result_; };
|
||||
|
||||
private:
|
||||
// jobs to execute
|
||||
std::vector<SearchJob> jobList_;
|
||||
// result of the search
|
||||
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;
|
||||
@ -30,28 +97,23 @@ public:
|
||||
Iterator last = begin + offset + size;
|
||||
|
||||
// add appropriate search job
|
||||
jobList_.push_back(SearchJob<Iterator>(first, last, pattern));
|
||||
jobList_.push_back(SearchJob(first, last, pattern));
|
||||
|
||||
// increment offset
|
||||
offset += size;
|
||||
// clear remains (it has been added to first bucket)
|
||||
remains = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Destructor
|
||||
~ParallelSearch()
|
||||
{
|
||||
}
|
||||
|
||||
// run the search workers and collect results
|
||||
void run()
|
||||
template <typename Iterator>
|
||||
void ParallelSearch<Iterator>::run()
|
||||
{
|
||||
// start thread for each job
|
||||
std::vector<std::thread> threads;
|
||||
for (SearchJob<Iterator>& job : jobList_)
|
||||
for (SearchJob& job : jobList_)
|
||||
{
|
||||
threads.push_back(std::thread(&SearchJob<Iterator>::run, &job));
|
||||
threads.push_back(std::thread(&SearchJob::execute, &job));
|
||||
}
|
||||
|
||||
// wait for all threads to join
|
||||
@ -61,19 +123,10 @@ public:
|
||||
}
|
||||
|
||||
// collect the results
|
||||
for (SearchJob<Iterator>& job : jobList_)
|
||||
for (SearchJob& job : jobList_)
|
||||
{
|
||||
result_.insert(result_.end(), job.get_result().begin(), job.get_result().end());
|
||||
}
|
||||
}
|
||||
|
||||
// return the result
|
||||
const ResultList& get_result() const { return result_; };
|
||||
|
||||
private:
|
||||
// jobs to execute
|
||||
std::vector<SearchJob<Iterator>> jobList_;
|
||||
// result of the search
|
||||
ResultList result_;
|
||||
};
|
||||
#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 <thread>
|
||||
|
||||
#include "SearchJob.hpp"
|
||||
#include "ParallelSearch.hpp"
|
||||
#include "TestRunner.hpp"
|
||||
|
||||
|
||||
// Type of our hay stack: a list of strings
|
||||
typedef std::vector<std::string> WordList;
|
||||
|
||||
typedef SearchJob<WordList::const_iterator> WordSearch;
|
||||
|
||||
typedef ParallelSearch<WordList::const_iterator> ParallelWordSearch;
|
||||
|
||||
void create_testdata(WordList& words)
|
||||
@ -37,15 +33,11 @@ void shuffle_testdata(WordList& words)
|
||||
std::shuffle(words.begin(), words.end(), rng);
|
||||
}
|
||||
|
||||
void run_wordsearch(WordSearch& search)
|
||||
void run_wordsearch(ParallelWordSearch& search)
|
||||
{
|
||||
search.run();
|
||||
}
|
||||
|
||||
void run_parallel_wordsearch(ParallelWordSearch& search)
|
||||
{
|
||||
search.run();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -60,13 +52,13 @@ int main()
|
||||
time_span = TestRunner(shuffle_testdata, test_data);
|
||||
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);
|
||||
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);
|
||||
time_span = TestRunner(run_wordsearch, parallelWordSearch);
|
||||
std::cout << "parallel search found " << parallelWordSearch.get_result().size() << " matches: " << time_span << " seconds" << std::endl;
|
||||
|
||||
// TODO add validation of results
|
||||
|
||||
Loading…
Reference in New Issue
Block a user