Speed up code by eliminating loop
Show older comments
Dear all,
I have to do a maximization of a 3-D array V(e,b,d) over the first-dimension, obtaining something that I call e_opt which will be a b*d matrix, i.e.
e_opt(b,d) = argmax_e {V(e,b,d)}
The non-standard feature is that the grid for e depends on d. So I am not able to eliminate the loop over d and I need to speed up the code. I attach a MWE so that things become clear. On my PC it runs in about 1 secs but this is too slow since I have to do this many times.
Any help is greatly appreciated!
clear
close all
clc
rng("default")
% Size of grids
n_b = 1000;
n_d = 1000;
n_e = 5000;
% Parameters
delta = 0.2696037674949296;
%omega_0 = 0.0015534835229589;
%omega_1 = 0.0013023827114628;
% Generate fake data
cost_mat = rand(n_e,n_d);
prob_mat = rand(n_e,n_d);
option_nob = rand(n_b,1);
d_grid = linspace(0,2.3,n_d)';
%% Create a grid for "e". The non-standard feature is that the upper bound
% of the e_grid depends on "d"
e_grid_mat = zeros(n_e,n_d);
e_min = 0;
space = 1.5;
for d_c=1:n_d
d_val = d_grid(d_c);
e_max = 0.1+0.2*d_val; %silly example, don't take this literally
% e_grid is NOT equally spaced
e_grid_mat(:,d_c) = e_min+(e_max-e_min)*(linspace(0,1,n_e).^space)';
end
%% The code below is the part that I'd like to speed up
tic
e_opt = zeros(n_b,n_d);
for d_c = 1:n_d
% Effort grid depends on d (upper bound changes, but same no. of elements)
e_grid = e_grid_mat(:,d_c); %each column of e_grid_mat is different!
% V(e,b) has dim: (n_e,n_b) and I maximize with respect to the first dimension, e
V = -cost_mat(:,d_c)-delta*prob_mat(:,d_c).*option_nob';
[~,max_ind] = max(V,[],1); %maxind is (1,n_b) vector
e_opt(:,d_c) = e_grid(max_ind);
end %end d
toc
%To check results
disp(mean(mean(e_opt)))
Accepted Answer
More Answers (1)
Dinesh
on 3 Jan 2024
Hi user,
Considering the dependency of "e_grid" on "d", full vectorization of your code may not be feasible. To optimize the code, you can explore MATLAB's parallel processing capabilities, particularly the "parfor" loop, which can execute iterations concurrently on multi-core processors. Ensure you have the Parallel Computing Toolbox installed and initiate a parallel pool before the loop to take advantage of this feature. This approach can significantly reduce the runtime if your system has multiple cores.
Here's a brief example using "parfor":
parpool;
tic
e_opt = zeros(n_b,n_d);
parfor d_c = 1:n_d
e_grid = e_grid_mat(:,d_c);
V = -cost_mat(:,d_c) - delta*prob_mat(:,d_c).*option_nob';
[~, max_ind] = max(V, [], 1);
e_opt(:, d_c) = e_grid(max_ind);
end
toc
In addition to parallelization, profile your code to identify other potential inefficiencies. Utilize MATLAB's "profile on" command to get a detailed report on the runtime of different parts of your script.
1 Comment
Alessandro Maria Marco
on 3 Jan 2024
Categories
Find more on Parallel Computing Toolbox 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!