Speedup with GPU

Main functions

Any script implemented using the library can be easily switched to GPU computation. To that end, one only needs two simple functions:

  • useGPU

  • gpuCpuConverter

The function useGPU() takes one single argument which can be

The function gpuCpuConverter() allows to convert numeric variables to the correct type according to the option selected with useGPU() (respectively, double, gpuarray or cuda). For instance, if x is a double and useGPU is set to 0, then x=gpuCpuConverter(x) will not change x. But if x is a double and useGPU is set to 1, then x=gpuCpuConverter(x) will convert x to a gpuarray (and send it to the graphic card). Hence, for each created/loaded variable “x” which is sufficiently large (i.e. not for small vectors), one has to add the following line to the script


Then, the switch between CPU and GPU computation is simply controlled by useGPU() which can be placed at the beginning of the script.

Functions which generate data

Matlab functions such as ones() and zeros() require a memory allocation. When GPU is activated, it is faster to generate directly these data on the graphic card instead of allocating CPU memory and then transfering them to the GPU. In order to make things transparents and keep the code clear, all the occurences of ones() and zeros() (i.e. within scripts but also within library classes) should be replaced by ones_() and zeros_(). These two functions have been defined in order to allocate the memory on the CPU or directly on the GPU according to the state of useGPU.

Concrete example

The 3D deconvolution example provided in the “Example/” folder of the library shows a concrete use of the GPU functionality.