Clean src. Add setTopology to the island model

This commit is contained in:
quemy 2012-11-24 16:38:08 +01:00
commit ae4699176f
4 changed files with 31 additions and 35 deletions

View file

@ -55,11 +55,10 @@ void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
{ {
stopped = false; stopped = false;
algo(pop); algo(pop);
stopped = true;
// Let's wait the end of communications with the island model // Let's wait the end of communications with the island model
for(auto& message : sentMessages) for(auto& message : sentMessages)
message.join(); message.join();
stopped = true;
//std::cout << "Fin de l'île " << this << std::endl;
} }
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
@ -77,15 +76,12 @@ eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT>::getPop()
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::check() void paradiseo::smp::Island<EOAlgo,EOT>::check()
{ {
// std::cout << "On check" << std::endl; // Sending
for(PolicyElement<EOT>& elem : migPolicy) for(PolicyElement<EOT>& elem : migPolicy)
{
if(!elem(pop)) if(!elem(pop))
{
// std::cout << "On lance l'emmigration" << std::endl;
send(elem.getSelect()); send(elem.getSelect());
}
} // Receiving
receive(); receive();
} }
@ -98,12 +94,11 @@ bool paradiseo::smp::Island<EOAlgo,EOT>::isStopped(void) const
template<template <class> class EOAlgo, class EOT> template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select) void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
{ {
// Allow island to work alone
if(model != nullptr) if(model != nullptr)
{ {
//std::cout << "Ile lance la migration" << std::endl;
eoPop<EOT> migPop; eoPop<EOT> migPop;
_select(pop, migPop); _select(pop, migPop);
//std::cout << " La population migrante est :" << std::endl << migPop << std::endl;
// Delete delivered messages // Delete delivered messages
for(auto it = sentMessages.begin(); it != sentMessages.end(); it++) for(auto it = sentMessages.begin(); it != sentMessages.end(); it++)
@ -120,18 +115,11 @@ void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
std::lock_guard<std::mutex> lock(this->m); std::lock_guard<std::mutex> lock(this->m);
while (!listImigrants.empty()) while (!listImigrants.empty())
{ {
eoPop<EOT> offspring = listImigrants.front(); eoPop<EOT> offspring = listImigrants.front();
// Evaluate objects to integrate // Evaluate objects to integrate
//std::cout << "Evaluation des individus : " << std::endl;
for(auto& indi : offspring) for(auto& indi : offspring)
{
eval(indi); eval(indi);
//std::cout << indi << std::endl;
}
//std::cout << "Ile " << this << " recoit : " << std::endl;
//std::cout << offspring << std::endl;
intPolicy(pop, offspring); intPolicy(pop, offspring);
listImigrants.pop(); listImigrants.pop();

View file

@ -50,7 +50,7 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
// Create table // Create table
table = createTable(topo, islands); table = createTable(topo, islands);
// Lauching threads // Launching threads
unsigned i = 0; unsigned i = 0;
for(auto it : islands) for(auto it : islands)
{ {
@ -72,7 +72,7 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
topo.isolateNode(table.getLeft()[it]); topo.isolateNode(table.getLeft()[it]);
} }
} }
// Check reception // Check sending
send(); send();
std::this_thread::sleep_for(std::chrono::nanoseconds(10)); std::this_thread::sleep_for(std::chrono::nanoseconds(10));
@ -89,27 +89,30 @@ template<class EOT>
void paradiseo::smp::IslandModel<EOT>::update(eoPop<EOT> _data, AIsland<EOT>* _island) void paradiseo::smp::IslandModel<EOT>::update(eoPop<EOT> _data, AIsland<EOT>* _island)
{ {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
//std::cout << "Pop reçue par le médiateur depuis " << _island << std::endl;
//std::cout << _data << std::endl;
listEmigrants.push(std::pair<eoPop<EOT>,AIsland<EOT>*>(_data, _island)); listEmigrants.push(std::pair<eoPop<EOT>,AIsland<EOT>*>(_data, _island));
} }
template<class EOT>
void paradiseo::smp::IslandModel<EOT>::setTopology(AbstractTopology& _topo)
{
topo = _topo;
}
template<class EOT> template<class EOT>
void paradiseo::smp::IslandModel<EOT>::send(void) void paradiseo::smp::IslandModel<EOT>::send(void)
{ {
std::lock_guard<std::mutex> lock(m); std::lock_guard<std::mutex> lock(m);
while (!listEmigrants.empty()) while (!listEmigrants.empty())
{ {
//std::cout << "Le mediateur va envoyer de " << listEmigrants.front().second << " qui est " << table.getLeft()[listEmigrants.front().second] << std::endl; // Get the neighbors
unsigned idFrom = table.getLeft()[listEmigrants.front().second];
std::vector<unsigned> neighbors = topo.getIdNeighbors(idFrom);
unsigned id = table.getLeft()[listEmigrants.front().second]; // Send elements to neighbors
std::vector<unsigned> neighbors = topo.getIdNeighbors(id);
eoPop<EOT> migPop = listEmigrants.front().first; eoPop<EOT> migPop = listEmigrants.front().first;
for (unsigned neighbor : neighbors) for (unsigned idTo : neighbors)
{ sentMessages.push_back(std::thread(&AIsland<EOT>::update, table.getRight()[idTo], migPop));
//std::cout << "On envoie à " << neighbor << std::endl;
sentMessages.push_back(std::thread(&AIsland<EOT>::update, table.getRight()[neighbor], migPop));
}
listEmigrants.pop(); listEmigrants.pop();
} }
} }

View file

@ -74,6 +74,12 @@ public:
*/ */
void update(eoPop<EOT> _data, AIsland<EOT>* _island); void update(eoPop<EOT> _data, AIsland<EOT>* _island);
/**
* Change topology
* @param _topo New topology.
*/
void setTopology(AbstractTopology& _topo);
protected: protected:
/** /**

View file

@ -87,9 +87,8 @@ int main(void)
model.add(test); model.add(test);
model.add(test2); model.add(test2);
//model(); model();
test();
test2();
cout << test.getPop() << endl; cout << test.getPop() << endl;
cout << test2.getPop() << endl; cout << test2.getPop() << endl;
} }