Massive increase in execution speed with MEX function?
조회 수: 6 (최근 30일)
이전 댓글 표시
Dear all,
I was experimenting with accelerating my code through MEX functions. See the code below (it solves a system of linear equations with constraints using the lsqlin function in each voxel of a computed tomography dataset - 512 x 512 x 163 voxels). It took about 90 minutes to process the whole dataset. When I converted the code to a MEX function and ran it, it only took 2.3 seconds.
I'm very happy with that, just wanted to ask if a such a massive speed-up is something you would expect with MEX functions (it's the first time I used it).
Thank you in advance.
Samuel
...
parfor k = 1:nslices
for j = 1:ncols
for i = 1:nrows
if low_image(i,j,k) > -521
d = [low_image_bf(i,j,k); high_image_bf(i,j,k)];
Aeq = [1 1 1];
beq = 1;
opts = optimoptions("lsqlin","Display","off");
x = lsqlin(C,d,[],[],Aeq,beq,[0 0 0],[1 1 1],[],opts);
mat_Fe(i,j,k) = x(1); % frakcia zeleza
mat_Fat(i,j,k) = x(2); % frakcia oleja
mat_Soft(i,j,k) = x(3); % frakcia vody (agaru+trocha jodu)
end
end
end
kstr = string(k);
msg = strcat(' Slice ', kstr, ' spracovany');
disp(msg)
end
댓글 수: 5
Raymond Norris
2023년 4월 12일
It almost seemed as though the compiled version wasn't running the exact same data set. What puzzles me though is if you are using the MATLAB Coder and not hand coding this yourself, wouldn't the compiled version also have the mistaken variable name? To ensure there aren't any other differences, have you compared the results (mat_*) to ensure that they are identical between compiled and non-compiled?
Is the local pool of 8 workers already started before you call tic?
Is 8 a factor of nslices?
Small suggestion, pull
Aeq = [1 1 1];
beq = 1;
opts = optimoptions("lsqlin","Display","off");
to just after the parfor call. These are constant values that don't need to be reassigned in each iteration.
I'm not an Optimization guy, but I'm wondering if there's any value in tinkering with the Algorithm. This link is in reference to fmincon, but it should apply to lsqlin as well.
Ryan Livingston
2023년 4월 12일
편집: Ryan Livingston
2023년 4월 12일
MATLAB Coder dev team member here: speedups on the order of 100x are not unheard of, though that's a big speedup indeed. I second Raymond's advice to sanity check things.
If you fixed the variable naming bug in your MATLAB code, did you regenerate the MEX code with Coder?
Is the MEX returning equivalent answers to running your code in MATLAB? If you're using floating point, the answers can't be expected to be identical but should have similar quality. For things like optimization solvers it's possible that expected numeric differences cause convergence to a different answer making one implementation vastly slower than another.
If you wrap the call to your function / mex in tic;toc; do you see similar speedups? Namely tic; yourFunction(); toc vs tic; yourFunction_mex(); toc.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!