If I create a gpuArray variable, do other variables that are derived from that variable go to the gpu too?

10 views (last 30 days)
If I declare a variable on the GPU and then I run a function on that variable and return the result of the function to another variable, does the new variable get instantiated on the gpu too? Or, do I need to explicitly send it to the gpu?
For example, in the code below, the variable I is created on the gpu. Does Ig also get created on the gpu? Or is it on the cpu?
Thanks. -Tony
I = gpuArray(imread('test.gif');
Ig = rgb2gray(I);

Answers (1)

Edric Ellis
Edric Ellis on 3 May 2016
In this case, Ig does indeed get created as a gpuArray. The general rule is that if a parameter of a function is considered to be "data", then a gpuArray passed as an argument for that parameter causes the computation to be performed on the GPU, and the output is a gpuArray.
In practice, this means that generally outputs of functions called with gpuArray arguments are also gpuArrays, except when either:
  1. The output is a size or dimension (this is not "data") - so size, numel, ndims for gpuArray data return ordinary MATLAB arrays.
  2. The input parameter is a size or dimension
As an example of the second case, consider
d = gpuArray(2);
x = magic(7);
result = sum(x, d);
In this case, the second argument to sum is a gpuArray - but the "data" in this case is an ordinary MATLAB array. So, d is automatically converted to an ordinary array (using gather behind the scenes), and the sum occurs on the CPU, and result is an ordinary (non-GPU) MATLAB array.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!