When I solve a system of differential equations in MATLAB, the task manager shows that all the CPU cores are in use. This is not true when I solve the same system in Mathematica. I have six cores. MATLAB solve the system in 3.4 seconds and Mathematica solves it in 20.4 seconds -> 6*3.4 = 20.4.
Why doesn't Mathematica use all the cores? and How can I force it to use all of them?
Mathematica code:
SetDirectory[NotebookDirectory[]];
gamma = Import["gamma.csv"];
Y[t_] := ParallelTable[Subscript[y, i, i][t], {i, 1, 816}, {j, 1, 1}];
part1 = gamma.Y[t];
RHS = Table[part1[[k, 1]] - Total[gamma[[All, k]]]*(Y[t][[k, 1]]), {k, 1, 816}, {j, 1, 1}];
ini = {IdentityMatrix[816][[815]]}\[Transpose];
sol = NDSolve[{Y'[t] == RHS, Y[0] == ini}, Flatten[Y[t]], {t, 0, 800},
Method -> {"EquationSimplification" -> "Solve"}]; // AbsoluteTiming
MATLAB code:
The function inside ode:
function dy = test(t,y, Gamma)
dy = zeros(816,1);
part1 = Gamma*y;
for iter1 = 1:816
dy(iter1,1) = part1(iter1,1) - sum(Gamma(:,iter1)).*y(iter1,1);
end
and the code running the ode:
clc
clear all
Gamma = csvread('Gamma.csv');
kronDel = @(j, k) j==k ;
ini = zeros(1,816);
for iter1 = 1:816
ini(1,iter1) = kronDel(815,iter1);
end
tic
[t,y] = ode23tb(@(t,y)test(t,y,Gamma),[0 800],ini);
toc
You should download gamma
matrix from here.
Edit
I think this must be related to the fact that MATLAB uses vectorization and hence can use all the CPU cores but Mathematica fails to do the same thing. In this question,related to integration, MATLAB does integration faster because it uses vectorization and as a result uses all the cores however, Mathematica won't use all the cores in that question.
NDSolve
makes use of your parallel kernels, period. If matlab has a solver which does that and solves the system you want to solve faster and to good accuracy, then you might be better off to use matlab for that task. On the other hand I'm not sure whether you are comparing apples and oranges as the two programs most probably use very different methods and probably won't give results with comparable accuracy. $\endgroup$