OpenCL buffers are just like numeric or integer vectors that reside on the
GPU and can directly be accessed by kernels. Both non-scalar arguments to
oclRun
and its return type are OpenCL buffers.
Just like vectors in R, OpenCL buffers have a mode, which is (as of now) one
of "double" or "numeric" (corresponds to double
in OpenCL C), "single"
(float
) or "integer" (int
).
The constructor clBuffer
takes a context as created by
oclContext
, a length and a mode argument.
The conversion function as.clBuffer
creates an OpenCL buffer of the
same length and mode as the argument and copies the data. Conversely,
as.double
(= as.numeric
) and as.integer
read a buffer and
coerce the result as vector the appropriate mode.
With is.clBuffer
one can check if an object is an OpenCL buffer.
The methods length.clBuffer
and print.clBuffer
retrieve the
length and print the contents, respectively.
Basic access to the data is available via [...]
. Note that
only contiguous memory operations are supported on GPU buffers, so if
the index does not reference a contiguous region then the
subsetting/assignment will be performed by retrieving the entire
buffer and perfroming the operation in R which is very expensive.
Note that unlike regular R objects GPU buffers are by design mutable,
i.e. the object is only a reference to the GPU memory and thus any
modification will affect all refernces. The contents can be emerged into R
via x[]
at which point the result is a regular R vector and no
longer tied to the source buffer. Analogously, x[] <- value
is
the canonical way to replace the entire contents of the buffer where
value
must have the same length as the buffer (no recycling).