subRef: Get a reference of the subset of a matrix

Description Usage Arguments Details Value Warning Examples

View source: R/pkgFunc.R

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

1
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:

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

In the compilation stage, the code will be changed to

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#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)

gpuMagic documentation built on Nov. 8, 2020, 5:15 p.m.