cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

licoah
Journeyman III

Problem by using clBuildProgram

hello

I have written a program according to Template in SDK.

I got this error message Error: Building Program (clBuildProgram) by running this program.

Can somebody help me?

int initializeHost(Array2D<float> images) { image = NULL; gradx = NULL; grady = NULL; width = images.getsize(); ///////////////////////////////////////////////////////////////// // Allocate and initialize memory used by host ///////////////////////////////////////////////////////////////// cl_uint sizeInBytes = width * sizeof(cl_float); image = (cl_float *) malloc(sizeInBytes); if(image == NULL) { std::cout<<"Error: Failed to allocate image memory on host\n"; return 1; } gradx = (cl_float *) malloc(sizeInBytes); if(gradx == NULL) { std::cout<<"Error: Failed to allocate gradx memory on host\n"; return 1; } grady = (cl_float *) malloc(sizeInBytes); if(grady == NULL) { std::cout<<"Error: Failed to allocate grady memory on host\n"; return 1; } image = images.begin(); return 0; } /* * Converts the contents of a file into a string */ std::string convertToString(const char *filename) { size_t size; char* str; std::string s; std::fstream f(filename, (std::fstream::in | std::fstream::binary)); if(f.is_open()) { size_t fileSize; f.seekg(0, std::fstream::end); size = fileSize = f.tellg(); f.seekg(0, std::fstream::beg); str = new char[size+1]; if(!str) { f.close(); return NULL; } f.read(str, fileSize); f.close(); str[size] = '\0'; s = str; return s; } return NULL; } /* * \brief OpenCL related initialization * Create Context, Device list, Command Queue * Create OpenCL memory buffer objects * Load CL file, compile, link CL source * Build program and kernel objects */ int initializeCL(void) { cl_int status = 0; size_t deviceListSize; ///////////////////////////////////////////////////////////////// // Create an OpenCL context ///////////////////////////////////////////////////////////////// context = clCreateContextFromType(0, CL_DEVICE_TYPE_CPU, NULL, NULL, &status); if(status != CL_SUCCESS) { std::cout<<"Error: Creating Context. (clCreateContextFromType)\n"; return 1; } /* First, get the size of device list data */ status = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &deviceListSize); if(status != CL_SUCCESS) { std::cout<< "Error: Getting Context Info \ (device list size, clGetContextInfo)\n"; return 1; } ///////////////////////////////////////////////////////////////// // Detect OpenCL devices ///////////////////////////////////////////////////////////////// devices = (cl_device_id *)malloc(deviceListSize); if(devices == 0) { std::cout<<"Error: No devices found.\n"; return 1; } /* Now, get the device list data */ status = clGetContextInfo( context, CL_CONTEXT_DEVICES, deviceListSize, devices, NULL); if(status != CL_SUCCESS) { std::cout<< "Error: Getting Context Info \ (device list, clGetContextInfo)\n"; return 1; } ///////////////////////////////////////////////////////////////// // Create an OpenCL command queue ///////////////////////////////////////////////////////////////// commandQueue = clCreateCommandQueue( context, devices[0], 0, &status); if(status != CL_SUCCESS) { std::cout<<"Creating Command Queue. (clCreateCommandQueue)\n"; return 1; } ///////////////////////////////////////////////////////////////// // Create OpenCL memory buffers ///////////////////////////////////////////////////////////////// imageBuffer = clCreateBuffer( context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(cl_float) * width, image, &status); if(status != CL_SUCCESS) { std::cout<<"Error: clCreateBuffer (inputBuffer)\n"; return 1; } gradxBuffer = clCreateBuffer( context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(cl_float) * width, gradx, &status); if(status != CL_SUCCESS) { std::cout<<"Error: clCreateBuffer (outputBuffer)\n"; return 1; } gradyBuffer = clCreateBuffer( context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, sizeof(cl_float) * width, grady, &status); if(status != CL_SUCCESS) { std::cout<<"Error: clCreateBuffer (outputBuffer)\n"; return 1; } ///////////////////////////////////////////////////////////////// // Load CL file, build CL program object, create CL kernel object ///////////////////////////////////////////////////////////////// const char * filename = "gradient.cl";//?????????????????????? std::string sourceStr = convertToString(filename); const char * source = sourceStr.c_str(); size_t sourceSize[] = { strlen(source) }; program = clCreateProgramWithSource( context, 1, &source, sourceSize, &status); if(status != CL_SUCCESS) { std::cout<< "Error: Loading Binary into cl_program \ (clCreateProgramWithBinary)\n"; return 1; } /* create a cl program executable for all the devices specified */ status = clBuildProgram(program, 1, devices, NULL, NULL, NULL); if(status != CL_SUCCESS) { std::cout<<"Error: Building Program (clBuildProgram)\n"; return 1; } /* get a kernel object handle for a kernel with the given name */ kernel = clCreateKernel(program, "gradient_Kernel", &status); if(status != CL_SUCCESS) { std::cout<<"Error: Creating Kernel from program. (clCreateKernel)\n"; return 1; } return 0; } ****************************************************************************************** .cl: __kernel void gradientKernel(__global float* image, __global float* gradx, __global float* grady, __constant uint width) { uint tid = get_global_id(0); gradx[tid] = image[tid+1] - image[tid]; grady[tid] = image[tid+width] - image[tid]; }

0 Likes
1 Reply
genaganna
Journeyman III

Originally posted by: licoah hello

I have written a program according to Template in SDK.

I got this error message Error: Building Program (clBuildProgram) by running this program.

Can somebody help me?

Please use clGetProgramBuildInfo to know what is actual error in kernel.   Please use following attached kernel code to resolve your problem. __constant parameter to kernel must be a pointer.

 

__kernel void gradientKernel(__global float* image, __global float* gradx, __global float* grady, uint width) { uint tid = get_global_id(0); gradx[tid] = image[tid+1] - image[tid]; grady[tid] = image[tid+width] - image[tid]; }

0 Likes