basically working ... needs polish
This commit is contained in:
parent
60ac1ce373
commit
6f3a548bc7
@ -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")
|
||||
add_executable (ParallelSearch "main.cpp" "SearchJob.hpp" "TestRunner.hpp" "ParallelSearch.hpp")
|
||||
|
||||
# TODO: Fügen Sie bei Bedarf Tests hinzu, und installieren Sie Ziele.
|
||||
|
||||
@ -10,6 +10,18 @@
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "",
|
||||
"ctestCommandArgs": ""
|
||||
},
|
||||
{
|
||||
"name": "x64-Release",
|
||||
"generator": "Ninja",
|
||||
"configurationType": "RelWithDebInfo",
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}",
|
||||
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||
"cmakeCommandArgs": "",
|
||||
"buildCommandArgs": "",
|
||||
"ctestCommandArgs": "",
|
||||
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||
"variables": []
|
||||
}
|
||||
]
|
||||
}
|
||||
43
ParallelSearch.hpp
Normal file
43
ParallelSearch.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef PARALLELSEARCH_HPP
|
||||
#define PARALLELSEARCH_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "SearchJob.hpp"
|
||||
|
||||
template <typename Iterator>
|
||||
class ParallelSearch
|
||||
{
|
||||
public:
|
||||
// Type of the search result
|
||||
typedef std::vector<Iterator> ResultList;
|
||||
// underlying search jobs
|
||||
typedef std::vector<SearchJob<Iterator>> JobList;
|
||||
|
||||
// Create partial search job for provided range
|
||||
SearchJob(unsigned workerCount, Iterator begin, Iterator end, const std::string& pattern)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
~SearchJob()
|
||||
{
|
||||
}
|
||||
|
||||
// run the search job and collect results internally
|
||||
void run()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// return the result
|
||||
const ResultList& GetResult() const { return result_; };
|
||||
|
||||
private:
|
||||
// jobs to execute
|
||||
JobList jobList_;
|
||||
// result of the search
|
||||
ResultList result_;
|
||||
};
|
||||
#endif
|
||||
64
main.cpp
64
main.cpp
@ -3,6 +3,7 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include "SearchJob.hpp"
|
||||
#include "TestRunner.hpp"
|
||||
@ -13,6 +14,9 @@ typedef std::vector<std::string> WordList;
|
||||
|
||||
typedef SearchJob<WordList::const_iterator> WordSearchJob;
|
||||
|
||||
typedef std::vector<WordSearchJob> SearchJobList;
|
||||
|
||||
|
||||
void create_testdata(WordList& words)
|
||||
{
|
||||
const char start = 'A';
|
||||
@ -24,6 +28,7 @@ void create_testdata(WordList& words)
|
||||
for (str[2] = start; str[2] <= end; str[2]++)
|
||||
for (str[3] = start; str[3] <= end; str[3]++)
|
||||
words.push_back(std::string(str, 4));
|
||||
|
||||
}
|
||||
|
||||
void shuffle_testdata(WordList& words)
|
||||
@ -37,10 +42,26 @@ void run_wordsearch(WordSearchJob& job)
|
||||
job.run();
|
||||
}
|
||||
|
||||
void run_parallel_wordsearch(std::vector<WordSearchJob>& jobList)
|
||||
{
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
for (WordSearchJob& job : jobList)
|
||||
{
|
||||
threads.push_back(std::thread(&WordSearchJob::run, &job));
|
||||
}
|
||||
|
||||
for (std::thread& th : threads)
|
||||
{
|
||||
th.join();
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double time_span;
|
||||
std::string pattern = "ABC";
|
||||
|
||||
WordList test_data;
|
||||
|
||||
std::cout << "Hello World." << std::endl;
|
||||
@ -51,12 +72,53 @@ int main()
|
||||
time_span = TestRunner(shuffle_testdata, test_data);
|
||||
std::cout << "It took me " << time_span << " seconds to shuffle " << test_data.size() << " words." << std::endl;
|
||||
|
||||
SearchJob<WordList::const_iterator> job(test_data.begin(), test_data.end(), "ABC");
|
||||
SearchJob<WordList::const_iterator> job(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;
|
||||
|
||||
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user