Results 1 to 4 of 4

Thread: Can't assign char numeric value in kernel

  1. #1

    Can't assign char numeric value in kernel

    I am having a silly problem I just can't figure out. In my kernel I want to use a char array to hold values in the range 0-255 but am unable to do so, when I read back the char array on the host side it has no values in it. I have a simple test kernel that tries to fill the output array with the char values 1 through 10 as an example of my problem and have tested it on Apples OpenCL implementation as well as NVIDIA's so I assume i'm doing something wrong. If I make the output array of type int it works correctly. Thanks in advance

    I use the khronos provided C++ bindings:

    main.cpp
    Code :
    #include <iostream>
    #include <fstream>
    #include "cl.hpp"
     
    int main(void)
    {
    	cl_int err = CL_SUCCESS;
     
    	cl::Context context(CL_DEVICE_TYPE_GPU, 0, NULL, NULL, &err); 
     
    	std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
     
    	char * outH = new char[10];
    	cl::Buffer outCL(context,CL_MEM_WRITE_ONLY| CL_MEM_USE_HOST_PTR,sizeof(cl_char)*10,outH,
    						 &err);
     
    	std::ifstream file("kernel.cl");
     
    	std::string prog(std::istreambuf_iterator<char>(file),(std::istreambuf_iterator<char>()));
     
    	cl::Program::Sources source(1,std::make_pair(prog.c_str(), prog.length()+1));
     
    	cl::Program program(context, source);
     
    	program.build(devices,"");
     
    	cl::Kernel kernel(program, "testKernel", &err);
     
    	kernel.setArg(0, outCL);
     
    	cl::CommandQueue queue(context, devices[0], 0, &err);
     
    	cl::Event event;
    	queue.enqueueNDRangeKernel(kernel, 
    									cl::NullRange,
    									cl::NDRange(10),
    									cl::NDRange(1, 1), 
    									NULL, 
    									&event);
    	event.wait();
     
    	queue.enqueueReadBuffer(outCL,CL_TRUE,0,sizeof(cl_char)*10,outH);
     
    	for (int i=0; i<10; i++) {
    		std::cout<<outH[i]<<std::endl;
    	}
     
        return 0;
    }

    and kernel.cl
    Code :
    #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
     
    __constant char charArray[] = {1,2,3,4,5,6,7,8,9,10};
     
    __kernel void testKernel(__global int * out)
    {
    	size_t tid = get_global_id(0);
    	out[tid] = charArray[tid];
    }

  2. #2

    Re: Can't assign char numeric value in kernel

    Sorry, I had a typo in the posted kernel, the global array on the device should be type char, not type int to duplicate the problem:
    Code :
    __kernel void testKernel(__global char * out)

  3. #3
    Senior Member
    Join Date
    Jul 2009
    Location
    Northern Europe
    Posts
    311

    Re: Can't assign char numeric value in kernel

    Do you get any errors? I don't see any error checking code, but perhaps the C++ framework translates them all into exceptions. If you're on MacOS X you can just set CL_LOG_ERRORS=stdout.

  4. #4

    Re: Can't assign char numeric value in kernel

    Yes, I removed the try/catch statement to make the code a little shorter and just tried CL_LOG_ERRORS=stdout and neither of them shows an error.

Similar Threads

  1. casting char* to struct
    By oconnellseanm in forum OpenCL
    Replies: 5
    Last Post: 07-28-2011, 10:38 AM
  2. passing an array of char* to kernel function
    By naroqueen in forum OpenCL
    Replies: 15
    Last Post: 12-16-2010, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •