How to solve linear equation using GPU?

17 views (last 30 days)
Assume I need to solve a linear matrix equation Ax=b where A is a 3x3 non-singular matrix and b is a column vector of 3 elements. I know I can use inv(A)*b or A\b to get the solution, but the statement is run in CPU. Now I wanna make sure GPU is employed to run the same statement. Since I'm not familiar with GPU support in Matlab so I ask you for help: how to do it? Ideally I guess there may be a function like enableGPU(true) so that all subsequent statements will be run in GPU automatically. Also, I don't want to limit myself to the above statements. If there is some specific GPU functions like gpuInv(A), that's fine, please tell me that function. Hopefully the GPU version of Matlab code to solve linear equation could be faster than its CPU counterpart. Thanks a lot.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2017
Edited: Walter Roberson on 25 Jan 2017
gA = gpuArray(A);
gB = gpuArray(B);
x = gA \ gB;
If you test with
gputimeit(@() gA \ gB, 0)
and compare to
timeit(@() A \ B, 0)
then at least on my system, the CPU version is about 80 times faster.
Since the size is fixed and you promise the array is non-singular, you can do still better:
function sss = sim3(A,B)
t2 = A(1,1).*A(2,2).*A(3,3);
t3 = A(1,2).*A(2,3).*A(3,1);
t4 = A(1,3).*A(2,1).*A(3,2);
t7 = A(1,1).*A(2,3).*A(3,2);
t8 = A(1,2).*A(2,1).*A(3,3);
t9 = A(1,3).*A(2,2).*A(3,1);
t5 = t2+t3+t4-t7-t8-t9;
t6 = 1.0./t5;
sss = t6.*(A(1,2).*A(2,3).*B(3)-A(1,3).*A(2,2).*B(3)-A(1,2).*A(3,3).*B(2)+A(1,3).*A(3,2).*B(2)+A(2,2).*A(3,3).*B(1)-A(2,3).*A(3,2).*B(1));-t6.*(A(1,1).*A(2,3).*B(3)-A(1,3).*A(2,1).*B(3)-A(1,1).*A(3,3).*B(2)+A(1,3).*A(3,1).*B(2)+A(2,1).*A(3,3).*B(1)-A(2,3).*A(3,1).*B(1));t6.*(A(1,1).*A(2,2).*B(3)-A(1,2).*A(2,1).*B(3)-A(1,1).*A(3,2).*B(2)+A(1,2).*A(3,1).*B(2)+A(2,1).*A(3,2).*B(1)-A(2,2).*A(3,1).*B(1));
This averages 20 times faster than \ on the CPU and averages 1500 times faster than \ on the GPU.
Communications overhead with the GPU is responsible for the relative slowness. If you were using larger matrices then at some point the GPU would be faster.

More Answers (1)

John D'Errico
John D'Errico on 25 Jan 2017
Do you have the proper toolbox? I would suggest this:
https://www.mathworks.com/products/parallel-computing.html
If you don't have it, then, no, you cannot simply invoke computations on your GPU.

Products

Community Treasure Hunt

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

Start Hunting!