Search code examples
openclpyopencl

Re-utilizing the openCL memory


Hello Everybody, as an example consider two openCL kernel one kernel let us say add and other is sub.

the add kernel is

__kernel void add(global int *output1,global int *input1,global int *input2
    /* Put other parameters here */
    ) 
       {
    int i = get_global_id(0);
    output1[i] = input1[i] + input2[i];
       }

the sub kernel is

__kernel void add(global int *output2,global int *input1,global int *input2
    /* Put other parameters here */
    ) 
        {
    int i = get_global_id(0);
    output2[i] = input1[i] - input2[i];
       }

for these two kernels whose 2 inputs are same i need to copy same inputs(input1 & input2) twice to the device from host memory and that may add some cost in terms of performance.
Is there any way so that i can copy the data once and re-utilize it in any function till i am not releasing the memory?


Solution

  • Making this an answer since it seems to fully answer the question.

    You normally create the buffer in device memory, and you can reuse these buffers by redefining the kernel arguments via clSetKernelArg() (unless, of course, you want to use them at the same time which is trickier, and I'm not sure it's even allowed by the OpenCL standard in fact).