Description Usage Arguments Value Author(s) See Also Examples
These functions perform the (potentially) necessary copying of data
between the host and the GPU device.
The functions codeToDevice
and copyFromDevice
are the explicit functions. We can use R's subsetting
to copy the elements with ref[]
and ref[] = x
.
1 2 3 4 5 | copyFromDevice(obj, nels, type)
copyToDevice(obj, to = cudaMalloc(length(obj), elType = elType, strict = strict),
elType = typeof(obj), strict = !missing(elType) )
cudaMalloc(numEls, sizeof = 4L, elType = NA,
strict = !missing(elType) || inherits(elType, "AsIs"))
|
obj |
the R object to be copied to the GPU or the reference to the GPU memory that is to be copied back to R |
nels,numEls |
the number of elements to be copied or to be allocated |
type |
the type of the elements being copied |
elType |
the name of the R/GPU type to be allocated |
sizeof |
the size of the element in the array/vector on the GPU to be allocated |
to |
a reference to space on the GPU to store the data being copied from R to the GPU |
strict |
a logical value that controls whether we treat the value
|
cudaMalloc
returns a generic pointer to memory on the GPU.
copyToDevice
returns an object that knows both the type and the
number of elements of the array and so can retrieve this from the GPU.
copyFromDevice
returns the R vector corresponding to the
data on the GPU.
Duncan Temple Lang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if(getNumDevices() > 0) {
cuGetContext()
N = 1e5
x = rnorm(N)
cx = copyToDevice(x)
length(cx[])
# or explicitly with
vals = copyFromDevice(cx)
length(vals)
i = sample(1:N, 100)
# subsetting on the GPU
cx[i]
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.