why MATLAB gpuarray is much slower in just adding two matrices?

瘦欲@ 提交于 2019-12-02 06:07:19

ehsan,

Titan is powerful.

I hope the following might help.

1> GPU has many (from hundred to thousands) low frequency stream cores, which means they have to execute the same instructions. So, they are very good at SIMD instructions. If you are doing to compute only one element of a matrix (the first example and the last), GPU is definitely not good at this.

2> For the second test, please involve the index i into the expression to eliminate optimization from compiler. Or, you can try to change 10000 to 50000 to see whether there is a difference.

for i=1:10000
a6=i*(0.5.*(a5-a0))-(a1+a2+a3)-(a5.*U./3.0)-(a5.*V./2.0)+(0.25.*a5.*a4);  
end    

3> CPU has its own Vector Processing Unit (VPU), which is also aimed for SIMD. The only problem is that, it is quite small, from 64 bit to 256 bit. So, if the matrix is small, CPU is much better than GPU. Therefore, to see the performance benefit of GPU, you can try a larger dimension, say, 5000x5000.

Please let me know if you have any further results on this.

I think your second GPU result (i.e. vectorised GPU calls) is the most pertinent - GPUs are most efficient when operating on large amounts of data in a vectorised fashion. In your case, you can probably get even better performance by converting your expression into an arrayfun call. arrayfun allows MATLAB to convert the entire expression into a single operation on the GPU, which takes best advantage of the (huge) available memory bandwidth of the device.

As to your problem calculating a6(1,1) - perhaps it might be best to calculate the whole array (i.e. don't index the right-hand-side expressions) and then index afterwards. Something like

tmp = (0.5.*(a5-a0))-(a1+a2+a3)-(a5.*U./3.0)-(a5.*V./2.0)+(0.25.*a5.*a4);
a6(1,1) = tmp(1,1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!