Add the SMP module

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2714 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
quemy 2012-08-17 11:52:14 +00:00
commit 0890c67d31
43 changed files with 4030 additions and 41 deletions

View file

@ -0,0 +1,43 @@
######################################################################################
### 0) Include directories
######################################################################################
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
######################################################################################
### 1) Set paths
######################################################################################
set(SMP_LIB_OUTPUT_PATH ${SMP_BIN_DIR}/lib)
set(LIBRARY_OUTPUT_PATH ${SMP_LIB_OUTPUT_PATH})
######################################################################################
### 2) Build lib
######################################################################################
set (SMP_FILE
thread.cpp
MWModel.h
scheduler.h)
add_library(smp STATIC ${SMP_FILE})
install(TARGETS smp ARCHIVE DESTINATION ${LIB} COMPONENT libraries)
######################################################################################
### 3) Look for headers
######################################################################################
file(GLOB HDRS SMP)
install(FILES ${HDRS} DESTINATION include${INSTALL_SUB_DIR}/smp COMPONENT headers)
######################################################################################
### 4) Install directories
######################################################################################
install(DIRECTORY algo
DESTINATION include${INSTALL_SUB_DIR}/smp
COMPONENT headers
FILES_MATCHING PATTERN "*.h"
)

View file

@ -0,0 +1,37 @@
/*
<MWAlgo.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 MWAlGO_H
#define MWAlGO_H
#include <MWAlgo/eoEasyEA.cpp>
#include <MWAlgo/eoEasyPSO.cpp>
#include <MWAlgo/eoSyncEasyPSO.cpp>
#endif

View file

@ -0,0 +1,43 @@
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoEasyEA_tag&)
{
if (this->isFirstCall)
{
size_t total_capacity = _pop.capacity() + this->offspring.capacity();
_pop.reserve(total_capacity);
this->offspring.reserve(total_capacity);
this->isFirstCall = false;
}
eoPop<EOT> empty_pop;
this->popEval(empty_pop, _pop); // A first eval of pop.
do
{
try
{
unsigned pSize = _pop.size();
this->offspring.clear(); // new offspring
this->breed(_pop, this->offspring);
scheduler(EOAlgo<EOT>::eval, this->offspring); // eval of parents + offspring if necessary
this->replace(_pop, this->offspring); // after replace, the new pop. is in _pop
if (pSize > _pop.size())
throw std::runtime_error("Population shrinking!");
else if (pSize < _pop.size())
throw std::runtime_error("Population growing!");
}
catch (std::exception& e)
{
std::string s = e.what();
s.append( " in eoEasyEA");
throw std::runtime_error( s );
}
}
while (this->continuator( _pop ) );
}

View file

@ -0,0 +1,35 @@
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoEasyPSO_tag&)
{
try
{
// initializes the topology, velocity, best particle(s)
this->init();
do
{
// loop over all the particles for the current iteration
for (unsigned idx = 0; idx < _pop.size (); idx++)
{
// perform velocity evaluation
this->velocity (_pop[idx],idx);
// apply the flight
this->flight (_pop[idx]);
}
// evaluate the position
scheduler(EOAlgo<EOT>::eval, _pop);
for (unsigned idx = 0; idx < _pop.size (); idx++)
// update the topology (particle and local/global best(s))
this->velocity.updateNeighborhood(_pop[idx],idx);
} while (this->continuator (_pop));
}
catch (std::exception & e)
{
std::string s = e.what ();
s.append (" in eoEasyPSO");
throw std::runtime_error (s);
}
}

View file

@ -0,0 +1,36 @@
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoSyncEasyPSO_tag&)
{
try
{
// initializes the topology, velocity, best particle(s)
this->init();
// just to use a loop eval
eoPop<EOT> empty_pop;
do
{
// perform velocity evaluation
//scheduler(this->velocity, _pop);
this->velocity.apply(_pop);
// apply the flight
this->flight.apply(_pop);
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
scheduler(EOAlgo<EOT>::eval, _pop);
// update the topology (particle and local/global best(s))
this->velocity.updateNeighborhood(_pop);
} while (this->continuator(_pop));
}
catch (std::exception & e)
{
std::string s = e.what ();
s.append (" in eoSyncEasyPSO");
throw std::runtime_error (s);
}
}

71
trunk/smp/src/MWModel.cpp Normal file
View file

@ -0,0 +1,71 @@
/*
<MWModel.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
*/
template<template <class> class EOAlgo, class EOT>
template<class... Args>
paradiseo::smp::MWModel<EOAlgo,EOT>::MWModel (unsigned workersNb, Args&... args) :
EOAlgo<EOT>(args...),
scheduler(workersNb-1)
{ assert(workersNb > 0); }
template<template <class> class EOAlgo, class EOT>
template<class... Args>
paradiseo::smp::MWModel<EOAlgo,EOT>::MWModel (Args&... args) :
MWModel(Thread::hardware_concurrency(), args...)
{}
template<template <class> class EOAlgo, class EOT>
paradiseo::smp::MWModel<EOAlgo,EOT>::~MWModel ()
{}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::apply(eoUF<EOT&, void>& func, eoPop<EOT>& pop)
{
scheduler(func, pop);
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::evaluate(eoPop<EOT>& pop)
{
scheduler(this->eval, pop);
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& pop)
{
// Call the tag dispatcher
operator()(pop,typename traits<EOAlgo,EOT>::type());
}
template<template <class> class EOAlgo, class EOT>
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& pop, const error_tag&)
{
throw std::runtime_error("This is not a valid algorithm");
}

