# Parallize vectorwise on GPU in Matlab using arrayfun

6 views (last 30 days)
Johannes Maul on 19 Feb 2021
Moved: Matt J on 18 Jan 2023
Hello together,
I'm a student who tries diffrent parallelisation techniques with Matlab. Therefore I'm using the Runge-Kutta (snippet1) to solve the van-der-pol equation(snippet 2).
code snippert 1:
function [y,x] = ownRungeKutta(t, x0, Mu)
y = zeros(length(t),2);
y(1,:) = x0;
x = t;
for i = 1:length(t)-1
h = abs(t(i)-t(i+1));
k1 = vdP_fkt1("",y(i,:)',Mu);
k2 = vdP_fkt1("",y(i,:)'+.5*k1*h,Mu);
k3 = vdP_fkt1("",y(i,:)'+.5*k2*h,Mu);
k4 = vdP_fkt1("",y(i,:)'+k3*h,Mu);
y(i+1,:) = y(i,:)'+((k1+2*k2+2*k3+k4)/6)*h;
end
end
The VDP function looks like: Code snippet 2:
function dxdt = vdP_fkt1(t,x,Mu)
dxdt= [x(2); Mu*(1-x(1)^2)*x(2)-x(1)];
end
Now, I know that the RungeKutta is not able to paralize. But I can paralize above diffrent parametrisations, which means we can use diffrent x0 and Mu to analyse the behavior of the function. In my opinion that should be very easy to paralize because its a trivial problem. Just call the funtion 100 times with diffrent parameters. The Problem is, that arrayfun just can be used elementwise. But x0 is a 1x2 vector and t is a n x 1 vector too. (t is to compare the own RungeKutta with other methods with dynamic steps). I also tried cellfun but that seems not running parallel on GPU...
So is there another possibility to paralize this on GPU?
Update:
I found a way to use GPUArrays in arrayfun with vectors (it's one of the examples, I did not recognize that), but I think it is not paralized over the size of i ( look at code snippet 3)
Code snippet 3:
for i = 1:anzGesamt
testGPUArray(i).T = gpuArray(t);
testGPUArray(i).x0 = gpuArray(1);
testGPUArray(i).x1 = gpuArray(0);
testGPUArray(i).Mu = gpuArray(Mutmp);
end
total = tic;
arrayfun(@(a) ownRungeKutta(a.T,a.x0,a.x1,a.Mu),testGPUArray);
totalTime = toc(total);
Everything is a gpuArray now also the variables in the code snippets 1-2. vdp_fkt1 also returns a gpuArray. But the overall code is slow as hell. Do I miss something?
It is also possible to debug it procedural. It looks like arrafun is executed linear. Is that normal?
Johannes

Alvaro on 18 Jan 2023
Moved: Matt J on 18 Jan 2023
Have you tried parallelizing using parfor?

### Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!