From b446ff3ac5d16bf95ab659eb0e46d407191b9188 Mon Sep 17 00:00:00 2001 From: boufaras Date: Tue, 25 Oct 2011 13:45:20 +0000 Subject: [PATCH] git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2509 331e1502-861f-0410-8da2-ba01fb791d7f --- .../tutorial/QAP_CPU/CMakeLists.txt | 1 + .../QAP_CPU/application/CMakeLists.txt | 39 +++ .../QAP_CPU/application/testSimpleHC_CPU.cu | 216 ++++++++++++++++ .../QAP_CPU/application/testSimpleTS_CPU.cu | 239 ++++++++++++++++++ .../tutorial/QAP_CPU/src/Problem.h | 62 +++++ .../tutorial/QAP_CPU/src/QapEval.h | 75 ++++++ .../tutorial/QAP_CPU/src/QapIncrEval.h | 100 ++++++++ 7 files changed, 732 insertions(+) create mode 100755 trunk/paradiseo-gpu/tutorial/QAP_CPU/CMakeLists.txt create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/application/CMakeLists.txt create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleHC_CPU.cu create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleTS_CPU.cu create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/src/Problem.h create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/src/QapEval.h create mode 100644 trunk/paradiseo-gpu/tutorial/QAP_CPU/src/QapIncrEval.h diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/CMakeLists.txt b/trunk/paradiseo-gpu/tutorial/QAP_CPU/CMakeLists.txt new file mode 100755 index 000000000..8bf58768d --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(application) \ No newline at end of file diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/CMakeLists.txt b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/CMakeLists.txt new file mode 100644 index 000000000..9e68a1683 --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/CMakeLists.txt @@ -0,0 +1,39 @@ +###################################################################################### +### 1) Include the sources +###################################################################################### + +INCLUDE_DIRECTORIES( + + # include CUDA source directory + ${CUDA_SRC_DIR} + # include EO source directory + ${PARADISEO_EO_SRC_DIR}/src + # include MO source directory + ${PARADISEO_MO_SRC_DIR}/src + # include problems directory + ${PARADISEO_PROBLEMS_SRC_DIR} + # include GPU directory + ${PARADISEO_GPU_SRC_DIR} + # include your source directory + ${CMAKE_CURRENT_SOURCE_DIR}/../src + ) + +###################################################################################### + +###################################################################################### +### 2) Specify where CMake can find the libraries +###################################################################################### + +LINK_DIRECTORIES(${PARADISEO_EO_BIN_DIR}/lib ${CUDA_LIB_DIR} ) + +###################################################################################### + +###################################################################################### +### 3) Define your targets and link the librairies +###################################################################################### + +CUDA_ADD_EXECUTABLE(CutestSimpleTS_CPU testSimpleTS_CPU.cu) +CUDA_ADD_EXECUTABLE(CutestSimpleHC_CPU testSimpleHC_CPU.cu) + +TARGET_LINK_LIBRARIES(CutestSimpleTS_CPU eoutils ga eo) +TARGET_LINK_LIBRARIES(CutestSimpleHC_CPU eoutils ga eo) \ No newline at end of file diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleHC_CPU.cu b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleHC_CPU.cu new file mode 100644 index 000000000..20a1a6d9c --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleHC_CPU.cu @@ -0,0 +1,216 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Karima Boufaras, Thé Van LUONG + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#include +#include +#include +using namespace std; + +//Specific data problem +unsigned * a; +unsigned * b; +unsigned n; + +// The general include for eo +#include +#include +// Fitness function +#include +// Cuda Fitness function +#include +// QAP solution +#include +#include +//QAP neighbor +#include +//QAP neighborhood +#include +//QAP data +#include +// The Solution and neighbor comparator +#include +#include +// The Iter continuator +#include +// Local search algorithm +#include +// The Tabou Search algorithm explorer +#include +//To compute execution time +#include + + +typedef eoInt solution; +typedef moIndexedSwapNeighbor Neighbor; +typedef moOrderNeighborhood Neighborhood; + + +int main(int argc, char **argv) +{ + + /* ========================================================= + * + * Parameters + * + * ========================================================= */ + + // First define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameter, read it through the parser, + // and assign the value to the variable + + // seed + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; // default value + eoValueParam statusParam(str_status.c_str(), "status", "Status file"); + parser.processParam( statusParam, "Persistence" ); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) { + parser.printHelp(cout); + exit(1); + } + if (statusParam.value() != "") { + ofstream os(statusParam.value().c_str()); + os << parser;// and you can use that file as parameter file + } + /* ========================================================= + * + * Random seed + * + * ========================================================= */ + + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(seed); + + /* ========================================================= + * + * Initilisation of QAP data + * + * ========================================================= */ + + load(argv[1]); + + /* ========================================================= + * + * Initilisation of the solution + * + * ========================================================= */ + + eoInitPermutation init(n); + solution sol; + init(sol); + + /* ========================================================= + * + * Evaluation of a solution neighbor's + * + * ========================================================= */ + + QapEval eval; + QapIncrEval incr_eval; + + /* ========================================================= + * + * Comparator of solutions and neighbors + * + * ========================================================= */ + + moNeighborComparator comparator; + moSolNeighborComparator solComparator; + + /* ========================================================= + * + * a solution neighborhood + * + * ========================================================= */ + + Neighborhood neighborhood((n*(n-1))/2); + + /* ========================================================= + * + * continuator + * + * ========================================================= */ + + moTrueContinuator continuator; + + /* ========================================================= + * + * An explorer of solution neighborhood's + * + * ========================================================= */ + + moSimpleHCexplorer explorer(neighborhood, incr_eval, comparator, solComparator); + + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moLocalSearch localSearch(explorer, continuator, eval); + + /* ========================================================= + * + * Execute the local search from random sollution + * + * ========================================================= */ + + //Can be eval here, else it will be done at the beginning of the localSearch + eval(sol); + + std::cout << "initial: " << sol<< std::endl; + // Create timer for timing CUDA calculation + moGPUTimer timer; + timer.start(); + localSearch(sol); + timer.stop(); + std::cout << "final: " << sol << std::endl; + printf("Execution time = %.2lf s\n",timer.getTime()); + + delete[] a; + delete[] b; + + + return 0; +} diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleTS_CPU.cu b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleTS_CPU.cu new file mode 100644 index 000000000..428a648cc --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/application/testSimpleTS_CPU.cu @@ -0,0 +1,239 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Karima Boufaras, Thé Van LUONG + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#include +#include +#include +using namespace std; + +//Specific data problem +unsigned * a; +unsigned * b; +unsigned n; + +// The general include for eo +#include +#include +// Fitness function +#include +// Cuda Fitness function +#include +// QAP solution +#include +#include +//QAP neighbor +#include +//QAP neighborhood +#include +//QAP data +#include +// The Solution and neighbor comparator +#include +#include +// The Iter continuator +#include +// Local search algorithm +#include +//Algorithm and its components +#include +//Tabu list +#include +//Memories +#include +#include +#include +//To compute execution time +#include + +typedef eoInt solution; +typedef moIndexedSwapNeighbor Neighbor; +typedef moOrderNeighborhood Neighborhood; + +int main(int argc, char **argv) +{ + + /* ========================================================= + * + * Parameters + * + * ========================================================= */ + + // First define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameter, read it through the parser, + // and assign the value to the variable + + // seed + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // Iteration number + eoValueParam nbIterationParam(100, "nbIteration", "TS Iteration number", 'I'); + parser.processParam( nbIterationParam, "TS Iteration number" ); + unsigned nbIteration = nbIterationParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; // default value + eoValueParam statusParam(str_status.c_str(), "status", "Status file"); + parser.processParam( statusParam, "Persistence" ); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) { + parser.printHelp(cout); + exit(1); + } + if (statusParam.value() != "") { + ofstream os(statusParam.value().c_str()); + os << parser;// and you can use that file as parameter file + } + + /* ========================================================= + * + * Random seed + * + * ========================================================= */ + + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(seed); + + /* ========================================================= + * + * Initilisation of QAP data + * + * ========================================================= */ + + load(argv[1]); + + /* ========================================================= + * + * Initilisation of the solution + * + * ========================================================= */ + + eoInitPermutation init(n); + solution sol; + init(sol); + + /*========================================================= + * + * Evaluation of a solution neighbor's + * + * ========================================================= */ + + QapEval eval; + QapIncrEval incr_eval; + + /* ========================================================= + * + * Comparator of solutions and neighbors + * + * ========================================================= */ + + moNeighborComparator comparator; + moSolNeighborComparator solComparator; + + /* ========================================================= + * + * a solution neighborhood + * + * ========================================================= */ + + Neighborhood neighborhood(n*(n-1)/2); + + /* ========================================================= + * + * continuator + * + * ========================================================= */ + + moIterContinuator continuator(nbIteration); + + /*========================================================= + * + * Tabu list + * + * ========================================================= */ + + unsigned sizeTabuList=(n*(n-1))/2; + unsigned duration=sizeTabuList/8; + // tabu list + moIndexedVectorTabuList tl(sizeTabuList,duration); + + /* ========================================================= + * + * Memories + * + * ========================================================= */ + + moDummyIntensification inten; + moDummyDiversification div; + moBestImprAspiration asp; + + /* ========================================================= + * + * the Tabu search algorithm + * + * ========================================================= */ + + //General Constructor + moTS tabuSearch(neighborhood, eval, incr_eval, comparator, solComparator, continuator, tl, inten, div, asp); + + /* ========================================================= + * + * Execute the Tabu search from random sollution + * + * ========================================================= */ + + //Can be eval here, else it will be done at the beginning of the localSearch + eval(sol); + + std::cout << "initial: " << sol<< std::endl; + // Create timer for timing CUDA calculation + moGPUTimer timer; + timer.start(); + tabuSearch(sol); + timer.stop(); + std::cout << "final: " << sol << std::endl; + printf("Execution time = %.2lf s\n",timer.getTime()); + + delete[] a; + delete[] b; + + +return 0; +} diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/Problem.h b/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/Problem.h new file mode 100644 index 000000000..86f645e0c --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/Problem.h @@ -0,0 +1,62 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Karima Boufaras, Thé Van LUONG + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr + */ + +#ifndef __Problem +#define __Problem +/** + * Function to load a QAP data from a specific file + * @param _fileName the name of file containing specific QAP data + */ + +void load(char * _fileName) { + + fstream file(_fileName, ios::in); + if (!file) { + + string str = "QAPData: Could not open file [" + (string)_fileName + "]."; + throw runtime_error(str); + } + unsigned i, j; + file >> n; + a = new unsigned[n * n]; + b = new unsigned[n * n]; + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + file >> a[i * n + j]; + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + file >> b[i * n + j]; +} + +#endif diff --git a/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/QapEval.h b/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/QapEval.h new file mode 100644 index 000000000..223bdcbf1 --- /dev/null +++ b/trunk/paradiseo-gpu/tutorial/QAP_CPU/src/QapEval.h @@ -0,0 +1,75 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Karima Boufaras, Thé Van LUONG + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef __QapEval +#define __QapEval + +/** + * Full Evaluation of QAP + */ + +template +class QapEval : public eoEvalFunc +{ + + public: + + /** + * Constructor + */ + + QapEval(){} + + /** + * Destructor + */ + + ~QapEval(){} + + /** + * Functor for full evaluation of the solution + * @param _sol the solution to evaluate + */ + + void operator() (EOT & _sol) { + int cost=0; + for (int i=0; i + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Karima Boufaras, Thé Van LUONG + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef __QapIncrEval +#define __QapIncrEval + +#include + +/** + * Incremental Evaluation of QAP + */ + +template +class QapIncrEval : public moEval{ + + public: + + typedef typename moEval::EOT EOT; + typedef typename moEval::Fitness Fitness; + + /** + * Constructor + */ + + QapIncrEval(){} + + /** + * Destructor + */ + + ~QapIncrEval(){} + + /** + * Functor for incremental evaluation of the solution + * @param _sol the solution + * @param _neighbor the neighbor of solution to evaluate + */ + + void operator() (EOT & _sol, Neighbor & _neighbor){ + + unsigned int cost=0; + unsigned i,j; + _neighbor.getIndices(n,i,j); + cost = _sol.fitness() +compute_delta(_sol,i,j); + _neighbor.fitness(cost); + } + + /** + * Specific to the QAP incremental evaluation (part of algorithmic) + * @param _sol the solution to evaluate + * @param i the first position of swap + * @param j the second position of swap + */ + + unsigned int compute_delta(EOT & _sol,unsigned i,unsigned j) + { + int d; + int k; + + d = (a[i*n+i]-a[j*n+j])*(b[_sol[j]*n+_sol[j]]-b[_sol[i]*n+_sol[i]]) + + (a[i*n+j]-a[j*n+i])*(b[_sol[j]*n+_sol[i]]-b[_sol[i]*n+_sol[j]]); + for (k = 0; k < n; k = k + 1) + if (k!=i && k!=j) + d = d + (a[k*n+i]-a[k*n+j])*(b[_sol[k]*n+_sol[j]]-b[_sol[k]*n+_sol[i]]) + + (a[i*n+k]-a[j*n+k])*(b[_sol[j]*n+_sol[k]]-b[_sol[i]*n+_sol[k]]); + return(d); + } +}; +#endif