127
trunk/smp/src/MWModel.h Normal file
View file

@ -0,0 +1,127 @@
/*
<MWModel.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 MWMODEL_H_
#define MWMODEL_H_
#include <cassert>
#include <scheduler.h>
#include <tagDispatching.h>
#include <eo>
namespace paradiseo
{
namespace smp
{
/** MWModel: Master / Worker Model for multicore computation
The MW Model wraps any algorithm in order to apply it on several populations.
@see smp::Worker, smp::Thread
*/
template<template <class> class EOAlgo, class EOT>
class MWModel : public EOAlgo<EOT>
{
public:
/**
* Constructor
* @param workersNb the number of workers including the master
* @param args... list of parameters according to the constructor of your algorithm
*/
template<class... Args>
MWModel(unsigned workersNb, Args&... args);
/**
* Constructor
* @param args... list of parameters according to the constructor of your algorithm
*/
template<class... Args>
MWModel(Args&... args);
~MWModel();
/**
* Apply an unary functor to the population
* @param func unary functor
* @param pop population
*/
void apply(eoUF<EOT&, void>& func, eoPop<EOT>& pop);
/**
* Evaluate the population
* @param pop population to evaluate
*/
void evaluate(eoPop<EOT>& pop);
/**
* Run the algorithm on population
* @param pop population to run the algorithm
*/
void operator()(eoPop<EOT>& pop);
protected:
/**
* Specific algorithm for eoEasyEA
*/
void operator()(eoPop<EOT>& pop, const eoEasyEA_tag&);
/**
* Specifid algorithm for EasyPSO
*/
void operator()(eoPop<EOT>& pop, const eoEasyPSO_tag&);
/**
* Specific algorithm for eoSyncEasyPSO
*/
void operator()(eoPop<EOT>& pop, const eoSyncEasyPSO_tag&);
/**
* If we don't know the algorithm type
*/
void operator()(eoPop<EOT>& pop,const error_tag&);
std::vector<Thread*> workers;
Scheduler<EOT> scheduler;
};
#include <MWModel.cpp>
#include <MWAlgo/MWAlgo.h>
}
}
#endif

View file

