bool TestOpenCL::initKernel() { boost::filesystem::path kernelDirectory=boost::filesystem::current_path(); kernelDirectory/="kernels"; std::string kernelPath=kernelDirectory.string()+"/roll.cl"; return loadKernelFile(m_openCLContext, m_openCLDevice, kernelPath, "rollHorizontal", m_kernel); }
extern cl_program clCreateProgramWithSource (cl_context context, cl_uint count, const char **strings, const size_t *lengths, cl_int *errcode_ret) { PRINT_DEBUG("\n====\tCreating program with source\t====\n"); if(context == NULL || context == (cl_context)0) { setErrCode(errcode_ret, CL_INVALID_CONTEXT); return (cl_program)0; } if(lengths == NULL || strings == NULL || count <= 0) { setErrCode(errcode_ret, CL_INVALID_VALUE); return (cl_program)0; } FILE *outputFile; time_t seconds; seconds = time (NULL); char fileName[33]; //convert integer to string sprintf(fileName,"%ld.c",seconds); PRINT_DEBUG("Creating file %s ...\n", fileName); if( (outputFile = fopen(fileName, "w")) == NULL) { fprintf(stderr, "Error opening file.\n"); return (cl_program)0; } PRINT_DEBUG("File created successfully\n"); PRINT_DEBUG("Writing to file ...\n"); int i; size_t sumSourceSize = 0; for(i=0; i<count; i++) { fputs(strings[i], outputFile); sumSourceSize += lengths[i]; } //close file so Lua can open it fclose(outputFile); PRINT_DEBUG("Finished writing to file.\n"); cl_program program = malloc(sizeof(struct _cl_program)); program->program_ref_count = 1; program->program_context = context; clRetainContext(context); program->program_num_devices = 0; program->program_devices = NULL; PRINT_DEBUG("Allocating memory for program_source\n"); program->program_source = malloc(sumSourceSize); PRINT_DEBUG("Concatanating strings together...\n"); //copy the first string and concat the rest strcpy(program->program_source, strings[0]); for(i=1; i<count; i++) { strcat(program->program_source, strings[i]); } lua_State *L = createLuaState(); PRINT_DEBUG("Loading kernel file %s...\n", fileName); loadKernelFile(L, fileName); char *kernelName; int numArgs; loadNextKernel(L, &kernelName, &numArgs); if(numArgs < 0 || kernelName == NULL) { PRINT_DEBUG("ERROR:numArgs returned less than zero\n"); setErrCode(errcode_ret, CL_INVALID_VALUE); return (cl_program)0; } // TODO: This next section is a bit messy. // It should be cleaned up. // Generating the linked list and then copying the data could // be put into separate functions. //temporary linked list struct struct list_element { char *funcName; int numArgs; struct list_element * next; }; struct list_element *head, *curr; head = NULL; int numKernels = 0; while(kernelName != NULL) { PRINT_DEBUG("Kernel name: %s Num args: %d\n", kernelName, numArgs); numKernels++; curr = malloc(sizeof(struct list_element)); curr->funcName = malloc(strlen(kernelName)); strcpy(curr->funcName, kernelName); //free(kernelName); curr->numArgs = numArgs; curr->next = head; head = curr; loadNextKernel(L, &kernelName, &numArgs); } lua_close(L); //TODO: Delete the temporary file curr = head; PRINT_DEBUG("Transfering func names to program object\n"); program->program_num_kernels = numKernels; program->program_kernel_names = malloc(numKernels * sizeof(char*)); program->program_kernel_num_args = malloc(numKernels * sizeof(int)); i=0; while(curr) { program->program_kernel_names[i] = malloc(strlen(curr->funcName)); strcpy(program->program_kernel_names[i],curr->funcName); program->program_kernel_num_args[i] = curr->numArgs; //the structs should be freed curr = curr->next; } //TODO: This function probably has a bunch of memory leaks. PRINT_DEBUG("\n====\tReturn program \t====\n"); return program; }