Merge branch 'openmp'

This commit is contained in:
Caner Candan 2011-05-04 17:57:54 +02:00
commit 5ab24a3dd6
18 changed files with 827 additions and 15 deletions

View file

@ -26,8 +26,11 @@
#ifndef _apply_h
#define _apply_h
#include <utils/eoParallel.h>
#include <utils/eoParser.h>
#include <eoFunctor.h>
#include <vector>
#include <omp.h>
/**
Applies a unary function to a std::vector of things.
@ -37,7 +40,76 @@
template <class EOT>
void apply(eoUF<EOT&, void>& _proc, std::vector<EOT>& _pop)
{
for (unsigned i = 0; i < _pop.size(); ++i)
size_t size = _pop.size();
#ifdef _OPENMP
double t1 = 0;
if ( eo::parallel.enableResults() )
{
t1 = omp_get_wtime();
}
if (!eo::parallel.isDynamic())
{
#pragma omp parallel for if(eo::parallel.isEnabled()) //default(none) shared(_proc, _pop, size)
for (size_t i = 0; i < size; ++i) { _proc(_pop[i]); }
}
else
{
#pragma omp parallel for schedule(dynamic) if(eo::parallel.isEnabled())
//doesnot work with gcc 4.1.2
//default(none) shared(_proc, _pop, size)
for (size_t i = 0; i < size; ++i) { _proc(_pop[i]); }
}
if ( eo::parallel.enableResults() )
{
double t2 = omp_get_wtime();
eoLogger log;
log << eo::file(eo::parallel.prefix()) << t2 - t1 << ' ';
}
#else // _OPENMP
for (size_t i = 0; i < size; ++i) { _proc(_pop[i]); }
#endif // !_OPENMP
}
/**
This is a variant of apply<EOT> which is called in parallel
thanks to OpenMP.
@ingroup Utilities
*/
template <class EOT>
void omp_apply(eoUF<EOT&, void>& _proc, std::vector<EOT>& _pop)
{
size_t size = _pop.size();
#pragma omp parallel for if(eo::parallel.isEnabled())
//doesnot work with gcc 4.1.2
//default(none) shared(_proc, _pop, size)
for (size_t i = 0; i < size; ++i)
{
_proc(_pop[i]);
}
}
/**
And now we are using the dynamic scheduling.
@ingroup Utilities
*/
template <class EOT>
void omp_dynamic_apply(eoUF<EOT&, void>& _proc, std::vector<EOT>& _pop)
{
size_t size = _pop.size();
#pragma omp parallel for if(eo::parallel.isEnabled()) schedule(dynamic)
//doesnot work with gcc 4.1.2
//default(none) shared(_proc, _pop, size)
for (size_t i = 0; i < size; ++i)
{
_proc(_pop[i]);
}

View file

@ -201,7 +201,7 @@
#include <utils/eoParserLogger.h>
//-----------------------------------------------------------------------------
#include <utils/eoParallel.h>
#endif

View file

@ -81,6 +81,25 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
replace(_replace)
{}
/** Ctor taking a breed and merge, an overload of ctor to define an offspring size */
eoEasyEA(
eoContinue<EOT>& _continuator,
eoEvalFunc<EOT>& _eval,
eoBreed<EOT>& _breed,
eoReplacement<EOT>& _replace,
unsigned _offspringSize
) : continuator(_continuator),
eval (_eval),
loopEval(_eval),
popEval(loopEval),
selectTransform(dummySelect, dummyTransform),
breed(_breed),
mergeReduce(dummyMerge, dummyReduce),
replace(_replace)
{
offspring.reserve(_offspringSize); // This line avoids an incremental resize of offsprings.
}
/*
eoEasyEA(eoContinue <EOT> & _continuator,
eoPopEvalFunc <EOT> & _pop_eval,
@ -191,7 +210,9 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
/// Apply a few generation of evolution to the population.
virtual void operator()(eoPop<EOT>& _pop)
{
eoPop<EOT> offspring, empty_pop;
_pop.reserve(offspring.capacity());
eoPop<EOT> empty_pop;
popEval(empty_pop, _pop); // A first eval of pop.
@ -270,6 +291,8 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
eoMergeReduce<EOT> mergeReduce;
eoReplacement<EOT>& replace;
eoPop<EOT> offspring;
// Friend classes
friend class eoIslandsEasyEA <EOT> ;
friend class eoDistEvalEasyEA <EOT> ;

View file

@ -66,17 +66,17 @@ class eoGenOp : public eoOp<EOT>, public eoUF<eoPopulator<EOT> &, void>
/** Max production is used to reserve space for all elements that are used by the operator,
not setting it properly can result in a crash
*/
virtual unsigned max_production(void) = 0;
virtual unsigned max_production(void) = 0;
virtual std::string className() const = 0;
virtual std::string className() const = 0;
void operator()(eoPopulator<EOT>& _pop)
{
_pop.reserve(max_production());
apply(_pop);
_pop.reserve( max_production() );
apply(_pop);
}
protected :
//protected :
/** the function that will do the work
*/
virtual void apply(eoPopulator<EOT>& _pop) = 0;

View file

@ -100,6 +100,8 @@ public:
void apply(eoPopulator<EOT>& _pop) {
_pop.reserve( this->max_production() );
position_type pos = _pop.tellp();
for (size_t i = 0; i < rates.size(); ++i) {
_pop.seekp(pos);
@ -108,7 +110,11 @@ public:
// try
// {
// apply it to all the guys in the todo std::list
(*ops[i])(_pop);
//(*ops[i])(_pop);
ops[i]->apply(_pop);
// }
// check for out of individuals and do nothing with that...
// catch(eoPopulator<EOT>::OutOfIndividuals&)

View file

@ -27,7 +27,8 @@ SET (EOUTILS_SOURCES eoData.cpp
make_help.cpp
pipecom.cpp
eoLogger.cpp
eoParserLogger.cpp)
eoParserLogger.cpp
eoParallel.cpp)
ADD_LIBRARY(eoutils STATIC ${EOUTILS_SOURCES})

115
eo/src/utils/eoParallel.cpp Normal file
View file

@ -0,0 +1,115 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
/*
(c) Thales group, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2 of the license.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Caner Candan <caner.candan@thalesgroup.com>
*/
#include <omp.h>
#include "eoParallel.h"
#include "eoLogger.h"
eoParallel::eoParallel() :
_isEnabled( false, "parallelize-loop", "Enable memory shared parallelization into evaluation's loops", '\0' ),
_isDynamic( false, "parallelize-dynamic", "Enable dynamic memory shared parallelization", '\0' ),
_prefix( "results", "parallelize-prefix", "Here's the prefix filename where the results are going to be stored", '\0' ),
_nthreads( 0, "parallelize-nthreads", "Define the number of threads you want to use, nthreads = 0 means you want to use all threads available", '\0' ),
_enableResults( false, "parallelize-enable-results", "Enable the generation of results", '\0' ),
_doMeasure( false, "parallelize-do-measure", "Do some measures during execution", '\0' ),
_t_start(0)
{
}
eoParallel::~eoParallel()
{
#ifdef _OPENMP
if ( doMeasure() )
{
double _t_end = omp_get_wtime();
eoLogger log;
log << eo::file("measure_" + prefix()) << _t_end - _t_start << std::endl;
}
#endif // !_OPENMP
}
std::string eoParallel::className() const
{
return "eoParallel";
}
std::string eoParallel::prefix() const
{
std::string value( _prefix.value() );
if ( _isEnabled.value() )
{
if ( _isDynamic.value() )
{
value += "_dynamic.out";
}
else
{
value += "_parallel.out";
}
}
else
{
value += "_sequential.out";
}
return value;
}
void eoParallel::_createParameters( eoParser& parser )
{
std::string section("Parallelization");
parser.processParam( _isEnabled, section );
parser.processParam( _isDynamic, section );
parser.processParam( _prefix, section );
parser.processParam( _nthreads, section );
parser.processParam( _enableResults, section );
parser.processParam( _doMeasure, section );
}
void make_parallel(eoParser& parser)
{
eo::parallel._createParameters( parser );
#ifdef _OPENMP
if ( eo::parallel.isEnabled() )
{
if ( eo::parallel.nthreads() > 0 )
{
omp_set_num_threads( eo::parallel.nthreads() );
}
}
if ( eo::parallel.doMeasure() )
{
eo::parallel._t_start = omp_get_wtime();
}
#endif // !_OPENMP
}
eoParallel eo::parallel;

88
eo/src/utils/eoParallel.h Normal file
View file

@ -0,0 +1,88 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
/*
(c) Thales group, 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation;
version 2 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: http://eodev.sourceforge.net
Authors:
Caner Candan <caner.candan@thalesgroup.com>
*/
/** @defgroup Parallel Parallel
* @ingroup Utilities
@{
*/
#ifndef eoParallel_h
#define eoParallel_h
#include "eoObject.h"
#include "eoParser.h"
/**
* eoParallel
* Class providing parameters for parallelization
* Use of a global variable eo::parallel to easily use the parallelization parameters anywhere
*/
class eoParallel : public eoObject
{
public:
eoParallel();
~eoParallel();
virtual std::string className() const;
inline bool isEnabled() const { return _isEnabled.value(); }
inline bool isDynamic() const { return _isDynamic.value(); }
std::string prefix() const;
inline unsigned int nthreads() const { return _nthreads.value(); }
inline bool enableResults() const { return _enableResults.value(); }
inline bool doMeasure() const { return _doMeasure.value(); }
friend void make_parallel(eoParser&);
private:
void _createParameters( eoParser& );
private:
eoValueParam<bool> _isEnabled;
eoValueParam<bool> _isDynamic;
eoValueParam<std::string> _prefix;
eoValueParam<unsigned int> _nthreads;
eoValueParam<bool> _enableResults;
eoValueParam<bool> _doMeasure;
double _t_start;
};
void make_parallel(eoParser&);
namespace eo
{
/**
* parallel is an external global variable defined in order to use where ever you want the parallel parameters
*/
extern eoParallel parallel;
}
/** @} */
#endif // !eoParallel_h

View file

@ -61,7 +61,6 @@ eoParameterLoader::~eoParameterLoader()
}
}
eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription,
string _lFileParamName, char _shortHand) :
programName(_argv[0]),

View file

@ -92,8 +92,6 @@ private :
std::vector<eoParam*> ownedParams;
};
/**
eoParser: command line parser and configuration file reader
This class is persistent, so it can be stored and reloaded to restore