From ad1ec3669de3a0657d5a49e274b42ad18cf457be Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Wed, 17 Nov 2010 14:43:18 +0100 Subject: [PATCH 1/4] functor operator applying to population in parallel --- eo/CMakeLists.txt | 6 ++++++ eo/doc/index.h | 4 ++-- eo/src/apply.h | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/eo/CMakeLists.txt b/eo/CMakeLists.txt index eea4df29..07becc2b 100644 --- a/eo/CMakeLists.txt +++ b/eo/CMakeLists.txt @@ -44,6 +44,12 @@ ENABLE_LANGUAGE(C) ### 2) Include required modules / configuration files ##################################################################################### +FIND_PACKAGE(OpenMP REQUIRED) +IF(OPENMP_FOUND) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_CXX_FLAGS}") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_C_FLAGS}") +ENDIF() + INCLUDE(CMakeBackwardCompatibilityCXX) INCLUDE(FindDoxygen) diff --git a/eo/doc/index.h b/eo/doc/index.h index 7d7921fe..2fad16e2 100644 --- a/eo/doc/index.h +++ b/eo/doc/index.h @@ -51,9 +51,9 @@ with a member like: operator()(eoPop). Once called on a given population, i search for the optimum of a given problem. Generally, operators are instanciated once and then binded in an algorithm by reference. -Thus, you can easily build you own algorithm by trying several combination of operators. +Thus, you can easily build your own algorithm by trying several combination of operators. -For an more detailled introduction to the design of %EO you can look at the +For a more detailled introduction to the design of %EO you can look at the slides from a talk at EA 2001 or at the corresponding article in Lecture Notes In Computer Science, 2310, Selected Papers from the 5th European Conference on Artificial Evolution: - http://portal.acm.org/citation.cfm?id=727742 diff --git a/eo/src/apply.h b/eo/src/apply.h index 0eb3484e..049983fe 100644 --- a/eo/src/apply.h +++ b/eo/src/apply.h @@ -37,8 +37,10 @@ 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]); } } From ebaf81ae8d6a5cd944cbd6630369e62b3a4609d4 Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Wed, 17 Nov 2010 17:27:55 +0100 Subject: [PATCH 2/4] added a apply.h variant for parallel execution --- eo/src/apply.h | 2 -- eo/src/omp_apply.h | 51 +++++++++++++++++++++++++++++++++++ eo/test/CMakeLists.txt | 1 + eo/test/t-openmp.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 eo/src/omp_apply.h create mode 100644 eo/test/t-openmp.cpp 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; +} + +//----------------------------------------------------------------------------- From 790153f6aa9cbb0381941f352ecc3c8f845abbde Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Fri, 19 Nov 2010 11:48:42 +0100 Subject: [PATCH 3/4] openmp test updated --- eo/test/t-openmp.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/eo/test/t-openmp.cpp b/eo/test/t-openmp.cpp index d098ac35..0e5f7238 100644 --- a/eo/test/t-openmp.cpp +++ b/eo/test/t-openmp.cpp @@ -28,6 +28,7 @@ int main(int ac, char** av) eoRealInitBounded& init = make_genotype( parser, state, EOT() ); eoPop< EOT >& pop = make_pop( parser, state, init ); + eoPop< EOT >& pop2 = make_pop( parser, state, init ); eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value ); eoEvalFuncCounter eval( mainEval ); @@ -45,8 +46,6 @@ int main(int ac, char** av) //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); From 6625cd247bb2015dbffd58af59a9224d90e6dc5c Mon Sep 17 00:00:00 2001 From: Caner Candan Date: Sat, 20 Nov 2010 01:01:45 +0100 Subject: [PATCH 4/4] openmp testing up-to-date --- eo/test/CMakeLists.txt | 12 ++++++ eo/test/boxplot.py | 10 +++++ eo/test/t-openmp.cpp | 89 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 95 insertions(+), 16 deletions(-) create mode 100755 eo/test/boxplot.py diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index fe28b21f..8d7956ba 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -93,6 +93,18 @@ ELSEIF(ENABLE_CMAKE_TESTING) INSTALL(TARGETS ${test} RUNTIME DESTINATION share/eo/test COMPONENT test) ENDFOREACH (test) + SET(RESOURCES + boxplot.py + ) + + FOREACH(file ${RESOURCES}) + EXECUTE_PROCESS( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_CURRENT_SOURCE_DIR}/${file} + ${CMAKE_CURRENT_BINARY_DIR}/${file} + ) + ENDFOREACH(file) + ENDIF(ENABLE_MINIMAL_CMAKE_TESTING) ###################################################################################### diff --git a/eo/test/boxplot.py b/eo/test/boxplot.py new file mode 100755 index 00000000..0590e4e9 --- /dev/null +++ b/eo/test/boxplot.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python + +from pylab import * +import sys + +DEFAULT_RESULTS_NAME = 'results.txt' + +if __name__ == '__main__': + boxplot( [ [ float(value) for value in line.split() ] for line in open( DEFAULT_RESULTS_NAME if len(sys.argv) < 2 else sys.argv[1] ).readlines() ] ) + show() diff --git a/eo/test/t-openmp.cpp b/eo/test/t-openmp.cpp index 0e5f7238..4ccc54ad 100644 --- a/eo/test/t-openmp.cpp +++ b/eo/test/t-openmp.cpp @@ -2,6 +2,10 @@ // t-openmp.cpp //----------------------------------------------------------------------------- +#include +#include +#include + #include #include @@ -24,13 +28,21 @@ 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 ); - eoPop< EOT >& pop2 = make_pop( parser, state, init ); - eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value ); - eoEvalFuncCounter eval( mainEval ); + unsigned int popMin = parser.getORcreateParam((unsigned int)1, "popMin", "Population Min", 'p', "Evolution Engine").value(); + unsigned int popMax = parser.getORcreateParam((unsigned int)100, "popMax", "Population Max", 'P', "Evolution Engine").value(); + + unsigned int dimMin = parser.getORcreateParam((unsigned int)1, "dimMin", "Dimension Min", 'd', "Evolution Engine").value(); + unsigned int dimMax = parser.getORcreateParam((unsigned int)100, "dimMax", "Dimension Max", 'D', "Evolution Engine").value(); + + unsigned int nRun = parser.getORcreateParam((unsigned int)100, "nRun", "Number of runs", 'r', "Evolution Engine").value(); + + std::string resultsFileName = parser.getORcreateParam(std::string("results.txt"), "resultsFileName", "Results file name", 0, "Evolution Engine").value(); + + double threshold = parser.getORcreateParam((double)3.0, "threshold", "Threshold of max speedup", 0, "Evolution Engine").value(); + + uint32_t seedParam = parser.getORcreateParam((uint32_t)0, "seed", "Random number seed", 0).value(); + if (seedParam == 0) { seedParam = time(0); } if (parser.userNeedsHelp()) { @@ -41,18 +53,63 @@ int main(int ac, char** av) make_help(parser); make_verbose(parser); - double ts1 = omp_get_wtime(); - apply< EOT >( eval, pop ); - //sleep(1); - double ts2 = omp_get_wtime(); + rng.reseed( seedParam ); - double tp1 = omp_get_wtime(); - omp_apply< EOT >( eval, pop2 ); - //sleep(1); - double tp2 = omp_get_wtime(); + eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value ); + eoEvalFuncCounter eval( mainEval ); - eo::log << "Ts = " << ts2 - ts1 << std::endl; - eo::log << "Tp = " << tp2 - tp1 << std::endl; + eoUniformGenerator< double > gen(-5, 5); + + std::ostringstream ss; + ss << resultsFileName << "-p" << popMin << "-P" << popMax << "-d" << dimMin << "-D" << dimMax << "-r" << nRun << "-t" << threshold << "-s" << seedParam; + std::ofstream resultsFile( ss.str().c_str() ); + + for ( size_t p = popMin; p <= popMax; ++p ) + { + for ( size_t d = dimMin; d <= dimMax; ++d ) + { + eo::log << eo::logging << p << 'x' << d << std::endl; + + for ( size_t r = 0; r < nRun; ++r ) + { + eoInitFixedLength< EOT > init( d, gen ); + + double Ts; + double Tp; + + // sequential scope + { + eoPop< EOT > pop( p, init ); + double t1 = omp_get_wtime(); + apply< EOT >(eval, pop); + double t2 = omp_get_wtime(); + Ts = t2 - t1; + } + + // parallel scope + { + eoPop< EOT > pop( p, init ); + double t1 = omp_get_wtime(); + omp_apply< EOT >(eval, pop); + double t2 = omp_get_wtime(); + Tp = t2 - t1; + } + + if ( ( Ts / Tp ) > threshold ) { continue; } + + resultsFile << Ts / Tp << ' '; + + eo::log << eo::debug; + eo::log << "Ts = " << Ts << std::endl; + eo::log << "Tp = " << Tp << std::endl; + eo::log << "S_p = " << Ts / Tp << std::endl; + } // end of runs + + resultsFile << std::endl; + + } // end of dimension + + } // end of population return 0; }