OpenCL kernel performs only once

Hi everyone,

I’m experiencing an issue with my (real simple, test purpose) OpenCL kernel : it seems to work fine the first time I execute my host program but then it won’t behave like it should and I don’t seem to be able to understand wtf is wrong with the code, I hope someone will be able to help !

Here’s the host code :

int SomeClass::someFunction() {

	cl_int err = CL_SUCCESS;
	try {

		vector<cl::Platform> platforms;
		cl::Platform::get(&platforms);
		if (platforms.size() == 0) {
			cout << "Platform size 0
";
			return -1;
		}
		
		cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
		cl::Context context(CL_DEVICE_TYPE_CPU, properties); 
		
		vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();

		cout << "Max compute units : " << devices[0].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << endl;
		
		cl::Program::Sources source;

		ifstream inputFile("kernel.cl");
		string *prog = new string(istreambuf_iterator<char>(inputFile),(istreambuf_iterator<char>()));
		source.push_back(make_pair(prog->c_str(), prog->length()));

		cl::Program program_ = cl::Program(context, source);
		program_.build(devices);

		cl::Kernel kernel(program_, "SimpleKernel", &err);

		int tab[10] = {0};
		cl::Buffer buffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_t(10), tab);

		kernel.setArg(0,buffer);

		cl::Event event;
		cl::CommandQueue queue(context, devices[0], 0, &err);

		queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(8), cl::NDRange(2), NULL, &event); 

		event.wait();

		for (int i=0 ; i<10 ; i++)
			cout << endl << tab[i];
		cout << endl;
	}
	catch (cl::Error err) {
		cerr << "ERROR: " << err.what() << "(" << err.err() << ")" << endl;
	}

	return EXIT_SUCCESS;
}

and the kernel code (kernel.cl) :

__kernel void SimpleKernel(__global int *output) {
	output[get_global_id(0)] = get_global_id(0);
	printf("%d %d
", get_local_id(0), get_global_id(0));
}

My goal was to fill an int array (called tab) with get_global_id(0) : its size is ten, initialised to 0. After kernel execution it should look like this : tab[0] = 0, tab[1] = 1, tab[2] = 2, etc… and IT WORKS but only the first time I execute the program.
When I try and printf the array, it outputs something like “0 1 2 3 4 5 6 7 0 0” but then, when I try it again it always outputs “0 0 0 0 0 0 0 0 0 0” and I have to restart my computer in order to make it work another time.

I can’t seem to figure what’s wrong, help me I’m going mad on this T_T

Thx in advance.

Foin

Normally this behavior is because you are accessing illeagal memory areas. One problem is, that your Buffer is just 10 byte (cl::Buffer buffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_t(10), tab):wink: but you write to 40 byte (sizeof(cl_int)*10)