Passing 2 times the same buffer as input ?

Hi,

I’m reading an already existing OpenCL code and it’s the first time I see the following.
The code as different layers of complexity so it’s difficult for me to really understand what’s going on.
But basically, I have the feeling that the author is passing a buffer as argument to a kernel 2 times : one time as read only and the other time as write only…
It C++, the code would be something like that:


// Function
void f(cont int * input, int * output){
...
}

// Calling the function
int * buffer;
...
f(buffer, buffer);

I thought that the compiler would complain and generate an error.
But my concern is more about the performance…
Have you already seen something like that? What do you think?

Thanks,

Vincent

If the Buffer really is a Read Only Buffer, this should not work. If it is only defined as const in that function this may work. Is it a local funktion in the kernel or the kernelfunction itself?

The kernel does something like that:


__kernel void myKernel(
__global const float * input,
__global float * output,
int size) {
...
output[i] = something * input[i];
}

hmm, this shouldn’t work i think. but the compiler will not complain because he can’t know that you are passing invalid arguments. The buffer definition mustn’t be Read_Only

In the code there should be a place using clCreateBuffer(), with CL_MEM_READ_ONLY / CL_MEM_WRITE_ONLY / CL_MEM_READ_WRITE. May be you could use CL_MEM_READ_WRITE and send only one arguement to let it read/write data. I would simply make 2 buffers to set one as an input, and the other one as an output.

I get that :slight_smile: My question is different. The code has been written by somebody else and there are like 10 layers of code above OpenCL.
The way I see it is that the initial coder called a function where the input and the output buffers are the same OpenCL memory address.
That’s why I got confused. Why would anybody do this?
Maybe, in the different layers under this function, one check if the memory addresses are the same and if yes use a different temporary buffer and then copy value back in the initial buffer.
To be continued…