diff --git a/eo/CMakeLists.txt b/eo/CMakeLists.txt index 7217df60..7bdcdba4 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/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 8536ee0c..8d7956ba 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -65,8 +65,7 @@ SET (TEST_LIST t-eoExtendedVelocity t-eoLogger t-eoIQRStat - t-eoDualFitness - t-eoParser + t-openmp ) @@ -94,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 new file mode 100644 index 00000000..4ccc54ad --- /dev/null +++ b/eo/test/t-openmp.cpp @@ -0,0 +1,117 @@ +//----------------------------------------------------------------------------- +// t-openmp.cpp +//----------------------------------------------------------------------------- + +#include +#include +#include + +#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); + + 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()) + { + parser.printHelp(std::cout); + exit(1); + } + + make_help(parser); + make_verbose(parser); + + rng.reseed( seedParam ); + + eoEvalFuncPtr< EOT, double, const std::vector< double >& > mainEval( real_value ); + eoEvalFuncCounter eval( mainEval ); + + 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; +} + +//-----------------------------------------------------------------------------