/* Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012 Alexandre Quemy This software is governed by the CeCILL license under French law and abiding by the rules of distribution of free software. You can ue, modify and/ or redistribute the software under the terms of the CeCILL license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info". In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software, that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured and, more generally, to use and operate it in the same conditions as regards security. The fact that you are presently reading this means that you have had knowledge of the CeCILL license and that you accept its terms. ParadisEO WebSite : http://paradiseo.gforge.inria.fr Contact: paradiseo-help@lists.gforge.inria.fr */ #include template paradiseo::smp::Scheduler::Scheduler(unsigned workersNb) : workers(workersNb), popPackages(workersNb), done(false), planning(workersNb), m(workersNb) { } template paradiseo::smp::Scheduler::~Scheduler() { } template void paradiseo::smp::Scheduler::operator()(eoUF& func, eoPop& pop) { // Call the tag dispatcher operator()(func, pop,typename policyTraits::type()); } template void paradiseo::smp::Scheduler::operator()(eoUF& func, eoPop& pop, const LinearPolicy&) { // Determine number of packages according to the number of workers unsigned nbPackages = workers.size(); // Fill packages unsigned nbIndi = pop.size() / nbPackages; unsigned remaining = pop.size() % nbPackages; unsigned indice = 0; for(unsigned i = 0; i < nbPackages; i++) { popPackages[i].clear(); for(unsigned j = 0; j < nbIndi; j++) { popPackages[i].push_back(&pop[i*nbIndi+j]); indice = i*nbIndi+j; } } for(unsigned i = 0; i < remaining; i++) popPackages[i].push_back(&pop[indice+i+1]); // Starting threads for(unsigned i = 0; i < workers.size(); i++) workers[i].start(&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(); } template void paradiseo::smp::Scheduler::operator()(eoUF& func, eoPop& pop, const ProgressivePolicy&) { done = false; for(unsigned i = 0; i < workers.size(); i++) { planning[i] = 2; workers[i].start(&Scheduler::applyProgressivePolicy, this, std::ref(func), std::ref(popPackages[i]), i); } unsigned counter = 0; unsigned j = 0; while(counter < pop.size()) { for(unsigned i = 0; i < workers.size(); i++) { if(popPackages[i].empty() && counter < pop.size()) { j = 0; std::unique_lock lock(m[i]); while (j < planning[i] && counter < pop.size()) { //std::cout << counter << std::endl; popPackages[i].push_back(&pop[counter]); counter++; j++; } planning[i] *= 2; } } /* A nanosleep can increase performances by 10% but as it is not supported by Fedora atm, I deactivate it. */ //std::this_thread::sleep_for(std::chrono::nanoseconds(10)); } done = true; for(unsigned i = 0; i < workers.size(); i++) workers[i].join(); } template void paradiseo::smp::Scheduler::applyLinearPolicy(eoUF& func, std::vector& pop) { for(unsigned i = 0; i < pop.size(); i++) func(*pop[i]); } template void paradiseo::smp::Scheduler::applyProgressivePolicy(eoUF& func, std::vector& pop, int id) { while(!done || !pop.empty()) { std::unique_lock lock(m[id]); for(unsigned i = 0; i < pop.size(); i++) { //std::cout << "." << id << "." << std::endl; func(*pop[i]); } pop.clear(); lock.unlock(); } }