#include "NBody.hpp" /////////////////////////////////// NBody::setupCLProgram() Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Build CL kernel program executable. // /////////////////////////////////// NBody::setupCLProgram() Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ int NBody::setupCLProgram() { cl_int status; /* create a CL program using the kernel source */ char *program_source = load_program_source(kernelFileName); size_t sourceSize[] = { strlen(program_source) }; program = clCreateProgramWithSource( context, 1, (const char**)&program_source, sourceSize, &status); if (status != CL_SUCCESS){ std::cout << "clCreateProgramWithSource failed." << std::endl; return 1; } /* create a cl program executable for all the devices specified */ status = clBuildProgram( program, 1, &devices[0], NULL, NULL, NULL); if(status != CL_SUCCESS) { if(status == CL_BUILD_PROGRAM_FAILURE) { cl_int logStatus; char * buildLog = NULL; size_t buildLogSize = 0; logStatus = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, &buildLogSize); if (logStatus != CL_SUCCESS){ std::cout << "clGetProgramBuildInfo failed." << std::endl; return 1; } buildLog = (char*)malloc(buildLogSize); if(buildLog == NULL) { std::cout << "Failed to allocate host memory. (buildLog)" << std::endl; return 1; } memset(buildLog, 0, buildLogSize); logStatus = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, NULL); if (logStatus != CL_SUCCESS){ std::cout << "clGetProgramBuildInfo failed." << std::endl; free(buildLog); return 1; } std::cout << " \n\t\t\tBUILD LOG\n"; std::cout << " ************************************************\n"; std::cout << buildLog << std::endl; std::cout << " ************************************************\n"; free(buildLog); } if(status != CL_SUCCESS) { std::cout << "clBuildProgram failed." << std::endl; return 1; } } } /////////////////////////////////// NBody::load_program_source Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // Loads the kernel code form a .cl file type and returns the source code as a char* // /////////////////////////////////// NBody::load_program_source Func \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ char* NBody::load_program_source(const char *filename) { FILE *fh; char *source; int size; fh = fopen(filename, "r"); if (fh == NULL){ std::cout << "Reading the source cod for kernel failed." << std::endl; exit(1); } fseek (fh, 0, SEEK_END); size = ftell (fh); rewind(fh); source = (char *) malloc(size+1); int result = fread(source, 1, size, fh); /*if (result != size){ std::cout << "Reading the source cod for kernel failed." << std::endl; exit(1); }*/ source[result] = '\0'; fclose(fh); return source; }