I am working on OpencL grayscale code using c++. I am just a beginner in this, This is just a 2nd code from me with OpenCL. It is showing, invalid arguments errors in enquecommands.I am not able to solve why!!? can anyone helps me in this. Also I would some review that is it okay? I have uses buffer instead of image objects just for the try. I will try with image objects later on.
const int width;
const int height;
float *vec_h;
long count = width*height;
// std::vector<float> size(width*height,1);
vec_h = (float )malloc(countsizeof(float));
std::vector<cl::Platform> plat;
std::vector<cl::Device>dev;
try
{
//Find OCL platform
cl::Platform::get(&plat);
string value;
if(plat[0].getInfo(CL_PLATFORM_NAME,&value)!=CL_SUCCESS)
{
std::cout<<"No platform found"<<std::endl;
}
else
cout<<"Platform found : "<<value<<endl;
//Find device
if(plat[0].getDevices(CL_DEVICE_TYPE_GPU,&dev)!=CL_SUCCESS)
{
std::cout<<"No device found"<<std::endl;
}
//Create Context
cl::Context ctxt(dev,nullptr,0,0,&error);
//Create CommandQueue
cl::CommandQueue cque(ctxt,dev,CL_QUEUE_PROFILING_ENABLE);
//Create Program
std::ifstream sourceFileName("Test1.cl");
std::string sourceFile(std::istreambuf_iterator<char>(sourceFileName),(std::istreambuf_iterator<char>()))
cl::Program::Sources gray_source(1,std::make_pair(sourceFile.c_str(),sourceFile.length()+1));
cl::Program prog(ctxt,gray_source);
//BuildProgram
prog.build(dev);
//Create Kernels
cl::Kernel kernel(prog,"Grayscale",&error);
//Memory Objects
cl::Memory input, output;
input = cl::Buffer(ctxt,CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,width*height,NULL,&error);
output =cl::Buffer(ctxt,CL_MEM_WRITE_ONLY,width*height,0,&error);
//Setting Kernel Arguments
if(kernel.setArg(0, input)!=CL_SUCCESS)
{
std::cout<<"Error on input arg"<<std::endl;
}
if(kernel.setArg(1, output)!=CL_SUCCESS)
{
std::cout<<"Error on output arg"<<std::endl;
}
//Profiling Event
cl::Event e1;
const cl::NDRange globalsize = width*height;
//Enqueue Task
cque.enqueueTask(kernel,NULL,&e1);
//Write to target device
cque.enqueueWriteBuffer(input,CL_TRUE,0,width*height*sizeof(float),vec_h,0,NULL,&e1);
cque.enqueueNDRangeKernel(kernel,0,globalsize,0,NULL,&e1);
cque.enqueueReadBuffer(output,CL_TRUE,0,width*height*sizeof(float),vec_h,NULL,&e1);
}