Add topology to IslandModel, sending messages mecanism, rewrite Bimap container
This commit is contained in:
parent
95b7b80f19
commit
b3f83717d6
10 changed files with 162 additions and 63 deletions
|
|
@ -27,51 +27,99 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
|||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
paradiseo::smp::IslandModel<EOT>::IslandModel(AbstractTopology& _topo) :
|
||||
topo(_topo)
|
||||
{ }
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::IslandModel<EOT>::add(AIsland<EOT>& _island)
|
||||
{
|
||||
static unsigned i = 0;
|
||||
islands[i] = &_island;
|
||||
islands[i]->setModel(this);
|
||||
i++;
|
||||
islands.push_back(&_island);
|
||||
islands.back()->setModel(this);
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::IslandModel<EOT>::operator()()
|
||||
{
|
||||
std::vector<Thread> threads(islands.size());
|
||||
|
||||
std::vector<std::thread> threads(islands.size());
|
||||
|
||||
// Construct topology according to the number of islands
|
||||
topo.construct(islands.size());
|
||||
|
||||
// Create table
|
||||
table = createTable(topo, islands);
|
||||
|
||||
// Lauching threads
|
||||
unsigned i = 0;
|
||||
for(auto it : islands)
|
||||
{
|
||||
threads[i].start(&AIsland<EOT>::operator(), it.second);
|
||||
threads[i] = std::thread(&AIsland<EOT>::operator(), it);
|
||||
i++;
|
||||
}
|
||||
|
||||
unsigned workingThread = islands.size();
|
||||
while(workingThread > 0)
|
||||
{
|
||||
// Count working islands
|
||||
workingThread = islands.size();
|
||||
for(auto& it : islands)
|
||||
if(it.second->isStopped())
|
||||
if(it->isStopped())
|
||||
workingThread--;
|
||||
|
||||
// Check reception
|
||||
send();
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
|
||||
}
|
||||
|
||||
for(auto& thread : threads)
|
||||
thread.join();
|
||||
|
||||
|
||||
for(auto& message : sentMessages)
|
||||
message.join();
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::IslandModel<EOT>::update(eoPop<EOT> _data, AIsland<EOT>* _island)
|
||||
{
|
||||
std::cout << "Pop reçue par le médiateur" << std::endl;
|
||||
std::lock_guard<std::mutex> lock(this->m);
|
||||
|
||||
std::cout << _data << std::endl;
|
||||
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>::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);
|
||||
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));
|
||||
}
|
||||
listEmigrants.pop();
|
||||
}
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable(AbstractTopology& _topo, std::vector<AIsland<EOT>*>& _islands)
|
||||
{
|
||||
Bimap<unsigned, AIsland<EOT>*> table;
|
||||
unsigned islandId = 0;
|
||||
for(auto it : islands)
|
||||
{
|
||||
table.add(islandId, it);
|
||||
islandId++;
|
||||
}
|
||||
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue