This commit is contained in:
lasnier 2012-12-05 17:05:00 +01:00
commit f8d2d1cfa5
21 changed files with 452 additions and 406 deletions

View file

@ -17,7 +17,6 @@ set(LIBRARY_OUTPUT_PATH ${SMP_LIB_OUTPUT_PATH})
######################################################################################
set (SMP_FILE
thread.cpp
MWModel.h
scheduler.h
island.h
@ -29,6 +28,7 @@ set (SMP_FILE
topology/ring.cpp
topology/hypercubic.cpp
topology/mesh.cpp
notifier.cpp
)
add_library(smp STATIC ${SMP_FILE})
@ -46,7 +46,7 @@ install(FILES ${HDRS} DESTINATION include${INSTALL_SUB_DIR}/smp COMPONENT header
### 4) Install directories
######################################################################################
install(DIRECTORY MWAlgo
install(DIRECTORY MWAlgo topology
DESTINATION include${INSTALL_SUB_DIR}/smp
COMPONENT headers
FILES_MATCHING PATTERN "*.h"

View file

@ -39,7 +39,7 @@ paradiseo::smp::MWModel<EOAlgo,EOT,Policy>::MWModel(unsigned workersNb, Args&...
template<template <class> class EOAlgo, class EOT, class Policy>
template<class... Args>
paradiseo::smp::MWModel<EOAlgo,EOT,Policy>::MWModel(Args&... args) :
MWModel(Thread::hardware_concurrency(), args...)
MWModel(std::thread::hardware_concurrency(), args...)
{}
template<template <class> class EOAlgo, class EOT, class Policy>

View file

@ -31,6 +31,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#define MWMODEL_H_
#include <cassert>
#include <thread>
#include <scheduler.h>
#include <algoDispatching.h>
@ -113,7 +114,7 @@ protected:
*/
void operator()(eoPop<EOT>& pop,const error_tag&);
std::vector<Thread*> workers;
std::vector<std::thread*> workers;
Scheduler<EOT,Policy> scheduler;
};

View file

@ -51,7 +51,7 @@ The abstract island is used to manipulate island pointers wihout the knowledge o
@see smp::Island
*/
template<class EOT>
template<class bEOT>
class AIsland
{
public:
@ -61,18 +61,7 @@ public:
/**
* Check if there is population to receive
*/
virtual void setModel(IslandModel<EOT>* _model) = 0;
/**
* Send population to mediator
* @param _select Method to select EOT to send
*/
virtual void send(eoSelect<EOT>& _select) = 0;
/**
* Check if there is population to receive
*/
virtual void receive(void) = 0;
virtual void setModel(IslandModel<bEOT>* _model) = 0;
/**
* Check if there is population to receive or to emigrate
@ -82,13 +71,15 @@ public:
/**
* Update the island by adding population to send in the imigrants list.
*/
virtual void update(eoPop<EOT> _data) = 0;
virtual void update(eoPop<bEOT> _data) = 0;
/**
* Check if the algorithm is stopped.
* @return true if stopped
*/
virtual bool isStopped(void) const = 0;
virtual void receive(void) = 0;
protected:
std::mutex m;

View file

@ -28,7 +28,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef BIMAP_MODEL_H_
#define BIMAP_H_
#define BIMAP_MODEL_H_
#include <set>
#include <map>

View file

@ -27,10 +27,10 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
template<class EOT>
paradiseo::smp::ContWrapper<EOT>::ContWrapper(eoContinue<EOT>& _cont, AIsland<EOT>* island) :
template<class EOT, class bEOT>
paradiseo::smp::ContWrapper<EOT, bEOT>::ContWrapper(eoContinue<EOT>& _cont, AIsland<bEOT>* island) :
ck(_cont),
islandNotifier(island, &AIsland<EOT>::check)
islandNotifier(island, &AIsland<bEOT>::check)
{
ck.add(islandNotifier);
}

View file

@ -50,7 +50,7 @@ By using the wrapper, we do not have to modify original continuators inside the
it avoids some side effects.
*/
template<class EOT>
template<class EOT, class bEOT>
class ContWrapper
{
public:
@ -59,11 +59,11 @@ public:
* @param _cont Original continuators
* @param _policy Policy to wrap with continuators
*/
ContWrapper(eoContinue<EOT>& _cont, AIsland<EOT>* island);
ContWrapper(eoContinue<EOT>& _cont, AIsland<bEOT>* island);
protected:
eoCheckPoint<EOT> ck;
IslandNotifier<EOT> islandNotifier;
IslandNotifier<bEOT> islandNotifier;
};
#include <contWrapper.cpp>

View file

@ -27,12 +27,12 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
template<template <class> class EOAlgo, class EOT>
template<template <class> class EOAlgo, class EOT, class bEOT>
template<class... Args>
paradiseo::smp::Island<EOAlgo,EOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
paradiseo::smp::Island<EOAlgo,EOT,bEOT>::Island(std::function<EOT(bEOT&)> _convertFromBase, std::function<bEOT(EOT&)> _convertToBase, eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
// The PPExpander looks for the continuator in the parameters pack.
// The private inheritance of ContWrapper wraps the continuator and add islandNotifier.
ContWrapper<EOT>(Loop<Args...>().template findValue<eoContinue<EOT>>(args...), this),
ContWrapper<EOT, bEOT>(Loop<Args...>().template findValue<eoContinue<EOT>>(args...), this),
// We inject the wrapped continuator by tag dispatching method during the algorithm construction.
algo(EOAlgo<EOT>(wrap_pp<eoContinue<EOT>>(this->ck,args)...)),
// With the PPE we look for the eval function in order to evaluate EOT to integrate
@ -41,14 +41,26 @@ paradiseo::smp::Island<EOAlgo,EOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _in
intPolicy(_intPolicy),
migPolicy(_migPolicy),
stopped(false),
model(nullptr)
model(nullptr),
convertFromBase(_convertFromBase),
convertToBase(_convertToBase)
{
// Check in compile time the inheritance thanks to type_trait.
static_assert(std::is_base_of<eoAlgo<EOT>,EOAlgo<EOT>>::value, "Algorithm must inherit from eoAlgo<EOT>");
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
template<template <class> class EOAlgo, class EOT, class bEOT>
template<class... Args>
paradiseo::smp::Island<EOAlgo,EOT,bEOT>::Island(eoPop<EOT>& _pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args) :
Island(
// Default conversion functions for homogeneous islands
[](bEOT& i) -> EOT { return std::forward<EOT>(i); },
[](EOT& i) -> bEOT { return std::forward<bEOT>(i); },
_pop, _intPolicy, _migPolicy, args...)
{ }
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::operator()()
{
stopped = false;
algo(pop);
@ -58,20 +70,20 @@ void paradiseo::smp::Island<EOAlgo,EOT>::operator()()
message.join();
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::setModel(IslandModel<EOT>* _model)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::setModel(IslandModel<bEOT>* _model)
{
model = _model;
}
template<template <class> class EOAlgo, class EOT>
eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT>::getPop()
template<template <class> class EOAlgo, class EOT, class bEOT>
eoPop<EOT>& paradiseo::smp::Island<EOAlgo,EOT,bEOT>::getPop()
{
return pop;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::check()
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::check()
{
// Sending
for(PolicyElement<EOT>& elem : migPolicy)
@ -82,37 +94,50 @@ void paradiseo::smp::Island<EOAlgo,EOT>::check()
receive();
}
template<template <class> class EOAlgo, class EOT>
bool paradiseo::smp::Island<EOAlgo,EOT>::isStopped(void) const
template<template <class> class EOAlgo, class EOT, class bEOT>
bool paradiseo::smp::Island<EOAlgo,EOT,bEOT>::isStopped(void) const
{
return (bool)stopped;
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::send(eoSelect<EOT>& _select)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::send(eoSelect<EOT>& _select)
{
// Allow island to work alone
if(model != nullptr)
{
eoPop<EOT> migPop;
_select(pop, migPop);
// Convert pop to base pop
eoPop<bEOT> baseMigPop;
for(auto& indi : migPop)
baseMigPop.push_back(convertToBase(indi));
//std::cout << "On envoie de l'île : " << migPop << std::endl;
// Delete delivered messages
for(auto it = sentMessages.begin(); it != sentMessages.end(); it++)
if(!it->joinable())
sentMessages.erase(it);
sentMessages.push_back(std::thread(&IslandModel<EOT>::update, model, migPop, this));
sentMessages.push_back(std::thread(&IslandModel<bEOT>::update, model, baseMigPop, this));
}
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::receive(void)
{
std::lock_guard<std::mutex> lock(this->m);
while (!listImigrants.empty())
{
eoPop<EOT> offspring = listImigrants.front();
//std::cout << "On reçoit dans l'île : " << listImigrants.size() << std::endl;
eoPop<bEOT> base_offspring = listImigrants.front();
// Convert objects from base to our objects type
eoPop<EOT> offspring;
for(auto& indi : base_offspring)
offspring.push_back(convertFromBase(indi));
// Evaluate objects to integrate
for(auto& indi : offspring)
@ -124,9 +149,10 @@ void paradiseo::smp::Island<EOAlgo,EOT>::receive(void)
}
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::Island<EOAlgo,EOT>::update(eoPop<EOT> _data)
template<template <class> class EOAlgo, class EOT, class bEOT>
void paradiseo::smp::Island<EOAlgo,EOT,bEOT>::update(eoPop<bEOT> _data)
{
//std::cout << "On update dans l'île" << std::endl;
std::lock_guard<std::mutex> lock(this->m);
listImigrants.push(_data);
}

View file

@ -60,10 +60,22 @@ The island wraps an algorithm and provide mecanism for emigration and integratio
@see smp::AbstractIsland, smp::MigPolicy
*/
template<template <class> class EOAlgo, class EOT>
class Island : private ContWrapper<EOT>, public AIsland<EOT>
template<template <class> class EOAlgo, class EOT, class bEOT = EOT>
class Island : private ContWrapper<EOT, bEOT>, public AIsland<bEOT>
{
public:
/**
* Constructor
* @param _convertFromBase Function to convert EOT from base EOT
* @param _convertToBase Function to convert base EOT to EOT
* @param _popSize Size of the algorithm population.
* @param _chromInit Population initializer.
* @param _intPolicy Integration policy
* @param _migPolicy Migration policy
* @param args Parameters to construct the algorithm.
*/
template<class... Args>
Island(std::function<EOT(bEOT&)> _convertFromBase, std::function<bEOT(EOT&)> _convertToBase, eoPop<EOT>& pop, IntPolicy<EOT>& _intPolicy, MigPolicy<EOT>& _migPolicy, Args&... args);
/**
* Constructor
* @param _popSize Size of the algorithm population.
@ -84,7 +96,7 @@ public:
* Set model
* @param _model Pointer to the Island Model corresponding
*/
virtual void setModel(IslandModel<EOT>* _model);
virtual void setModel(IslandModel<bEOT>* _model);
/**
* Return a reference to the island population.
@ -101,7 +113,7 @@ public:
* Update the list of imigrants.
* @param _data Elements to integrate in the main population.
*/
void update(eoPop<EOT> _data);
void update(eoPop<bEOT> _data);
/**
* Check if the algorithm is stopped.
@ -109,6 +121,11 @@ public:
*/
virtual bool isStopped(void) const;
/**
* Check if there is population to receive
*/
virtual void receive(void);
protected:
/**
@ -117,20 +134,17 @@ protected:
*/
virtual void send(eoSelect<EOT>& _select);
/**
* Check if there is population to receive
*/
virtual void receive(void);
eoEvalFunc<EOT>& eval;
eoPop<EOT>& pop;
EOAlgo<EOT> algo;
std::queue<eoPop<EOT>> listImigrants;
std::queue<eoPop<bEOT>> listImigrants;
IntPolicy<EOT>& intPolicy;
MigPolicy<EOT>& migPolicy;
std::atomic<bool> stopped;
std::vector<std::thread> sentMessages;
IslandModel<EOT>* model;
IslandModel<bEOT>* model;
std::function<EOT(bEOT&)> convertFromBase;
std::function<bEOT(EOT&)> convertToBase;
};
#include <island.cpp>

View file

@ -48,18 +48,12 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
{
running = true;
// INIT PART
// Create topology, table and initialize islands
initModel();
std::vector<std::thread> threads(islands.size());
// Preparing islands
for(auto it : islands)
it.second = true;
// Construct topology according to the number of islands
topo.construct(islands.size());
// Create table
table = createTable();
// Launching threads
unsigned i = 0;
for(auto it : islands)
@ -68,14 +62,15 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
i++;
}
//
// Lambda function in order to know the number of working islands
std::function<int()> workingIslands = [this]() -> int
{
return (int)std::count_if(std::begin(islands), std::end(islands),
[](std::pair<AIsland<EOT>*, bool>& i) -> bool
{ return i.second; } );
};
// SCHEDULING PART
while(workingIslands() > 0)
{
// Count working islands
@ -93,13 +88,29 @@ void paradiseo::smp::IslandModel<EOT>::operator()()
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
// ENDING PART
// Wait the end of algorithms
for(auto& thread : threads)
thread.join();
// Wait the end of messages sending
for(auto& message : sentMessages)
message.join();
// Force last integration
i = 0;
for(auto it : islands)
{
threads[i] = std::thread(&AIsland<EOT>::receive, it.first);
i++;
}
// Wait the end of the last integration
for(auto& thread : threads)
thread.join();
running = false;
}
@ -107,6 +118,7 @@ template<class EOT>
void paradiseo::smp::IslandModel<EOT>::update(eoPop<EOT> _data, AIsland<EOT>* _island)
{
std::lock_guard<std::mutex> lock(m);
//std::cout << "Mediateur reçoit ! " << _data << std::endl;
listEmigrants.push(std::pair<eoPop<EOT>,AIsland<EOT>*>(_data, _island));
}
@ -119,6 +131,7 @@ void paradiseo::smp::IslandModel<EOT>::setTopology(AbstractTopology& _topo)
// If we change when the algorithm is running, we need to recontruct the topo
if(running)
{
std::cout << "Changing topology" << std::endl;
topo.construct(islands.size());
// If we change the topology during the algorithm, we need to isolate stopped islands
for(auto it : islands)
@ -131,8 +144,9 @@ template<class EOT>
void paradiseo::smp::IslandModel<EOT>::send(void)
{
std::lock_guard<std::mutex> lock(m);
while (!listEmigrants.empty())
if (!listEmigrants.empty())
{
//std::cout << "Mediator envoie ! " << listEmigrants.size() << std::endl;
// Get the neighbors
unsigned idFrom = table.getLeft()[listEmigrants.front().second];
std::vector<unsigned> neighbors = topo.getIdNeighbors(idFrom);
@ -146,6 +160,26 @@ void paradiseo::smp::IslandModel<EOT>::send(void)
}
}
template<class EOT>
bool paradiseo::smp::IslandModel<EOT>::isRunning() const
{
return (bool)running;
}
template<class EOT>
void paradiseo::smp::IslandModel<EOT>::initModel(void)
{
// Preparing islands
for(auto& it : islands)
it.second = true; // Indicate islands are active
// Construct topology according to the number of islands
topo.construct(islands.size());
// Create table
table = createTable();
}
template<class EOT>
Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable()
{
@ -159,9 +193,3 @@ Bimap<unsigned, AIsland<EOT>*> paradiseo::smp::IslandModel<EOT>::createTable()
return table;
}
template<class EOT>
bool paradiseo::smp::IslandModel<EOT>::isRunning() const
{
return (bool)running;
}

View file

@ -52,6 +52,8 @@ island model pattern according to a topology.
@see smp::Island, smp::MigPolicy
*/
template<class EOT>
class IslandModel
{
@ -89,6 +91,11 @@ protected:
*/
void send(void);
/**
* Initialize islands, topology and table before starting the model
*/
void initModel(void);
Bimap<unsigned, AIsland<EOT>*> createTable();
std::queue<std::pair<eoPop<EOT>,AIsland<EOT>*>> listEmigrants;

View file

@ -27,8 +27,6 @@ ParadisEO WebSite : http://paradiseo.gforge.inria.fr
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <typeinfo>
template<class EOT, class Policy>
paradiseo::smp::Scheduler<EOT,Policy>::Scheduler(unsigned workersNb) :
workers(workersNb),
@ -75,7 +73,7 @@ void paradiseo::smp::Scheduler<EOT,Policy>::operator()(eoUF<EOT&, void>& func, e
// Starting threads
for(unsigned i = 0; i < workers.size(); i++)
workers[i].start(&Scheduler<EOT,Policy>::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i]));
workers[i] = std::thread(&Scheduler<EOT,Policy>::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i]));
// Wait the end of tasks
for(unsigned i = 0; i < workers.size(); i++)
@ -90,7 +88,7 @@ void paradiseo::smp::Scheduler<EOT,Policy>::operator()(eoUF<EOT&, void>& func, e
for(unsigned i = 0; i < workers.size(); i++)
{
planning[i] = 2;
workers[i].start(&Scheduler<EOT,Policy>::applyProgressivePolicy, this, std::ref(func), std::ref(popPackages[i]), i);
workers[i] = std::thread(&Scheduler<EOT,Policy>::applyProgressivePolicy, this, std::ref(func), std::ref(popPackages[i]), i);
}
unsigned counter = 0;

View file

@ -35,7 +35,6 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#include <atomic>
#include <mutex>
#include <thread.h>
#include <policiesDispatching.h>
#include <eoEvalFunc.h>
@ -106,7 +105,7 @@ protected:
*/
std::vector<std::vector<EOT*>> subGroups(eoPop<EOT>& pop);
std::vector<Thread> workers;
std::vector<std::thread> workers;
std::vector<std::vector<EOT*>> popPackages;
std::atomic<bool> done;

View file

@ -30,15 +30,16 @@ Contact: paradiseo-help@lists.gforge.inria.fr
#ifndef SMP_H
#define SMP_H
#include <thread.h>
#include <MWModel.h>
#include <scheduler.h>
#include <islandModel.h>
#include <island.h>
#include <abstractIsland.h>
#include <migPolicy.h>
#include <intPolicy.h>
#include <policyElement.h>
#include <islandNotifier.h>
#include <notifier.h>
// Topologies
#include <topology/topology.h>

View file

@ -1,73 +0,0 @@
/*
<thread.cpp>
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 <algorithm>
#include <utility>
#include <thread.h>
namespace paradiseo
{
namespace smp
{
Thread::Thread(Thread&& other)
{
t = std::move(other.t);
}
Thread& Thread::operator=(Thread&& other)
{
t = std::move(other.t);
return *this;
}
const std::thread::id paradiseo::smp::Thread::getId() const
{
return t.get_id();
}
bool paradiseo::smp::Thread::joinable() const
{
return t.joinable();
}
void paradiseo::smp::Thread::join()
{
t.join();
}
int paradiseo::smp::Thread::hardware_concurrency()
{
return std::thread::hardware_concurrency();
}
}
}

View file

@ -1,112 +0,0 @@
/*
<thread.h>
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
*/
#ifndef THREAD_H_
#define THREAD_H_
#include <iostream>
#include <thread>
#include <vector>
namespace paradiseo
{
namespace smp
{
/**
A thread class which encapsulate the std::thread behaviour for more flexibility
@see smp::Worker, smp::MWModel
*/
class Thread
{
public:
/**
* Default constructor
*/
Thread() = default;
/**
* Constructor that will immediatly start the thread
* @param f represente any callable object such as function or class method
* @param args... reference object and parameters for f
*/
template< class Callable, class... Args >
explicit Thread(Callable&& f, Args&&... args) : t(std::thread(std::forward<Callable>(f), std::forward<Args>(args)...)) {}
Thread(Thread&& other);
Thread(const Thread&) = delete;
Thread& operator=(const Thread&) = delete;
virtual ~Thread() = default;
Thread& operator=(Thread&& other);
/**
* Start the thread according to parameters
* If the thread is running, it will wait until the end of its task
* @param f represente any callable object such as function or class method
* @param args... reference object and parameters for f
*/
template<class Callable,class... Args>
void start(Callable&& f,Args&&... args);
/**
* Get the id of the thread
* @return id of the thread
*/
const std::thread::id getId() const;
/**
* Get the state of the thread
* @return true if the thread is running, false otherwise
*/
bool joinable() const;
/**
* Wait until the end of the task
*/
void join();
static int hardware_concurrency();
protected:
std::thread t;
};
template<class Callable,class... Args>
void paradiseo::smp::Thread::start(Callable&& f,Args&&... args)
{
t = std::thread(std::forward<Callable>(f), std::forward<Args>(args)...);
}
}
}
#endif /*THREAD_H_*/

View file

@ -11,7 +11,6 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
######################################################################################
set (TEST_LIST
t-smpThread
t-smpScheduler
t-smpMW_eoEasyEA
t-smpMW_eoEasyPSO
@ -19,6 +18,7 @@ set (TEST_LIST
t-smpIsland
t-smpTopo
t-smpMI_Homogeneous
t-smpMI_Heterogeneous
)
######################################################################################

View file

@ -82,15 +82,6 @@ int main(void)
eoPop<Indi> pop2(param.popSize, chromInit);
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
// Topology
Topology<Complete> topo;
IslandModel<Indi> model(topo);
model.add(test);
model.add(test2);
//model();
test();
test2();

View file

@ -0,0 +1,222 @@
// TODO : Un vrai test, propre, qui veut dire quelque chose :)
#include <smp>
#include <eo>
#include <ga.h>
#include "smpTestClass.h"
using namespace paradiseo::smp;
using namespace std;
typedef eoBit<double> Indi2; // A bitstring with fitness double
// Conversion functions
Indi2 fromBase(Indi& i, unsigned size)
{
// Dummy conversion. We just create a new Indi2
Indi2 v;
for (unsigned ivar=0; ivar<size; ivar++)
{
bool r = rng.flip(); // new value, random in {0,1}
v.push_back(r); // append that random value to v
}
std::cout << "Convert from base : " << v << std::endl;
return v;
}
Indi toBase(Indi2& i)
{
// Dummy conversion. We just create a new Indi
Indi v;
std::cout << "Convert to base : " << v << std::endl;
return v;
}
// EVAL
//-----------------------------------------------------------------------------
// a simple fitness function that computes the number of ones of a bitstring
// @param _Indi2 A biststring Indi2vidual
double binary_value(const Indi2 & _Indi2)
{
double sum = 0;
for (unsigned i = 0; i < _Indi2.size(); i++)
sum += _Indi2[i];
return sum;
}
// GENERAL
//-----------------------------------------------------------------------------
int main(void)
{
// PARAMETRES
// all parameters are hard-coded!
const unsigned int SEED = 42; // seed for random number generator
const unsigned int T_SIZE = 3; // size for tournament selection
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
const unsigned int POP_SIZE = 10; // Size of population
const unsigned int MAX_GEN = 10; // Maximum number of generation before STOP
const float CROSS_RATE = 0.8; // Crossover rate
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
const float MUT_RATE = 1.0; // mutation rate
// GENERAL
//////////////////////////
// Random seed
//////////////////////////
//reproducible random seed: if you don't change SEED above,
// you'll aways get the same result, NOT a random run
rng.reseed(SEED);
// EVAL
/////////////////////////////
// Fitness function
////////////////////////////
// Evaluation: from a plain C++ fn to an EvalFunc Object
eoEvalFuncPtr<Indi2> eval( binary_value );
// INIT
////////////////////////////////
// Initilisation of population
////////////////////////////////
// declare the population
eoPop<Indi2> pop;
// fill it!
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
{
Indi2 v; // void Indi2vidual, to be filled
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
{
bool r = rng.flip(); // new value, random in {0,1}
v.push_back(r); // append that random value to v
}
eval(v); // evaluate it
pop.push_back(v); // and put it in the population
}
// ENGINE
/////////////////////////////////////
// selection and replacement
////////////////////////////////////
// SELECT
// The robust tournament selection
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
// REPLACE
// The simple GA evolution engine uses generational replacement
// so no replacement procedure is needed
// OPERATORS
//////////////////////////////////////
// The variation operators
//////////////////////////////////////
// CROSSOVER
// 1-point crossover for bitstring
eo1PtBitXover<Indi2> xover;
// MUTATION
// standard bit-flip mutation for bitstring
eoBitMutation<Indi2> mutation(P_MUT_PER_BIT);
// STOP
// CHECKPOINT
//////////////////////////////////////
// termination condition
/////////////////////////////////////
// stop after MAX_GEN generations
eoGenContinue<Indi2> continuator(MAX_GEN);
// GENERATION
/////////////////////////////////////////
// the algorithm
////////////////////////////////////////
// standard Generational GA requires as parameters
// selection, evaluation, crossover and mutation, stopping criterion
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi2> criteria(1);
eoDetTournamentSelect<Indi2> selectOne(2);
eoSelectNumber<Indi2> who(selectOne, 1);
MigPolicy<Indi2> migPolicy;
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
// // Integration policy
eoPlusReplacement<Indi2> intPolicy;
// We bind conversion functions
auto frombase = std::bind(fromBase, std::placeholders::_1, VEC_SIZE);
auto tobase = std::bind(toBase, std::placeholders::_1);
Island<eoSGA,Indi2, Indi> gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE,
eval, continuator);
//////////////////////////////////////////////////////////////////
typedef struct {
unsigned popSize = 10;
unsigned tSize = 2;
double pCross = 0.8;
double pMut = 0.7;
unsigned maxGen = 10;
} Param;
Param param;
loadInstances("t-data.dat", n, bkv, a, b);
// Evaluation function
IndiEvalFunc plainEval;
// Init a solution
IndiInit chromInit;
// Define selection
eoDetTournamentSelect<Indi> selectOne2(param.tSize);
eoSelectPerc<Indi> select2(selectOne2);// by default rate==1
// Define operators for crossover and mutation
IndiXover Xover; // CROSSOVER
IndiSwapMutation mutationSwap; // MUTATION
// Encapsule in a tranform operator
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
// Define replace operator
eoPlusReplacement<Indi> replace;
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
// Define population
eoPop<Indi> pop2(param.popSize, chromInit);
// Island 1
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria2(1);
eoDetTournamentSelect<Indi> selectOne3(5);
eoSelectNumber<Indi> who2(selectOne3, 2);
MigPolicy<Indi> migPolicy2;
migPolicy2.push_back(PolicyElement<Indi>(who2, criteria2));
// // Integration policy
eoPlusReplacement<Indi> intPolicy2;
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
// Topology
Topology<Complete> topo;
IslandModel<Indi> model(topo);
model.add(test);
model.add(gga);
model();
cout << test.getPop() << endl;
cout << gga.getPop() << endl;
return 0;
}

View file

@ -6,104 +6,117 @@
using namespace paradiseo::smp;
using namespace std;
void changeTopo(IslandModel<Indi>& _model, AbstractTopology& _topo)
{
static bool first = false;
// Change topology after 1s of computation
std::chrono::milliseconds dura(1000);
std::this_thread::sleep_for( dura );
if(!first)
{
_model.setTopology(_topo);
first = !first;
}
}
int main(void)
{
// Defining parameters
typedef struct {
unsigned popSize = 100;
unsigned popSize = 1000;
unsigned tSize = 2;
double pCross = 0.8;
double pMut = 0.7;
unsigned maxGen = 100;
unsigned maxGen = 1000;
} Param;
Param param;
// Fixing the seed
rng.reseed(42);
// Load instance
loadInstances("t-data.dat", n, bkv, a, b);
// Evaluation function
//Common part to all islands
IndiEvalFunc plainEval;
// Init a solution
IndiInit chromInit;
// Define selection
eoDetTournamentSelect<Indi> selectOne(param.tSize);
eoSelectPerc<Indi> select(selectOne);// by default rate==1
// Define operators for crossover and mutation
IndiXover Xover; // CROSSOVER
IndiSwapMutation mutationSwap; // MUTATION
// Encapsule in a tranform operator
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
// Define replace operator
eoPlusReplacement<Indi> replace;
eoGenContinue<Indi> genCont(param.maxGen+100); // generation continuation
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
eoGenContinue<Indi> genCont_3(param.maxGen); // generation continuation
// Define population
// MODEL
// Topologies
Topology<Complete> topo;
IslandModel<Indi> model(topo);
// ISLAND 1
// // Algorithm part
eoGenContinue<Indi> genCont(param.maxGen+100);
eoPop<Indi> pop(param.popSize, chromInit);
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria(5);
eoDetTournamentSelect<Indi> selectOne1(20);
eoSelectNumber<Indi> who(selectOne1, 3);
Topology<Ring> topo2;
//std::function<void(void)> task = std::bind(changeTopo, model, topo2);
//Notifier topoChanger(task);
MigPolicy<Indi> migPolicy;
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
// // Integration policy
eoPlusReplacement<Indi> intPolicy;
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
// ISLAND 1
// // Algorithm part
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
eoPop<Indi> pop2(30, chromInit);
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria_2(5);
eoDetTournamentSelect<Indi> selectOne_2(25);
eoSelectNumber<Indi> who_2(selectOne_2, 5);
MigPolicy<Indi> migPolicy_2;
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
// // Integration policy
eoPlusReplacement<Indi> intPolicy_2;
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
// Island 3
// // Algorithm part
eoGenContinue<Indi> genCont_3(param.maxGen);
eoPop<Indi> pop3(30, chromInit);
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria_3(10);
eoDetTournamentSelect<Indi> selectOne_3(15);
eoSelectNumber<Indi> who_3(selectOne_3, 1);
MigPolicy<Indi> migPolicy_3;
migPolicy.push_back(PolicyElement<Indi>(who_3, criteria_3));
// // Integration policy
eoPlusReplacement<Indi> intPolicy_3;
Island<eoEasyEA,Indi> test3(pop3, intPolicy_3, migPolicy_3, genCont_3, plainEval, select, transform, replace);
try
{
// Island 1
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria(5);
eoDetTournamentSelect<Indi> selectOne(20);
eoSelectNumber<Indi> who(selectOne, 3);
MigPolicy<Indi> migPolicy;
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
// // Integration policy
eoPlusReplacement<Indi> intPolicy;
eoPop<Indi> pop(param.popSize, chromInit);
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
// Island 2
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria_2(5);
eoDetTournamentSelect<Indi> selectOne_2(25);
eoSelectNumber<Indi> who_2(selectOne_2, 5);
MigPolicy<Indi> migPolicy_2;
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
// // Integration policy
eoPlusReplacement<Indi> intPolicy_2;
eoPop<Indi> pop2(30, chromInit);
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
// Island 3
// // Emigration policy
// // // Element 1
eoPeriodicContinue<Indi> criteria_3(10);
eoDetTournamentSelect<Indi> selectOne_3(15);
eoSelectNumber<Indi> who_3(selectOne_3, 1);
MigPolicy<Indi> migPolicy_3;
migPolicy.push_back(PolicyElement<Indi>(who_3, criteria_3));
// // Integration policy
eoPlusReplacement<Indi> intPolicy_3;
eoPop<Indi> pop3(30, chromInit);
Island<eoEasyEA,Indi> test3(pop3, intPolicy_3, migPolicy_3, genCont_3, plainEval, select, transform, replace);
// Topology
Topology<Complete> topo;
IslandModel<Indi> model(topo);
model.add(test);
model.add(test2);
model.add(test3);

View file

@ -1,60 +0,0 @@
#include <cassert>
#include <vector>
#include <atomic>
#include <smp>
#include "smpTestClass.h"
using namespace std;
using namespace paradiseo::smp;
void f(std::atomic<int> &x)
{
for(int i = 0; i < 100; i++) {
cout << x << endl;
x++;
}
}
void g(std::atomic<int> &x)
{
for(int i = 0; i < 100; i++)
x--;
}
void foo()
{
std::cout << "Foo" << std::endl;
//std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main(void)
{
//---------------------------------------------------
std::atomic<int> nb(0);
Thread t1(&f,std::ref(nb));
Thread t2(&g,std::ref(nb));
t1.join();
t2.join();
assert(nb == 0); // Test atomic_barrier
//--------------------------------------------------
try
{
t1.start(foo);
t1.join();
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 0;
}