Commit eb43e1e1 authored by moh_lo's avatar moh_lo
Browse files

GPU version ok, at least with onemax (cuda_onemax)

parent 68361aa3
......@@ -33,7 +33,7 @@ protected:
CSymbolTable *pSymbolTable; // the symbol table
bool bSymbolInserted,bWithinEvaluator; // used to change evalutor type from double to float
bool bInitFunction,bDisplayFunction,bFunction, bNotFinishedYet, bWithinEO_Function, bWithinGPU_Function;
bool bDoubleQuotes,bWithinDisplayFunction,bWithinInitialiser,bWithinMutator,bWithinXover;
bool bDoubleQuotes,bWithinDisplayFunction,bWithinInitialiser,bWithinMutator,bWithinXover,bWithinGPUEval;
bool bWaitingForSemiColon,bFinishNB_GEN,bFinishMINIMISE,bFinishMINIMIZE,bGenerationFunction;
bool bCatchNextSemiColon,bWaitingToClosePopulation;
CSymbol *pASymbol;
......@@ -47,7 +47,7 @@ public:
// constructor
{
bFunction=bWithinEvaluator=bDisplayFunction=bInitFunction=bNotFinishedYet=0;
bSymbolInserted=bDoubleQuotes=bWithinDisplayFunction=bWithinInitialiser=bWithinMutator=bWithinXover=0;
bSymbolInserted=bDoubleQuotes=bWithinDisplayFunction=bWithinInitialiser=bWithinMutator=bWithinXover=bWithinGPUEval=0;
bWaitingForSemiColon=bFinishNB_GEN=bFinishMINIMISE=bFinishMINIMIZE=bGenerationFunction=0;
bCatchNextSemiColon,bWaitingToClosePopulation=0;
}
......@@ -213,12 +213,12 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
}
else if(TARGET==GPU){
if (pSym->Object->ObjectType==oObject)
fprintf(fpOutputFile," genome.%s=genome.%s;\n",pSym->Object->sName,pSym->Object->sName);
fprintf(fpOutputFile," %s=genome.%s;\n",pSym->Object->sName,pSym->Object->sName);
if (pSym->Object->ObjectType==oPointer)
fprintf(fpOutputFile," genome.%s=(genome.%s ? new %s(*(genome.%s)) : NULL);\n",pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
fprintf(fpOutputFile," %s=(genome.%s ? new %s(*(genome.%s)) : NULL);\n",pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->sName);
if (pSym->Object->ObjectType==oArray){
fprintf(fpOutputFile," {for(int EASEA_Ndx=0; EASEA_Ndx<%d; EASEA_Ndx++)\n",pSym->Object->nSize/pSym->Object->pType->nSize);
fprintf(fpOutputFile," genome.%s[EASEA_Ndx]=genome.%s[EASEA_Ndx];}\n",pSym->Object->sName,pSym->Object->sName);
fprintf(fpOutputFile," %s[EASEA_Ndx]=genome.%s[EASEA_Ndx];}\n",pSym->Object->sName,pSym->Object->sName);
}
}
else
......@@ -406,7 +406,7 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
<TEMPLATE_ANALYSIS>"\\INSERT_EVALUATOR" {
yyreset();
yyin = fpGenomeFile; // switch to .ez file and analyser
BEGIN COPY_EVALUATOR;
BEGIN COPY_EVALUATOR;
}
<TEMPLATE_ANALYSIS>"\\ANALYSE_PARAMETERS" {
yyreset();
......@@ -467,6 +467,56 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
<TEMPLATE_ANALYSIS>"\\MIG_FREQ" {fprintf(fpOutputFile,"%f",fMIG_FREQ);}
<TEMPLATE_ANALYSIS>"\\MIG_TARGET_SELECTOR" {fprintf(fpOutputFile,"%s",sMIG_TARGET_SELECTOR);}
<TEMPLATE_ANALYSIS>"\\START_GPU_INDIVIDUAL_H_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
strcpy(sFileName, sRAW_PROJECT_NAME);
strcat(sFileName,"Individual.h");
fpOutputFile=fopen(sFileName,"w");
if (bVERBOSE) printf("Creating %s...\n",sFileName);
}
<TEMPLATE_ANALYSIS>"\\START_GPU_INDIVIDUAL_CPP_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
strcpy(sFileName, sRAW_PROJECT_NAME);
strcat(sFileName,"Individual.cpp");
fpOutputFile=fopen(sFileName,"w");
if (bVERBOSE) printf("Creating %s...\n",sFileName);
}
<TEMPLATE_ANALYSIS>"\\START_GPU_EVAL_CU_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
strcpy(sFileName, sRAW_PROJECT_NAME);
strcat(sFileName,"GPUEval.cu");
fpOutputFile=fopen(sFileName,"w");
if (bVERBOSE) printf("Creating %s...\n",sFileName);
}
<TEMPLATE_ANALYSIS>"\\START_GPU_EVAL_H_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
strcpy(sFileName, sRAW_PROJECT_NAME);
strcat(sFileName,"GPUEval.h");
fpOutputFile=fopen(sFileName,"w");
bWithinGPUEval = true;
if (bVERBOSE) printf("Creating %s...\n",sFileName);
}
<TEMPLATE_ANALYSIS>"\\START_USER_FUN_H_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
strcpy(sFileName, sRAW_PROJECT_NAME);
strcat(sFileName,"UserFunc.h");
fpOutputFile=fopen(sFileName,"w");
if (bVERBOSE) printf("Creating %s...\n",sFileName);
}
<TEMPLATE_ANALYSIS>"\\START_EO_GENOME_H_TPL" {
char sFileName[1000];
fclose(fpOutputFile);
......@@ -619,7 +669,7 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
} }
printf ("Have a nice compile time.\n");
if (TARGET==EO) fprintf(fpOutputFile,"\n# That's all folks ! \n");
else fprintf(fpOutputFile,"\n// That's all folks ! \n");
else if( TARGET == GPU ) fprintf(stdout,"\n// That's all folks ! \n");
fflush(fpOutputFile);
fclose(fpOutputFile);
fclose(fpTemplateFile);
......@@ -1073,7 +1123,7 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
if (bWithinDisplayFunction && TARGET!=GPU) fprintf(fpOutputFile,"(*this)");
else if ((TARGET==EO)&&(bWithinInitialiser)) fprintf(fpOutputFile, "(*genome)");
else if ((TARGET==EO)&&(bWithinMutator)) fprintf(fpOutputFile, "_genotype");
//else if (TARGET==GPU) fprintf(fpOutputFile,"(*this)");
else if (TARGET==GPU && bWithinGPUEval) fprintf(fpOutputFile,"(*genome)");
else fprintf(fpOutputFile,"genome");
} // local genome name
<COPY_USER_FUNCTION>"\"" {(bDoubleQuotes ? bDoubleQuotes=0:bDoubleQuotes=1); fprintf(fpOutputFile,"\"");}
......@@ -1151,7 +1201,14 @@ YYSTYPE& yylval = *(YYSTYPE*)yyparserptr->yylvalptr;
if (TARGET==GALIB) fprintf(fpOutputFile,"EZ_EVAL+=(double)(clock()-EZ_t1);\n return (float)");
if (TARGET==EO) {fprintf(fpOutputFile,"genome.fitness(");bCatchNextSemiColon=true;}// changes function type// changes function type
if (TARGET==DREAM) {fprintf(fpOutputFile,"infoHabitant.setFitness(new Double(");bCatchNextSemiColon=true;}// changes function type
if (TARGET==GPU) { fprintf(fpOutputFile,"return nFitness = ");}
if (TARGET==GPU ) {
if( bWithinGPUEval ){
fprintf(fpOutputFile,"return ");
bWithinGPUEval = false;
}
else
fprintf(fpOutputFile,"return nFitness = ");
}
bWithinEvaluator=0;
}
else if ((bWithinMutator)&&(TARGET!=GALIB)) {
......
......@@ -11,9 +11,9 @@ _________________________________________________________*/
\User declarations:
#define SIZE 16
float pMutPerGene=0.1;
static float pMutPerGene=0.1;
inline void swap(bool& a, bool& b)
static inline void swap(bool& a, bool& b)
{bool c=a; a=b; b=c;}
\end
......@@ -59,9 +59,14 @@ for(int i=0;i<CrossoverPosition+1;i++)
\end
\GenomeClass::evaluator:
int Score=0;
for (int i=0; i<SIZE;i++)
Score+=(int)Genome.x[i];
float Score=0;
for (int i=0; i<SIZE;i++)
Score+=(Genome.x[i]?1.:0.);
for( size_t i=0; i<SIZE ; i++ )
printf("%d ",(Genome.x[i]==1?1:0));
printf("\n");
return Score;
\end
......
#define FITNESS_TYPE float
#define BOOLEAN_EA char
#define true 1
#define false 0
#ifdef DEBUG_KRNL
#define CDC( lastError, errorMsg, fun ) \
fun ; \
cout <<__FILE__<< " : "<<__LINE__<<" "<<errorMsg<<" : "<<endl; \
cout << "\t" <<(cudaGetErrorString(lastError=cudaGetLastError())) \
<< endl; \
if( lastError != cudaSuccess )exit(-1)
#else
#define CDC( lastError, errorMsg, fun ) \
fun
#endif
#ifndef OUTPUT_EA_H
#define OUTPUT_EA_H
#define TMP_BUF_LENGTH 512
#define TIMING
#include "timing.h"
#include <libxml/tree.h>
#include "basetype.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
DECLARE_TIME(gpu);
DECLARE_TIME(cpu);
DECLARE_TIME(init);
DECLARE_TIME(krnl);
DECLARE_TIME(memCpy1);
DECLARE_TIME(memCpy2);
DECLARE_TIME(alloc);
BOOLEAN_EA cmp;
BOOLEAN_EA repartition;
size_t popSize;
size_t nbBlock,nbThreadPB,nbThreadLB,memSize,sharedMemSize;
size_t fakeIteration;
}OutputEa;
xmlNodePtr outputEaToXmlNode(OutputEa* tt);
xmlChar* timevalToXmlChar(struct timeval* ts);
void outputTss(const char* filename);
void addTs(OutputEa* ts);
void initTss(const char* args);
void attachSignal();
#ifdef __cplusplus
}
#endif
#endif
#include <time.h> //gettimeofday
#include <sys/time.h>
#include <stdio.h>
#ifndef TIMING_H
#define TIMING_H
#ifdef TIMING
#define DECLARE_TIME(t) \
struct timeval t##_beg, t##_end, t##_res
#define TIME_ST(t) \
gettimeofday(&t##_beg,NULL)
#define TIME_END(t) \
gettimeofday(&t##_end,NULL)
#define SHOW_TIME(t) \
timersub(&t##_end,&t##_beg,&t##_res); \
printf("%s : %d.%06d\n",#t,t##_res.tv_sec,t##_res.tv_usec)
#define SHOW_SIMPLE_TIME(t) \
printf("%s : %d.%06d\n",#t,t.tv_sec,t.tv_usec)
#define COMPUTE_TIME(t) \
timersub(&t##_end,&t##_beg,&t##_res)
#else
#define DECLARE_TIME(t)
#define TIME_ST(t)
#define TIME_END(t)
#define SHOW_TIME(t)
#define SHOW_SIMPLE_TIME(t)
#endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include "tool.h"
#include <math.h>
int tossCoin(double p){
if( rand() < p*RAND_MAX)
return 1;
else
return 0;
}
int getRandomIntMax(int max){
double r = rand();
r = r / RAND_MAX;
r = r * max;
return r;
}
int randomLoc(int min, int max){
return min+getRandomIntMax(max-min);
}
size_t
partieEntiereSup(float E){
int fl = floor(E);
if( fl == E )
return E;
else
return floor(E)+1;
}
#ifndef TOOL_H
#define TOOL_H
#ifdef __cpluplus
extern "C" {
#endif
int tossCoin(double p);
int getRandomIntMax(int max);
int randomLoc(int min, int max);
size_t partieEntiereSup(float E);
#ifdef __cpluplus
}
#endif
#endif
/*_________________________________________________________
This C:\WINDOWS\a_listmarc\listsort.ez file was automatically created by GUIDE v0.1
_________________________________________________________*/
\User declarations:
int SIZE=10;
double pMutPerGene=0.4;
inline void swap(int& a,int& b) {int c=a; a=b; b=c;}
\end
\User functions:
void cross(GenomeClass *pChild, GenomeClass *pParent, int locus){
Element *p, *pChildList, *pParentList;
pChildList=pChild->pList; pParentList=pParent->pList;
for (int i=0;i<SIZE;i++){
if (i>=locus){
for(p=pChild->pList;pParentList->Value!=p->Value;p=p->pNext);
swap(p->Value, pChildList->Value);
}
pChildList=pChildList->pNext;
pParentList=pParentList->pNext;
}
}
\end
\Initialisation function:
if ((argc>1)&&(!strcmp(argv[1],"size"))) SIZE=atoi(argv[2]);
\end
\User classes:
Element { int Value;
Element *pNext; }
GenomeClass { Element *pList;
int Size; }
\end
\GenomeClass::initialiser:
Element *pElt;
Genome.Size=0;
Genome.pList=NULL;
for (int i=0;i<SIZE;i++){ // creation of a linked list of SIZE elements
pElt=new Element; // with the decreasing values:
pElt->Value=i+1; // (SIZE, SIZE-1, ... , 3, 2, 1)
pElt->pNext=Genome.pList;
Genome.pList=pElt;
Genome.Size++;
}
\end
\GenomeClass::crossover:
int locus=random (0,SIZE-1);
cross(&child1, &parent2, locus);
cross(&child2, &parent1, locus);
\end
\GenomeClass::mutator:
int NbMut=0;
Element *p=Genome.pList;
while (p->pNext){
if (tossCoin(pMutPerGene)){ //We swap the current value with the next
swap(p->Value,p->pNext->Value);
NbMut++;
}
p=p->pNext;
}
return NbMut;
\end
\GenomeClass::evaluator:
int i=0,eval=0;
Element *p=Genome.pList;
while(p->pNext){
if (p->Value==++i) eval+=10;
if (p->Value<p->pNext->Value) eval+=4;
else eval-=2;
p=p->pNext;
}
if (p->Value==SIZE) eval+=10;
return (eval<0 ? 0 : eval);
\end
\GenomeClass::display:
os << "Size:" << Genome.Size << "\n";
os << "pList:" << *(Genome.pList) << "\n";
os << "This was MY display function !\n";
\end
\At each new generation:
if (NB_GEN-currentGeneration==10) pMutPerGene=0.1;
\end
\Default run parameters:
// Variation operators:
Operators are called: Sequentially
Mutation probability: 0.3
Crossover probability: 0.8
// Evolution Engine:
Evaluator goal: Maximise
Number of generations: 2000
Evolutionary engine: Custom
Population size: 100
Elite: 10
Fertility: 80
Genitors selector: Tournament 2
Selected genitors: 100
Offspring size: 100
Reduce parents: Tournament 2
Surviving parents: 20
Reduce offspring: Tournament 2
Surviving offspring: 80
Final reduce: Tournament 2
Elitism: Strong
// Island model:
Number of islands: 5
Emigration policy: Move
Migrants selector: Tournament 2
Migrants destination: Neighbours
Migration frequency: 0.2
Number of emigrants: 3
Immigration replacement: Tournament 2
Immigration policy: Add
\end
\User declarations:
#define SIZE 100
#define SIZE 16
float pMutPerGene=0.1;
inline void swap(bool& a, bool& b)
......@@ -43,9 +43,10 @@ GenomeClass
\end
\GenomeClass::evaluator:
int Score=0;
float Score=0;
for (int i=0; i<SIZE;i++)
Score+=(int)Genome.x[i];
return Score;
\end
......
......@@ -12,8 +12,8 @@ $(EXEC):EaseaSym.o EaseaLex.o EaseaParse.o alexyacc/libalex.so
%.o:%.cpp
$(CPPC) $(CPPFLAGS) -c -o $@ $<
EaseaParse.cpp:winreceive
EaseaLex.cpp:winreceive
# EaseaParse.cpp:winreceive
# EaseaLex.cpp:winreceive
#compile library for alex and ayacc unix version
alexyacc/libalex.so:alexyacc/*.cpp
......
\TEMPLATE_START// -*- mode: c++; c-indent-level: 2; c++-member-init-indent: 8; comment-column: 35; -*-
//
// (The above line is useful in Emacs-like editors)
//
//****************************************
//
// EASEA.cpp
//
// C++ file generated by AESAE-EO v0.7b
//
//****************************************
//
//
// Main file for creating a new representation in EO
// =================================================
//
// This main file includes all other files that have been generated by the
// script create.sh, so it is the only file to compile.
//
// In case you want to build up a separate library for your new Evolving Object,
// you'll need some work - follow what's done in the src/ga dir, used in the
// main file BitEA in tutorial/Lesson4 dir.
// Or you can wait until we do it :-)
// Miscellany includes and declarations
#include <iostream>
using namespace std;
// eo general include
#include "eo"
// real bounds (not yet in general eo include)
#include "utils/eoRealVectorBounds.h"
unsigned *pCurrentGeneration;
unsigned *pEZ_NB_GEN;
double EZ_MUT_PROB, EZ_XOVER_PROB, EZ_REPL_PERC=0.0;
int EZ_NB_GEN, EZ_POP_SIZE;
unsigned long EZ_NB_EVALUATIONS=0L;
inline int random(int b1=0, int b2=1){
return rng.random(b2-b1)+b1;
}
inline double random(double b1=0, double b2=1){
return rng.uniform(b2-b1)+b1;
}
inline float random(float b1=0, float b2=1){
return rng.uniform(b2-b1)+b1;
}
\ANALYSE_PARAMETERS
\INSERT_USER_DECLARATIONS
\INSERT_INITIALISATION_FUNCTION
// include here whatever specific files for your representation
// Basically, this should include at least the following
/** definition of representation:
* class EASEAGenome MUST derive from EO<FitT> for some fitness
*/
#include "EASEAGenome.h"
// GENOTYPE EASEAGenome ***MUST*** be templatized over the fitness
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// START fitness type: double or eoMaximizingFitness if you are maximizing
// eoMinimizingFitness if you are minimizing
typedef \MINIMAXI MyFitT ; // type of fitness
// END fitness type
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// Then define your EO objects using that fitness type
typedef EASEAGenome<MyFitT> Indi; // ***MUST*** derive from EO
\INSERT_USER_FUNCTIONS
/** definition of evaluation:
* class EASEAEvalFunc MUST derive from eoEvalFunc<EASEAGenome>
* and should test for validity before doing any computation
* see tutorial/Templates/evalFunc.tmpl
*/
#include "EASEAEvalFunc.h"
/** definition of initialization:
* class EASEAGenomeInit MUST derive from eoInit<EASEAGenome>
*/
#include "EASEAInit.h"
/** include all files defining variation operator classes
*/
#include "EASEAMutation.h"
#include "EASEAQuadCrossover.h"
// Use existing modules to define representation independent routines
// These are parser-based definitions of objects
// how to initialize the population
// it IS representation independent if an eoInit is given
#include <do/make_pop.h>
eoPop<Indi >& make_pop(eoParser& _parser, eoState& _state, eoInit<Indi> & _init){
return do_make_pop(_parser, _state, _init);
}
// the stopping criterion
#include "do/make_continue.h"
eoContinue<Indi>& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval){
return do_make_continue(_parser, _state, _eval);
}
// outputs (stats, population dumps, ...)
#include <do/make_checkpoint.h>
eoCheckPoint<Indi>& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>& _continue) {
return do_make_checkpoint(_parser, _state, _eval, _continue);
}
// evolution engine (selection and replacement)
#include <do/make_algo_easea.h>
eoAlgo<Indi>& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>& _continue, eoGenOp<Indi>& _op){
return do_make_algo_scalar(_parser, _state, _eval, _continue, _op);
}
// simple call to the algo. stays there for consistency reasons
// no template for that one
#include <do/make_run.h>
// the instanciating fitnesses
#include <eoScalarFitness.h>
void run_ea(eoAlgo<Indi>& _ga, eoPop<Indi>& _pop){
do_run(_ga, _pop);
}
// checks for help demand, and writes the status file
// and make_help; in libutils
void make_help(eoParser & _parser);
// now use all of the above, + representation dependent things
int main(int argc, char* argv[]){
try {
\INSERT_INIT_FCT_CALL
eoParser parser(argc, argv); // for user-parameter reading
eoState state; // keeps all things allocated
// The fitness
//////////////
EASEAEvalFunc<Indi> plainEval/* (varType _anyVariable) */;
// turn that object into an evaluation counter
eoEvalFuncCounter<Indi> eval(plainEval);
// a genotype initializer
EASEAInit<Indi> init;
// or, if you need some parameters, you might as well
// - write a constructor of the eoMyStructInit that uses a parser
// - call it from here:
// eoEASEAInit<Indi> init(parser);
// Build the variation operator (any seq/prop construct)
// here, a simple example with only 1 crossover (2->2, a QuadOp) and
// one mutation, is given.
// Hints to have choice among multiple crossovers and mutations are given
// A (first) crossover (possibly use the parser in its Ctor)
EASEAQuadCrossover<Indi> cross /* (eoParser parser) */;
// IF MORE THAN ONE:
// read its relative rate in the combination
// double cross1Rate = parser.createParam(1.0, "cross1Rate", "Relative rate for crossover 1", '1', "Variation Operators").value();