Parallelization is not working in Matlab

10 views (last 30 days)
Chang seok Ma on 30 Mar 2021
Answered: Nipun on 14 May 2024 at 6:06
Hello,
I am trying to paralleliza my code in Matlab. Below is a part of my code.
Basically, I am finding the minimum position and the value given 4 state variables.
I want to speed up my code by using parallelization so I changed 'for i_a = 1:Na' to 'parfor i_a = 1:Na' but it doesn't seem like Matlab parallelize the code because if I see the result at the end of each loop (disp([i_a i_d i_y i_t toc])), it seems like Matlab is calculating minimum value one by one. (And I don't think the code is faster also)
Am I doing something wrong? (I don't think there is false sharing issue here because the one I update is Vnew and Vnew is not used during the calculation)
for i_a = 1:Na %Loop over state variable a
for i_d = 1:Nd %Loop over state variable d
for i_y = 1:Ny %Loop over state variable y
for i_t = 1:Nt %Loop over state variable t
tic;
+ ((wage*hour*YS(i_y) + T(i_t) + A(i_a)*R + (1-delta)*D(i_d) - adjf(1) - adjf(2) - fixed*(1-delta)*D(i_d))<=0)*(-1e10));
if i_d == 1
end
+ ((wage*hour*YS(i_y) + T(i_t) + A(i_a)*R - noadjf)<=0)*(-1e10));
lb = [a_min-10,d_min-10];
ub = [a_max+10,d_max+10];
a = [];
b = [];
aeq = [];
beq = [];
x0 = (lb + ub) / 2;
options = optimoptions('fmincon','Display','off');
else
end
disp([i_a i_d i_y i_t toc])
end
end
end
end

Nipun on 14 May 2024 at 6:06
Hi Chang Seok,
I understand that you intend to parallelize your MATLAB code to enhance its performance, particularly by utilizing the `parfor` loop for iterating over one of the state variables in your nested loop structure. However, you've observed that the execution does not seem to be parallelized effectively, as indicated by the sequential display of loop iterations and a lack of noticeable speed improvement.
1. Parallel Pool
Ensure that a parallel pool is active before executing the `parfor` loop. If a parallel pool is not already open, MATLAB will attempt to open one, which can add overhead, especially if the loop is relatively short. You can manually start a parallel pool using `parpool`
The effectiveness of parallelization is more pronounced for loops where each iteration takes a significant amount of time. If the computations within each iteration are relatively quick, the overhead of distributing tasks among workers can outweigh the benefits. Your nested `fmincon` optimizations seem computationally intensive, which should, in theory, benefit from parallelization.
3. Data Transfer and Dependencies
`parfor` loops work best when each iteration is independent of others, minimizing the need for data transfer between workers. Your code appears to follow this principle, as each iteration writes to unique indices of `Vnew`, `indpol_ap`, `indpol_dp`, and `indadj`. However, ensure that all variables used within the loop are properly initialized and that there are no hidden dependencies.
4. Profiling and Optimization
Consider using MATLAB's Profiler to identify bottlenecks in your code. The Profiler can help you understand where the most time is spent and guide optimization efforts:
profile on
profile off
profile viewer
If after these considerations, you still do not observe improved performance, it might be worth examining more closely the specific computations within the loop or consulting MATLAB's parallel computing documentation for more advanced techniques, such as chunking iterations or optimizing data transfer between workers.
Hope this helps.
Regards,
Nipun