How to accelerate the execution of this code?
Show older comments
In the example below I have a matrix (J_inv(pos(:,j))) which is different in each iteration of the parfor-loop. I tried to vectorize somehow, but had no success. I found it to be the fastest solution with the parallel computing toolbox. This example has an average duration of roughly 20 seconds per for-loop iteration.
Hints:
- size of pos is 6x1000000
- self.inv_j is a 3x6 sym
Does anyone know how i can accelerate the code execution without decreasing the size of pos, so that i can increase the number of overall iterations?
for i = 1:n_iterations
% here parameter values are changed to random numbers that affect the inverse jacobian
% ...
%
self.update_inv_jacobian();
J_inv = matlabFunction(self.inv_j, 'Vars', {[self.all_dofs]});
parfor j = 1:length(pos)
Q = J_inv_const(pos(:,j)) * pos(:,k);
P = lsqminnorm(J_inv(pos(:,j)), Q);
errors(:,k) = pos(:,k) - P;
end
max_errors(:,i) = max(errors, [], 2);
end
2 Comments
Jonas
on 7 Jul 2023
i would start looking into the speed of each line. for that, change the parfor to for, add a 'profile on' before the i loop and a 'profile viewer' after the end of the i loop
Steven Lord
on 16 Aug 2023
What is J_inv_const in this code? Is it a symbolic expression, is it a function handle (perhaps created using matlabFunction on a symbolic expression), etc.?
I am a little surprised that your errors variable is not a sliced output variable, since its index isn't a function of the parfor loop variable.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!