Commit 069c0d6a authored by maitre's avatar maitre

CUDA evaluation, copy to/from gpu

parent 1cde3ed4
......@@ -29,7 +29,8 @@ Centre de Math
#define YYTEXT_SIZE 10000
class CSymbol;
extern CSymbol *pCURRENT_CLASS, *pCURRENT_TYPE, *pGENOME;
extern CSymbol *pCURRENT_CLASS, *pCURRENT_TYPE, *pGENOME, *pCLASSES[128];
extern int nClasses_nb;
extern FILE *fpOutputFile, *fpTemplateFile, *fpGenomeFile, *fpExplodedGenomeFile;
extern char sRAW_PROJECT_NAME[], sPROJECT_NAME[], sEO_DIR[], sEZ_PATH[], sEZ_FILE_NAME[];
......
......@@ -14,6 +14,9 @@ Centre de Math
#include <unistd.h>
#include "debug.h"
size_t genomeSize;
bool genomeSizeValidity=false;
// local functions
char* selectorDetermination(int nMINIMISE, char* sSELECTOR){
......@@ -161,8 +164,23 @@ exponent ([Ee][+-]?[0-9]+)
CListItem<CSymbol*> *pSym;
pGENOME->pSymbolList->reset();
while (pSym=pGENOME->pSymbolList->walkToNextItem())
if ((pSym->Object->pType->ObjectType==oUserClass)&&(!pSym->Object->pType->bAlreadyPrinted))
if ((pSym->Object->pType->ObjectType==oUserClass)&&(!pSym->Object->pType->bAlreadyPrinted)){
DEBUG_PRT("%p",pSym->Object->pType);
pSym->Object->pType->printClasses(fpOutputFile);
}
if( TARGET == CUDA ){
DEBUG_PRT("User classes are :");
for( int i = nClasses_nb-1 ; i>=0 ; i-- ){
DEBUG_PRT(" %s, %p ,%d| ",pCLASSES[i]->sName,pCLASSES[i],pCLASSES[i]->bAlreadyPrinted);
if( !pCLASSES[i]->bAlreadyPrinted ){
fprintf(fpOutputFile,"// User class not refereced by the Genome");
pCLASSES[i]->printClasses(fpOutputFile);
}
}
DEBUG_PRT("\n");
}
}
<TEMPLATE_ANALYSIS>"\\GENOME_CTOR" {
CListItem<CSymbol*> *pSym;
......@@ -180,24 +198,23 @@ exponent ([Ee][+-]?[0-9]+)
}
<TEMPLATE_ANALYSIS>"\\GENOME_SIZE" {
CListItem<CSymbol*> *pSym;
if (bVERBOSE) printf ("Inserting default genome size calculator.\n");
pGENOME->pSymbolList->reset();
size_t size_of_genome=0;
while (pSym=pGENOME->pSymbolList->walkToNextItem()){
/* if (pSym->Object->ObjectQualifier==1) continue; // 1=Static */
/* if ((pSym->Object->ObjectType==oArray)&&(TARGET==DREAM)) */
/* fprintf(fpOutputFile," %s = new %s[%d];\n",pSym->Object->sName,pSym->Object->pType->sName,pSym->Object->nSize/pSym->Object->pType->nSize); */
/* if (pSym->Object->ObjectType==oPointer){ */
/* if (TARGET==DREAM) fprintf(fpOutputFile," %s=null;\n",pSym->Object->sName); */
/* else fprintf(fpOutputFile," %s=NULL;\n",pSym->Object->sName); */
/* } */
DEBUG_PRT("%s has size : %lu",pSym->Object->sName,pSym->Object->nSize);
size_of_genome+=pSym->Object->nSize;
if (bVERBOSE) printf ("Inserting default genome size.\n");
if( !genomeSizeValidity ){
if (bVERBOSE) printf ("\tComputing default genome size.\n");
CListItem<CSymbol*> *pSym;
pGENOME->pSymbolList->reset();
while (pSym=pGENOME->pSymbolList->walkToNextItem()){
DEBUG_PRT("%s has size : %lu",pSym->Object->sName,pSym->Object->nSize);
size_of_genome+=pSym->Object->nSize;
}
DEBUG_PRT("Total genome size is %lu",size_of_genome);
genomeSizeValidity=true;
}
else{
size_of_genome = genomeSize;
}
fprintf(fpOutputFile,"%d",size_of_genome);
DEBUG_PRT("Total genome size is %lu",size_of_genome);
}
<TEMPLATE_ANALYSIS>"\\COPY_CUDA_BUFFER" {
......@@ -1088,6 +1105,13 @@ exponent ([Ee][+-]?[0-9]+)
<COPY>"MINIMIZE"/[ \t\n]*"==" {if (bDoubleQuotes) fprintf(fpOutputFile,"MINIMIZE");
else fprintf(fpOutputFile,"EZ_MINIMIZE");} // local genome name
<COPY>"__device__" |
<COPY>"__host__" {
if( TARGET==CUDA ){
fprintf(fpOutputFile,"%s",yytext);
}
}
<COPY>"currentGeneration"[ \t\n]*"=" {fprintf(stderr,"\n%s - Error line %d: The current generation number cannot be changed (not an l-value).\n",sEZ_FILE_NAME,yylineno); exit(1);}
<COPY>"NB_GEN"[ \t\n]*"=" {fprintf(stderr,"\n%s - Error line %d: The number of generations can only be changed within the generation function.\n",sEZ_FILE_NAME,yylineno); exit (1);}
<COPY>"POP_SIZE"[ \t\n]*"=" {fprintf(stderr,"\n%s - Error line %d: The size of the population can only be changed within the generation function.\n",sEZ_FILE_NAME,yylineno); exit (1);}
......@@ -1224,6 +1248,8 @@ exponent ([Ee][+-]?[0-9]+)
//****************************************
<COPY_DISPLAY>"\\GenomeClass::display"[ \t\n]*":" {
/* DEBUG_PRT("Display function is at %d line in %s.ez",yylineno,sRAW_PROJECT_NAME); */
/* fprintf(fpOutputFile,"\n#line %d \"%s.ez\"\n",yylineno,sRAW_PROJECT_NAME); */
bDisplayFunction=bWithinDisplayFunction=1;
BEGIN COPY_USER_FUNCTION;
}
......@@ -1595,7 +1621,9 @@ exponent ([Ee][+-]?[0-9]+)
yyin = fpTemplateFile;
BEGIN TEMPLATE_ANALYSIS;
bWithinInitialiser=bWithinXover=bWithinMutator=bWithinEvaluator=0;
if (bWithinDisplayFunction) bWithinDisplayFunction=0; // display function
if (bWithinDisplayFunction){
bWithinDisplayFunction=0; // display function
}
else return END_OF_FUNCTION;} // Back to the template file
<COPY_USER_FUNCTION>.|\n {putc(yytext[0],fpOutputFile);}
......
......@@ -16,7 +16,9 @@ Centre de Math
CSymbol *pCURRENT_CLASS;
CSymbol *pCURRENT_TYPE;
CSymbol *pGENOME;
CSymbol* pCLASSES[128];
char sRAW_PROJECT_NAME[1000];
int nClasses_nb = 0;
char sPROJECT_NAME[1000];
char sLOWER_CASE_PROJECT_NAME[1000];
char sEZ_FILE_NAME[1000];
......@@ -214,15 +216,15 @@ GenomeAnalysis
ClassDeclarationsSection
: CLASSES {
if (bVERBOSE) printf("Declaration of user classes :\n\n");}
if (bVERBOSE) printf("Declaration of user classes :\n\n");}
ClassDeclarations
| CLASSES {
if (bVERBOSE) printf("No user class declaration found other than GenomeClass.\n");}
;
ClassDeclarations
: ClassDeclaration
| ClassDeclarations ClassDeclaration
: ClassDeclaration
| ClassDeclarations ClassDeclaration
;
ClassDeclaration
......@@ -230,9 +232,12 @@ ClassDeclaration
pCURRENT_CLASS=SymbolTable.insert($1);
pCURRENT_CLASS->pSymbolList=new CLList<CSymbol *>();
$1->ObjectType=oUserClass;
DEBUG_PRT("Yacc Symbol declaration %s %d",$1->sName,$1->nSize);
pCLASSES[nClasses_nb++] = $1;
}
'{' VariablesDeclarations '}' {
if (bVERBOSE) printf("Class %s declared for %d bytes.\n\n",$1->sName,$1->nSize);
DEBUG_PRT("Yacc variable declaration %s %d",$1->sName,$1->nSize);
}
;
......@@ -378,6 +383,7 @@ BaseConstructorParameter
GenomeDeclarationSection
: GENOME {
DEBUG_PRT("Yacc genome decl %s",$1.pSymbol->sName);
if (bVERBOSE) printf ("\nGenome declaration analysis :\n\n");
pGENOME=new CSymbol("Genome");
pCURRENT_CLASS=SymbolTable.insert(pGENOME);
......@@ -386,7 +392,7 @@ GenomeDeclarationSection
pGENOME->ObjectQualifier=0;
pGENOME->sString=NULL;
}
'{' VariablesDeclarations '}' {}
'{' VariablesDeclarations '}' {}
;
//GenomeMethodsDeclaration
......
......@@ -92,6 +92,54 @@ void CSymbol::print(FILE *fp){
fprintf(fp," %s %s[%d];\n",pSym->Object->pType->sName,pSym->Object->sName,pSym->Object->nSize/pSym->Object->pType->nSize);
}
if( TARGET==CUDA ){ // here we we are generating function to copy objects from host memory to gpu's.
bool isFlatClass = true;
pSymbolList->reset();
while (pSym=pSymbolList->walkToNextItem()){
DEBUG_PRT("analyse flat %s",pSym->Object->pType->sName);
if( (pSym->Object->ObjectType == oPointer) ){ //|| (pSym->Object->pType->ObjectType == oObject) ){
isFlatClass = false;
break;
}
}
DEBUG_PRT("Does %s flat class : %s",sName,(isFlatClass?"yes":"no"));
pSymbolList->reset();
fprintf(fp," %s* cudaSendToGpu%s(){\n",sName,sName);
fprintf(fp," %s* ret=NULL;\n",sName);
if( isFlatClass ){
fprintf(fp," cudaMalloc((void**)&ret,sizeof(%s));\n",sName);
fprintf(fp," cudaMemcpy(ret,this,sizeof(%s),cudaMemcpyHostToDevice);\n",sName);
fprintf(fp," return ret;\n");
}
else{
fprintf(fp," %s tmp;\n",sName);
fprintf(fp," memcpy(&tmp,this,sizeof(%s));\n",sName);
while (pSym=pSymbolList->walkToNextItem()){
if( (pSym->Object->ObjectType == oPointer) ){ //|| (pSym->Object->pType->ObjectType == oObject) ){
fprintf(fp," tmp.%s=this->%s->cudaSendToGpu%s();\n",pSym->Object->sName,pSym->Object->sName,pSym->Object->pType->sName);
}
}
fprintf(fp," cudaMalloc((void**)&ret,sizeof(%s));\n",sName);
fprintf(fp," cudaMemcpy(ret,&tmp,sizeof(%s),cudaMemcpyHostToDevice);\n",sName);
fprintf(fp," return ret;\n");
}
fprintf(fp," }\n\n");
fprintf(fp," void cudaGetFromGpu%s(%s* dev_ptr){\n",sName,sName);
fprintf(fp," %s* ret=NULL;\n",sName);
if( isFlatClass ){
fprintf(fp," ret = (%s*)malloc(sizeof(%s));\n",sName,sName);
fprintf(fp," cudaMemcpy(ret,dev_ptr,sizeof(%s),cudaMemcpyDeviceToHost);\n",sName);
while (pSym=pSymbolList->walkToNextItem())
fprintf(fp," this->%s=ret->%s;\n",pSym->Object->sName,pSym->Object->sName);
fprintf(fp," }\n\n");
}
}
fprintf(fp," %s(){ // Constructor\n",sName); // constructor
pSymbolList->reset(); // in which we initialise all pointers to NULL
while (pSym=pSymbolList->walkToNextItem())
......@@ -342,7 +390,7 @@ void CSymbol::printUserClasses(FILE *fp){
if (bAlreadyPrinted) return;
bAlreadyPrinted=true;
while (pSym=pSymbolList->walkToNextItem()){
if (pSym->Object->pType->ObjectType==oUserClass)
if ((pSym->Object->pType->ObjectType==oUserClass))
pSym->Object->pType->printUC(fp);
}
}
......@@ -377,7 +425,7 @@ void CSymbol::printAllSymbols(FILE *fp, char *sCompleteName, EObjectType FatherT
strcat(sNewCompleteName,"[");
sprintf(s,"%d",pSym->Object->nSize/pSym->Object->pType->nSize);
strcat(sNewCompleteName,s);
strcat(sNewCompleteName,"]");
strcat(sNewCompleteName,"]");
}
fprintf(fp,"%s\n",sNewCompleteName);
strcpy(sNewCompleteName, sCompleteName);
......
......@@ -31,6 +31,11 @@ float Rosenbrock(float *, int);
float Schwefel(float *, int);
float Weierstrass(float *, int);
EvalCounter* d_counter;
struct gpuOptions{
EvalCounter* counter;
};
\end
\User functions:
......@@ -165,38 +170,35 @@ float gauss()
}
\end
\Initialisation function:
/* if(argc>1 ){ */
/* if((!(strcmp(argv[1],"Sphere")))) Fitness = Sphere; */
/* if((!(strcmp(argv[1],"AckleyPath")))) Fitness = AckleyPath; */
/* if((!(strcmp(argv[1],"Easom")))) Fitness = Easom; */
/* if((!(strcmp(argv[1],"Griewangk")))) Fitness = Griewangk; */
/* if((!(strcmp(argv[1],"Rastrigin")))) Fitness = Rastrigin; */
/* if((!(strcmp(argv[1],"Rosenbrock")))) Fitness = Rosenbrock; */
/* if((!(strcmp(argv[1],"Schwefel")))) Fitness = Schwefel; */
/* if((!(strcmp(argv[1],"Weierstrass")))) Fitness = Weierstrass; */
/* if(argc>2) n = atoi(argv[2]); */
/* } */
/* else */
/* Fitness = Weierstrass; */
/* n=MIN(n, SIZE); */
std::cout<<"************* n: "<<n<<std::endl;
// pour l'impression dans le fichier de resultats
/* float MinTheo = 0.; */
// printf("%s_T_ad n= %s MinTheo= %f ",argv[1],argv[2],MinTheo);
EvalCounter counter;
counter.a = 0;
d_counter = counter.cudaSendToGpuEvalCounter();
initOpts.counter=d_counter;
\end
\Finalization function:
cout << "finalization function called" << endl;
EvalCounter counter;
counter.cudaGetFromGpuEvalCounter(d_counter);
cout << counter << endl;
\end
\User classes :
EvalCounter {
int a;
}
TestClass1 {
int a;
float b;
}
GenomeClass {
float x[SIZE];
float sigma[SIZE]; // auto-adaptative mutation parameter
}
float x[SIZE];
float sigma[SIZE]; // auto-adaptative mutation parameter
}
\end
\GenomeClass::initialiser : // "initializer" is also accepted
......@@ -237,6 +239,7 @@ return NbMut;
float Point[SIZE];
for (int i=0; i<N_LIM; i++) Point[i] = Genome.x[i];
Score= Weierstrass(Point, N_LIM);
initOpts.counter->a+=2;
return Score;
\end
......
This diff is collapsed.
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