added a apply.h variant for parallel execution
This commit is contained in:
parent
ad1ec3669d
commit
ebaf81ae8d
4 changed files with 113 additions and 2 deletions
|
|
@ -37,10 +37,8 @@
|
|||
template <class EOT>
|
||||
void apply(eoUF<EOT&, void>& _proc, std::vector<EOT>& _pop)
|
||||
{
|
||||
#pragma omp parallel for default(none) shared(_proc, _pop)
|
||||
for (unsigned i = 0; i < _pop.size(); ++i)
|
||||
{
|
||||
#pragma omp critical
|
||||
_proc(_pop[i]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
51
eo/src/omp_apply.h
Normal file
51
eo/src/omp_apply.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoApply.h
|
||||
// (c) Maarten Keijzer 2000
|
||||
/*
|
||||
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; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
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: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _omp_apply_h
|
||||
#define _omp_apply_h
|
||||
|
||||
#include <eoFunctor.h>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
Applies a unary function to a std::vector of things.
|
||||
|
||||
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)
|
||||
{
|
||||
#pragma omp parallel for default(none) shared(_proc, _pop)
|
||||
for (unsigned i = 0; i < _pop.size(); ++i)
|
||||
{
|
||||
//#pragma omp critical
|
||||
_proc(_pop[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -65,6 +65,7 @@ SET (TEST_LIST
|
|||
t-eoExtendedVelocity
|
||||
t-eoLogger
|
||||
t-eoIQRStat
|
||||
t-openmp
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
61
eo/test/t-openmp.cpp
Normal file
61
eo/test/t-openmp.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-openmp.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <utils/eoLogger.h>
|
||||
#include <utils/eoParserLogger.h>
|
||||
|
||||
#include <eo>
|
||||
#include <es/make_real.h>
|
||||
|
||||
#include <apply.h>
|
||||
#include <omp_apply.h>
|
||||
|
||||
#include <omp.h>
|
||||
|
||||
#include "real_value.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoReal< eoMinimizingFitness > EOT;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
eoParserLogger parser(ac, av);
|
||||
eoState state;
|
||||
|
||||
eoRealInitBounded<EOT>& init = make_genotype( parser, state, EOT() );
|
||||
eoPop< EOT >& pop = make_pop( parser, state, init );
|
||||
eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value );
|
||||
eoEvalFuncCounter<EOT> eval( mainEval );
|
||||
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(std::cout);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
make_help(parser);
|
||||
make_verbose(parser);
|
||||
|
||||
double ts1 = omp_get_wtime();
|
||||
apply< EOT >( eval, pop );
|
||||
//sleep(1);
|
||||
double ts2 = omp_get_wtime();
|
||||
|
||||
eoPop< EOT >& pop2 = make_pop( parser, state, init );
|
||||
|
||||
double tp1 = omp_get_wtime();
|
||||
omp_apply< EOT >( eval, pop2 );
|
||||
//sleep(1);
|
||||
double tp2 = omp_get_wtime();
|
||||
|
||||
eo::log << "Ts = " << ts2 - ts1 << std::endl;
|
||||
eo::log << "Tp = " << tp2 - tp1 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
Reference in a new issue