Sheeep

reading data from CL Kernel

Discussion created by Sheeep on Dec 24, 2009
Latest reply on Dec 26, 2009 by genaganna

Hi all,

i worked with ati stream and brook+ for 1 year.

now i'm trying to work with opencl. My problem is, the cl kernel is running, but i don't get the output. I tried using cpu and gpu and allways i have this problem.

My CL code ist a simple addition:

 

#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void add(__global int *a,__global int *b,__global int *c){
    size_t tid = get_global_id(0);

        c[0]=a[0]+b[0];
   
}

 

 

And this is my hostcode using c++ bindings:

 

#include

#define alloca _alloca
#include
#include
#include
#include
#include
#include

int main(int argc, char** argv){
    try{
        //get CL platform info
        std::vector platforms;
            cl:: Platform::get(&platforms);
              std::vector::iterator i;
            if(platforms.size() > 0)
            {
                for(i = platforms.begin(); i != platforms.end(); ++i)
                {
                    if(!strcmp((*i).getInfo().c_str(), "Advanced Micro Devices, Inc."))
                    {
                        break;
                    }
                }
            }
         cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(*i)(), 0 };

        //creating CL context
        cl::Context context(CL_DEVICE_TYPE_GPU,cps);   
   
        //get CL device list
        std::vector devices = context.getInfo();

        //create CommandQuere
         cl::CommandQueue queue(context, devices[0]);

        //reading CL file
        std::ifstream file("HelloCL_Kernels.cl");
        std::string prog(std::istreambuf_iterator(file), (std::istreambuf_iterator())); 
        cl:: Program::Sources source(1, std::make_pair(prog.c_str(), prog.length()+1));

        // create CL program
        cl:: Program* pProgram=new cl:: Program(context, source);
        cl:: Program&    program = *pProgram;
        program.build(devices);

        //create CL kernel
        cl::Kernel kernel(program, "add");
       
        //create Host mem objects
        int wsize=1;
        float *hostIn1=new float[wsize];
        int *hostIn2=new int[wsize];
        int *hostOut=new int[wsize];
        hostIn1[0]=1;
        hostIn1[0]=5;

        //create CL mem objects
        cl::Buffer clIn1(context,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(float)*wsize,&hostIn1,NULL);
        cl::Buffer clIn2(context,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(float)*wsize,&hostIn2,NULL);
        cl::Buffer clOut(context,CL_MEM_WRITE_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(float)*wsize,&hostOut,NULL);

        //write mem objects to buffer
        queue.enqueueWriteBuffer(clIn1,CL_TRUE,0,sizeof(float)*wsize,hostIn1,NULL,NULL);
        queue.enqueueWriteBuffer(clIn2,CL_TRUE,0,sizeof(float)*wsize,hostIn2,NULL,NULL);
       

        //set kernelargs
        kernel.setArg(0,clIn1);
        kernel.setArg(1,clIn2);
        kernel.setArg(2,clOut);
       
        //run CL program
        queue.finish();
        queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(4,4), cl::NDRange(2, 2));
       
        //read buffer
        queue.enqueueReadBuffer(clOut,CL_TRUE,0,sizeof(float)*wsize,hostOut,NULL,NULL);

        queue.finish();

        std::cout<<<hostOut[0]<<std::endl;

        delete pProgram;
       
    }
    catch(cl::Error err){
     std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl;
    }

    return EXIT_SUCCESS;
}

do anybody nows, why i don't get the addition's result, but 0?

 

Outcomes