@ -0,0 +1,97 @@
/*
<scheduler.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
*/
template<class EOT>
paradiseo::smp::Scheduler<EOT>::Scheduler(unsigned workersNb) :
workers(workersNb),
popPackages(workersNb),
done(false),
planning(workersNb),
idWaitingThread(-1)
{ }
template<class EOT>
paradiseo::smp::Scheduler<EOT>::~Scheduler()
{ }
template<class EOT>
void paradiseo::smp::Scheduler<EOT>::operator()(eoUF<EOT&, void>& func, eoPop<EOT>& pop)
{
done = false;
idWaitingThread = -1;
for(unsigned i = 0; i < workers.size(); i++)
{
planning[i] = 2;
workers[i].start(&Scheduler<EOT>::apply, this, std::ref(func), std::ref(popPackages[i]), i);
}
unsigned counter = 0;
unsigned j = 0;
while(counter < pop.size())
{
std::unique_lock<std::mutex> lock(m);
cvt.wait(lock, [this]() -> bool {return (int)idWaitingThread != -1;});
j = 0;
while (j < planning[idWaitingThread] && counter < pop.size())
{
popPackages[idWaitingThread].push_back(&pop[counter]);
counter++;
j++;
}
planning[idWaitingThread] *= 2;
idWaitingThread = -1;
cv.notify_one();
}
done = true;
idWaitingThread = -1;
cv.notify_all();
for(unsigned i = 0; i < workers.size(); i++)
workers[i].join();
}
template<class EOT>
void paradiseo::smp::Scheduler<EOT>::apply(eoUF<EOT&, void>& func, std::vector<EOT*>& pop, int id)
{
while(!done || !pop.empty())
{
for(unsigned i = 0; i < pop.size(); i++)
func(*pop[i]);
pop.clear();
std::unique_lock<std::mutex> lock(m);
idWaitingThread = id;
// We notify the scheduler we finished the package
cvt.notify_one();
// We wait for a new package
cv.wait(lock, [this]() -> bool {return (int)idWaitingThread == -1 || (bool)done;});
}
}

105
trunk/smp/src/scheduler.h Normal file
View file

@ -0,0 +1,105 @@
/*
<scheduler.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 SCHEDULER_H_
#define SCHEDULER_H_
#include <iostream>
#include <vector>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <thread.h>
#include <eoEvalFunc.h>
#include <eoPop.h>
namespace paradiseo
{
namespace smp
{
/**
A scheduler class
*/
template<class EOT>
class Scheduler
{
public:
/**
* Constructor
* @param workersNb number of workers to perform tasks
*/
Scheduler(unsigned workersNb);
/**
* Destructor
*/
~Scheduler();
/**
* Start an unary functor on workers on all the pop
* @param func unary functor
* @param pop reference to the population
*/
void operator()(eoUF<EOT&, void>& func, eoPop<EOT>& pop);
protected:
/**
* Apply an unary functor on a sub-group of population
* @param func unary functor
* @param pop reference to the sub-group
* @param id id of the thread
*/
void apply(eoUF<EOT&, void>& func, std::vector<EOT*>& pop, int id);
/**
* Create sub-groups with similar size from a population.
* @param pop reference to the pop
*/
std::vector<std::vector<EOT*>> subGroups(eoPop<EOT>& pop);
std::vector<Thread> workers;
std::vector<std::vector<EOT*>> popPackages;
std::mutex m;
std::atomic<bool> done;
std::vector<unsigned> planning;
std::atomic<int> idWaitingThread;
std::condition_variable cv;
std::condition_variable cvt;
};
#include <scheduler.cpp>
}
}
#endif /*SCHEDULER_H_*/

35
trunk/smp/src/smp Normal file
View file

@ -0,0 +1,35 @@
/*
<smp>
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 SMP_INCLUDE
#define SMP_INCLUDE
#include <smp.h>
#endif

37
trunk/smp/src/smp.h Normal file
View file

@ -0,0 +1,37 @@
/*
<smp.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 SMP_H
#define SMP_H
#include <thread.h>
#include <MWModel.h>
#include <scheduler.h>
#endif

View file

@ -0,0 +1,68 @@
/*
<tagDispatching.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
*/
#include <eo>
namespace paradiseo
{
namespace smp
{
/** Tag Dispatching
* The Tag Dispatching enable to choose the algorithm to call at compile time
**/
// Main algorithms tags
struct error_tag {};
struct eoEasyEA_tag {};
struct eoEasyPSO_tag {};
struct eoSyncEasyPSO_tag {};
template<template <class> class A, class B>
struct traits {
typedef error_tag type;
};
template<class B>
struct traits<eoEasyEA,B> {
typedef eoEasyEA_tag type;
};
template<class B>
struct traits<eoEasyPSO,B> {
typedef eoEasyPSO_tag type;
};
template<class B>
struct traits<eoSyncEasyPSO,B> {
typedef eoSyncEasyPSO_tag type;
};
}
}

73
trunk/smp/src/thread.cpp Normal file
View file

@ -0,0 +1,73 @@
/*
<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();
}
}
}

112
trunk/smp/src/thread.h Normal file
View file

@ -0,0 +1,112 @@
/*
<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_*/