subRef: Get a reference of the subset of a matrix

View source: R/pkgFunc.R

subRefR Documentation

Get a reference of the subset of a matrix

Description

The function will get a reference of the matrix subset. This is a 0-copy method, which means any change in the reference variable will cause the change in the original matrix. The function is useful when the GPU memory is limited or you do not want to create a copy the data. DO NOT call this function in R, this is for openCL code only(eg. gpuSapply).

Usage

subRef(variable, i = "", j = "")

Arguments

variable

the matrix you want to subset

i

the index of a vector or the row index of a matrix

j

(Optional) The column index of a matrix

Details

The package implement this function purely using the code. it will not actually be called on device side. For example, if we have the following code:

#Alternative of B=A[ind]
B=subRef(A,ind)
a=B[2]

In the compilation stage, the code will be changed to

a=A[ind[2]]

The variable B does not exist in the code after the compilation and therefore no memory is allocated for it.

Value

A reference to the subset of a matrix

Warning

Since this feature is implemented like a macro, so it is possible to change the value of ind after the matrix B is created and before you modify the matrix B. In such case, it may cause an unexpected error. It is a good practice to keep the ind same while using the subset reference.

Examples

#create data
ind=1:10
A=matrix(0,100,100)
#Use the one-index subsetting, create a vector of length 10
B=subRef(A,ind)
#Subsetting the matrix A,create a 10-by-10 matrix
C=subRef(A,ind,ind)
#row subsetting 
D=subRef(A,ind,)
#column subsetting
E=subRef(A,,ind)

Jiefei-Wang/gpuMagic documentation built on March 27, 2022, 5:23 a.m.