Learn R Programming

OpenCL (version 0.2-10)

clBuffer: Create and handle OpenCL buffers

Description

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).

Usage

clBuffer(context, length, mode = c("numeric", "single", "double", "integer"))
as.clBuffer(vector, context, mode = class(vector))
is.clBuffer(any)
# S3 method for clBuffer
as.double(x, ...)
# S3 method for clBuffer
as.integer(x, ...)
# S3 method for clBuffer
print(x, ...)
# S3 method for clBuffer
length(x)
# S3 method for clBuffer
[(x, i)
# S3 method for clBuffer
[(x, i) <- value

Arguments

context

OpenCL context as created by oclContext

length

Length of the required buffer

mode

Mode of the buffer, can be one of "numeric", "single", "double" or "integer"

vector

Numeric or integer vector or clFloat object

any

Arbitrary object

x

OpenCL buffer object (clBuffer)

i

index specifying elements to extract or replace

value

New values

...

Arguments passed to subsequent methods

Author

Aaron Puchert and Simon Urbanek

See Also

oclContext, oclRun

Examples

Run this code
library(OpenCL)

## Only proceed if this machine has at least one OpenCL platform
if (length(oclPlatforms())) {

ctx<-oclContext()

buf<-clBuffer(ctx, 16, "numeric")
# Do not write buf<-..., as this replaces buf with a vector.
buf[]<-sqrt(1:16)
buf

intbuf<-as.clBuffer(1:16, ctx)
print(intbuf)

length(buf)
as.numeric(buf)
buf[]

buf[3:5]
buf[1:2] = 0
buf

## clBuffer is the required argument and return type of oclRun.
## See oclRun() examples.
}

Run the code above in your browser using DataLab