Commit eb43e1e1 authored by moh_lo's avatar moh_lo

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
......
This diff is collapsed.
This diff is collapsed.
\TEMPLATE_START//********************************************
//
// EASEA.cpp
//
// C++ file generated by AESAE-EO Millennium Edition (v0.6)
//
//********************************************
\ANALYSE_PARAMETERS
\INSERT_USER_DECLARATIONS
\INSERT_INITIALISATION_FUNCTION \ANALYSE_USER_CLASSES
\INSERT_USER_CLASSES
\GENOME_CTOR
\GENOME_CTOR
\GENOME_DTOR
\INSERT_GENOME
\GENOME_DTOR
\COPY_CTOR
\READ
\WRITE
\INSERT_USER_FUNCTIONS
\INSERT_INITIALISER
\INSERT_CROSSOVER
\INSERT_MUTATOR
\INSERT_EVALUATOR
\INSERT_INIT_FCT_CALL
\SELECTORS
\REPL_PERC
\REPLACEMENT
\ELITIST_REPLACE_NAME
\DISCARD_PRM
\ELITISM
\TEMPLATE_END
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment