gpuSapply: A GPU version of the sapply function

Description Usage Arguments Details Value Examples

View source: R/gpuApply.R

Description

Please refer to sapply to see the basic usage

Usage

1
2
3
4
5
6
7
8
9
gpuSapply(
  X,
  FUN,
  ...,
  .macroParms = NULL,
  .device = "auto",
  loading = "auto",
  .options = gpuSapply.getOption()
)

Arguments

X

a vector that FUN will loop over.

FUN

The function to be applied to each element of X

...

optional arguments to FUN

.macroParms

The function argument that will be treated as macro in the code. If an argument is treated as macro, its value cannot be changed by the code

.device

the device ID(s) indicates the device that the function will be excuted on. Running the code on Multiple devices is supported but is still under development

loading

The loading of each device, only useful when having multiple devices.

.options

The package and openCL compilation options, please call gpuSapply.getOption() to get all the available options

Details

This function compiles the R code and runs it on the openCL-compatible devices. The usage is similar to the sapply function with some addtional opencl-related arguments.

Value

A vector or a matrix

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#matrix multiplication function
matMul = function(ind,A,B){
C = A%*%B[,ind]
return(C)
}

n = 100
m = 200
k = 100
#Create the data
A = matrix(runif(n*m),n,m)
B = matrix(runif(k*m),m,k)
#Perform matrix multiplication
#GPU
res_gpu = gpuSapply(1:k,matMul,A,B)
#CPU
res_cpu = sapply(1:k,matMul,A,B)

#error
range(res_gpu-res_cpu)

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