Why does norm(gpuArray) return a double in Matlab 2015b?
4 views (last 30 days)
Show older comments
I have Matlab 2015b and Matlab 2017a, and I noticed that the two versions of Matlab were treating "norm" differently. One would output a double and the other would output a gpuArray. The example is below.
Matlab 2017a
class(norm(gpuArray)) = gpuArray
Matlab 2015b
class(norm(gpuArray)) = double
My question is: Is Matlab 2015b gathering the data off the gpu every time you run a built in function like norm?
0 Comments
Accepted Answer
Joss Knight
on 10 Aug 2017
Edited: Joss Knight
on 10 Aug 2017
The debate over this could go round the houses 100 times! The logic was that scalars should always be returned on the CPU because it's more efficient to do scalar operations there. But all gpuArray scalar operations are computed on the host anyway (for efficiency), and it's just as likely that the result of norm will be used in an array operation (such as normalizing an array). So the new logic is that scalars that are computed on the GPU should stay on the GPU, because to gather them to the CPU incurs a device synchronization that could slow down people's code. This is annoying if you immediately pass the norm to a function that doesn't support gpuArray, but it's easy enough to call gather in that case.
So the answer to your question is no, MATLAB always returns results on the GPU, with the exception of querying array properties ( size, nnz, bandwidth etc). But norm used to be an outlier to that guarantee.
0 Comments
More Answers (0)
See Also
Categories
Find more on GPU Computing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!