Harry
Harry

Reputation: 1115

OpenCL: clSetKernelArg vs. clSetKernelArg + clEnqueueWriteBuffer

A question regarding buffer transfer in OpenCL: I want to pass a buffer (cl_mem) from the host to the kernel (i.e. to the device). There are two host-functions:

I use clSetKernelArg to pass my buffer to one of the kernel arguments. But does this mean that the buffer is automatically transfered to the device? Further, there is the function clEnqueueWriteBuffer which writes a buffer to a device.

My question: is there any difference in using (a.) only clSetKernelArg or (b.) clSetKernelArg and clEnqueueWriteBuffer in combination for my use-case (pass buffers to kernel)?

Upvotes: 1

Views: 1288

Answers (1)

noma
noma

Reputation: 1229

You have to call both functions before enqueuing a kernel for execution.

clSetKernelArg

Used to set the argument value for a specific argument of a kernel.

This one only sets the argument value, e.g. some pointer, for the called kernel. There are no implicit data transfers.


Think of the following examples:

  • the same memory object is used as argument for different kernels
    • => only one write to device needed; but multiple arguments to be set for different kernels
  • a changing input memory object could be used multiple times with the same kernel
    • => one write per call; but kernel argument only set once
  • a read and a write buffer might be switched using clSetKernelArg() between two calls of the same kernel (double buffering)
    • => maybe no transfer, or only every n iterations; but two arguments set before every call

In general: Data transfers between host and compute device are very expensive, and hence should be avoided, which is best possible by explicitly triggering them.

Upvotes: 2

Related Questions