/************************************************************************ C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example : ScalarVectGlobalMemDP.c Objective : Perform scalar-vector multiplication using global memory (double precision) Input : None Output : Execution time in seconds , Gflops achieved Created : August-2013 E-mail : hpcfte@cdac.in **************************************************************************/ #include #include #include #include #include #include #define SIZE 128 // Modify SIZE to execute for different data sizes #define EPS 1.0e-15 // threshhold aprrox epsilion value #define OPENCL_CHECK_STATUS(NAME,ERR) {\ const char *name=NAME;\ cl_int Terr=ERR;\ if (Terr != CL_SUCCESS) {\ printf("\n\t\t Error: %s (%d) \n",name, Terr);\ exit(-1);} }\ const char * kernelSourcePathScalarVectDp = "ScalarVectGlobalMemDP_kernel.cl"; /* get platform function*/ int getPlatform(cl_platform_id *selectedPlatform,cl_uint *numDevices) { cl_int err; int count; char pbuff[100]; cl_uint numPlatforms; cl_platform_id *platforms; *selectedPlatform = NULL; /* Get the number of OpenCL Platforms Available */ err = clGetPlatformIDs ( 0, 0, &numPlatforms); if( err != CL_SUCCESS || numPlatforms == 0) { printf(" \n\t\t No Platform Found \n"); return 1; } else { if( numPlatforms == 0) { return 1; } else { /* Allocate the space for available platform*/ assert( (platforms = (cl_platform_id *)malloc( sizeof(cl_platform_id) * (numPlatforms))) != NULL); /* Get available OpenCL Platforms IDs*/ err = clGetPlatformIDs( numPlatforms,platforms, NULL); OPENCL_CHECK_STATUS(" Failed to get Platform IDs",err); for ( count = 0 ; count < numPlatforms ; count++) { /* get platform info*/ err=clGetPlatformInfo(platforms[count],CL_PLATFORM_NAME,sizeof(pbuff),pbuff,NULL); OPENCL_CHECK_STATUS("clGetPlatformInfo Failed",err); /* get device id and info*/ err = clGetDeviceIDs( platforms[count],CL_DEVICE_TYPE_GPU,0,0,numDevices); if( err != CL_SUCCESS || *numDevices ==0) { continue; } else { /* get selected platform*/ *selectedPlatform =platforms[count]; printf("\n\t---------------------------Device details-------------------------------------\n\n"); printf("\tPlatform used : %s\n",pbuff); break; } } } } if ( count == numPlatforms ) { printf(" \n\t No platform found \n"); return 1; } free(platforms); return 0; } /* read program source file*/ char* readKernelSource(const char* kernelSourcePath) { FILE *fp = NULL; size_t sourceLength; char *sourceString ; fp = fopen( kernelSourcePath , "r"); if(fp == 0) { printf("failed to open file"); return NULL; } // get the length of the source code fseek(fp, 0, SEEK_END); sourceLength = ftell(fp); rewind(fp); // allocate a buffer for the source code string and read it in sourceString = (char *)malloc( sourceLength + 1); if( fread( sourceString, 1, sourceLength, fp) !=sourceLength ) { printf("\n\t Error : Fail to read file "); return 0; } sourceString[sourceLength]='\0'; fclose(fp); return sourceString; }// end of readKernelSource // Fill in the vector with double precision values void fill_dp_vector(double* vec,int size) { int ind; for(ind=0;ind fabs(output[i])) relativeError = ((temp_Out[i] - output[i]) / temp_Out[i]); else relativeError = fabs((output[i] - temp_Out[i]) / output[i]); if (relativeError > eps && relativeError != 0.0e+00 ) { if(errorNorm < relativeError) { errorNorm = relativeError; flag=1; } } } if( flag == 1) { printf(" \n \t\t Results verfication : Failed\n"); printf(" \n \t\t Considered machine precision : %e\n", eps); printf(" \n \t\t Relative Error : %e\n", errorNorm); } else { printf("\n \n\t\tResults verfication : Success\n\n"); } } //void Execute_scalarVectGMDP(int SIZE,FILE *fp) int main(int argc,char *argv[]) { cl_platform_id selectedPlatform; //holds list of platforms cl_uint numPlatforms; //holds number of platforms cl_int err; //holds error (return value) cl_uint numDevices; /*hold the number of devices */ cl_device_id *devices; /* hold list of devices */ int count; cl_context context; //holds context object cl_program program; //holds program object double *h_VectA; //holds host input buffer double *h_Output; //holds host output buffer int i ,h_Scalar; int vectSize=SIZE; h_Scalar=4; //can be changed for different scalar value /* allocate host memory*/ assert((h_VectA=(double *)malloc(vectSize*sizeof(double)))!=NULL); assert((h_Output=(double *)malloc(vectSize*sizeof(double)))!=NULL); /*initialize host memory*/ fill_dp_vector(h_VectA,vectSize); for(i=0;i