From b0479a15e939be205af6075b79f0b28c3682f8f2 Mon Sep 17 00:00:00 2001 From: quemy Date: Sun, 25 May 2014 21:52:22 +0200 Subject: [PATCH] Fix a bug in SMP Master / Worker model when the population size is lower than the number of workers. --- smp/src/scheduler.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/smp/src/scheduler.cpp b/smp/src/scheduler.cpp index 68b366c08..9a5210e0a 100644 --- a/smp/src/scheduler.cpp +++ b/smp/src/scheduler.cpp @@ -67,17 +67,21 @@ void paradiseo::smp::Scheduler::operator()(eoUF& func, e indice = i*nbIndi+j; } } - - for(unsigned i = 0; i < remaining; i++) - popPackages[i].push_back(&pop[indice+i+1]); + + if(nbIndi != 0) // Handle the offset if there is less individuals than workers + indice++; + for(unsigned i = 0; i < remaining; i++) + popPackages[i].push_back(&pop[indice+i]); // Starting threads for(unsigned i = 0; i < workers.size(); i++) - workers[i] = std::thread(&Scheduler::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i])); - + if(!popPackages[i].empty()) + workers[i] = std::thread(&Scheduler::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i])); + // Wait the end of tasks for(unsigned i = 0; i < workers.size(); i++) - workers[i].join(); + if(!popPackages[i].empty() && workers[i].joinable()) + workers[i].join(); } template @@ -117,7 +121,7 @@ void paradiseo::smp::Scheduler::operator()(eoUF& func, e } done = true; - + for(unsigned i = 0; i < workers.size(); i++) workers[i].join(); }