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

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

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2022-7-12
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: 

更多回答(0 个)

类别

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

标签

尚未输入任何标签。

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by