diff --git a/eo/src/apply.h b/eo/src/apply.h index 049983fe..0eb3484e 100644 --- a/eo/src/apply.h +++ b/eo/src/apply.h @@ -37,10 +37,8 @@ template void apply(eoUF& _proc, std::vector& _pop) { -#pragma omp parallel for default(none) shared(_proc, _pop) for (unsigned i = 0; i < _pop.size(); ++i) { -#pragma omp critical _proc(_pop[i]); } } diff --git a/eo/src/omp_apply.h b/eo/src/omp_apply.h new file mode 100644 index 00000000..bbdc4065 --- /dev/null +++ b/eo/src/omp_apply.h @@ -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 +#include + +/** + Applies a unary function to a std::vector of things. + + This is a variant of apply which is called in parallel + thanks to OpenMP. + + @ingroup Utilities +*/ +template +void omp_apply(eoUF& _proc, std::vector& _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 diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 4f194681..fe28b21f 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -65,6 +65,7 @@ SET (TEST_LIST t-eoExtendedVelocity t-eoLogger t-eoIQRStat + t-openmp ) diff --git a/eo/test/t-openmp.cpp b/eo/test/t-openmp.cpp new file mode 100644 index 00000000..d098ac35 --- /dev/null +++ b/eo/test/t-openmp.cpp @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------------- +// t-openmp.cpp +//----------------------------------------------------------------------------- + +#include +#include + +#include +#include + +#include +#include + +#include + +#include "real_value.h" + +//----------------------------------------------------------------------------- + +typedef eoReal< eoMinimizingFitness > EOT; + +//----------------------------------------------------------------------------- + +int main(int ac, char** av) +{ + eoParserLogger parser(ac, av); + eoState state; + + eoRealInitBounded& 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 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; +} + +//-----------------------------------------------------------------------------