Commit 7d635f9d authored by Thomas Kolb's avatar Thomas Kolb
Browse files

Merge remote-tracking branch 'origin/feature/parallel_collision_check' into...

Merge remote-tracking branch 'origin/feature/parallel_collision_check' into feature/boost_drops_mass
parents ea527b5d d06d745f
......@@ -37,7 +37,15 @@ BotThreadPool::BotThreadPool(std::size_t num_threads)
}
if(currentJob) {
currentJob->steps = currentJob->bot->move();
switch(currentJob->jobType) {
case Move:
currentJob->steps = currentJob->bot->move();
break;
case CollisionCheck:
currentJob->killer = currentJob->bot->checkCollision();
break;
}
std::lock_guard<std::mutex> processedQueueGuard(m_processedQueueMutex);
m_processedJobs.push(std::move(currentJob));
......
......@@ -16,12 +16,26 @@ class Bot;
class BotThreadPool
{
public:
enum JobType {
Move,
CollisionCheck
};
struct Job {
JobType jobType;
// inputs
std::shared_ptr<Bot> bot;
// output
// for jobType == Move
std::size_t steps;
// for jobType == CollisionCheck
std::shared_ptr<Bot> killer;
Job(JobType type, const std::shared_ptr<Bot> myBot)
: jobType(type), bot(myBot)
{}
};
private:
......
......@@ -153,20 +153,38 @@ void Field::consumeFood(void)
void Field::moveAllBots(void)
{
// first round: move all bots
for(auto &b : m_bots) {
std::unique_ptr<BotThreadPool::Job> job(new BotThreadPool::Job{b, 0});
std::unique_ptr<BotThreadPool::Job> job(new BotThreadPool::Job(BotThreadPool::Move, b));
m_threadPool.addJob(std::move(job));
}
m_threadPool.waitForCompletion();
// collision check for all bots
// FIXME: make this work without temporary vector
std::vector< std::unique_ptr<BotThreadPool::Job> > tmpJobs;
tmpJobs.reserve(m_bots.size());
std::unique_ptr<BotThreadPool::Job> job;
while((job = m_threadPool.getProcessedJob()) != NULL) {
tmpJobs.push_back(std::move(job));
}
// second round: collision check
for(auto &j : tmpJobs) {
j->jobType = BotThreadPool::CollisionCheck;
m_threadPool.addJob(std::move(j));
}
m_threadPool.waitForCompletion();
// collision check for all bots
while((job = m_threadPool.getProcessedJob()) != NULL) {
std::shared_ptr<Bot> victim = job->bot;
std::size_t steps = job->steps;
std::shared_ptr<Bot> killer = victim->checkCollision();
std::shared_ptr<Bot> killer = job->killer;
if (killer)
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment