Why does the dot product operation on complex numbers not return an expected real value on gpuArray?

9 views (last 30 days)
I am performing a dot product using "dot" function on complex numbers and expecting a real value. When I do dot product on a CPU it gives me the expected output by returning a real value. However, when I do the same operation on a GPU, it gives me a complex number with imaginary part equal to 0. Is this expected behavior?
Please see the following code snippet  and it's output:
>> a=1+i;
dot(a,a)
ans =
2
>> a=gpuArray(a);
>> dot(a,a)
ans =
2.0000 + 0.0000i

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Jul 2022
Edited: MathWorks Support Team on 12 Jul 2022
This is an expected behavior with the GPU and it is based on the differences in how memory is assigned by MATLAB. On a GPU, we use an interleaved format for complex numbers. Hence, real and complex parts are stored together. On a CPU, we store the real and complex parts separately. 
In the case of a CPU, it is easy to detect when all the complex parts are zero and to drop that section of the memory. However, this is an expensive operation in the interleaved format, so the 0 value complex part is maintained on the GPU. Testing the two outputs with “isequal” will confirm that they are the same despite the slight difference in the data type. 
GPU also maintains the complexity of an output for functions which might return a complex output. 
So that, if they are then used in another downstream operation which might return a complex output then the data type continues. 
For more information, please visit the below documentation page: 

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!