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

3 次查看(过去 30 天)
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);

回答(1 个)

Edric Ellis
Edric Ellis 2016-5-3
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.

类别

Help CenterFile Exchange 中查找有关 GPU Computing in MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by