gpuSapply {gpuMagic} | R Documentation

## A GPU version of the sapply function

### Description

Please refer to sapply to see the basic usage

### Usage

    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

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

[Package gpuMagic version 1.8.0 Index]