Clean src. Add setTopology to the island model
This commit is contained in:
parent
cf93d6aeb4
commit
ae4699176f
4 changed files with 31 additions and 35 deletions
|
|
@ -55,11 +55,10 @@ void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
|
|||
{
|
||||
stopped = false;
|
||||
algo(pop);
|
||||
stopped = true;
|
||||
// Let's wait the end of communications with the island model
|
||||
for(auto& message : sentMessages)
|
||||
message.join();
|
||||
stopped = true;
|
||||
//std::cout << "Fin de l'île " << this << std::endl;
|
||||
}
|
||||
|
||||
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>
|
||||
void paradiseo::smp::Island<EOAlgo,EOT>::check()
|
||||
{
|
||||
// std::cout << "On check" << std::endl;
|
||||
// Sending
|
||||
for(PolicyElement<EOT>& elem : migPolicy)
|
||||
{
|
||||
if(!elem(pop))
|
||||
{
|
||||
// std::cout << "On lance l'emmigration" << std::endl;
|
||||
send(elem.getSelect());
|
||||
}
|
||||
}
|
||||
|
||||
// Receiving
|
||||
receive();
|
||||
}
|
||||
|
||||
|
|
@ -98,13 +94,12 @@ bool paradiseo::smp::Island<EOAlgo,EOT>::isStopped(void) const
|
|||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
|
||||
{
|
||||
// Allow island to work alone
|
||||
if(model != nullptr)
|
||||
{
|
||||
//std::cout << "Ile lance la migration" << std::endl;
|
||||
eoPop<EOT> migPop;
|
||||
_select(pop, migPop);
|
||||
//std::cout << " La population migrante est :" << std::endl << migPop << std::endl;
|
||||
|
||||
|
||||
// Delete delivered messages
|
||||
for(auto it = sentMessages.begin(); it != sentMessages.end(); it++)
|
||||
if(!it->joinable())
|
||||
|
|
@ -119,19 +114,12 @@ void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
|
|||
{
|
||||
std::lock_guard<std::mutex> lock(this->m);
|
||||
while (!listImigrants.empty())
|
||||
{
|
||||
|
||||
{
|
||||
eoPop<EOT> offspring = listImigrants.front();
|
||||
|
||||
// Evaluate objects to integrate
|
||||
//std::cout << "Evaluation des individus : " << std::endl;
|
||||
for(auto& indi : offspring)
|
||||
{
|
||||
eval(indi);
|
||||
//std::cout << indi << std::endl;
|
||||
}
|
||||
//std::cout << "Ile " << this << " recoit : " << std::endl;
|
||||
//std::cout << offspring << std::endl;
|
||||
|
||||
intPolicy(pop, offspring);
|
||||
listImigrants.pop();
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
|
|||
// Create table
|
||||
table = createTable(topo, islands);
|
||||
|
||||
// Lauching threads
|
||||
// Launching threads
|
||||
unsigned i = 0;
|
||||
for(auto it : islands)
|
||||
{
|
||||
|
|
@ -72,7 +72,7 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
|
|||
topo.isolateNode(table.getLeft()[it]);
|
||||
}
|
||||
}
|
||||
// Check reception
|
||||
// Check sending
|
||||
send();
|
||||
|
||||
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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::IslandModel<EOT>::setTopology(AbstractTopology& _topo)
|
||||
{
|
||||
topo = _topo;
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::IslandModel<EOT>::send(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
while (!listEmigrants.empty())
|
||||
{
|
||||
//std::cout << "Le mediateur va envoyer de " << listEmigrants.front().second << " qui est " << table.getLeft()[listEmigrants.front().second] << std::endl;
|
||||
|
||||
unsigned id = table.getLeft()[listEmigrants.front().second];
|
||||
std::vector<unsigned> neighbors = topo.getIdNeighbors(id);
|
||||
// Get the neighbors
|
||||
unsigned idFrom = table.getLeft()[listEmigrants.front().second];
|
||||
std::vector<unsigned> neighbors = topo.getIdNeighbors(idFrom);
|
||||
|
||||
// Send elements to neighbors
|
||||
eoPop<EOT> migPop = listEmigrants.front().first;
|
||||
for (unsigned neighbor : neighbors)
|
||||
{
|
||||
//std::cout << "On envoie à " << neighbor << std::endl;
|
||||
sentMessages.push_back(std::thread(&AIsland<EOT>::update, table.getRight()[neighbor], migPop));
|
||||
}
|
||||
for (unsigned idTo : neighbors)
|
||||
sentMessages.push_back(std::thread(&AIsland<EOT>::update, table.getRight()[idTo], migPop));
|
||||
|
||||
listEmigrants.pop();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,6 +74,12 @@ public:
|
|||
*/
|
||||
void update(eoPop<EOT> _data, AIsland<EOT>* _island);
|
||||
|
||||
/**
|
||||
* Change topology
|
||||
* @param _topo New topology.
|
||||
*/
|
||||
void setTopology(AbstractTopology& _topo);
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -87,9 +87,8 @@ int main(void)
|
|||
model.add(test);
|
||||
model.add(test2);
|
||||
|
||||
//model();
|
||||
test();
|
||||
test2();
|
||||
model();
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << test2.getPop() << endl;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue