speed up execution time for monte carlo simulation

Hello guys
I am doing a monte carlo simulation and this is the core logic of my simulation. I have to repeat below mentioned code nested loop operation 8 times to get the final plot. So the whole operation its taking around 2 hours to plot. I am getting the result i want and very happy with it but i would be very glad if someone helped me speed up execution time. So I read about par for and when i attempt to use it in my case, its conflicting near this line, something to do with indexing in parfor -
my_vec(:,ji) = diag(V);
Also when i use parfor inside the first for loop, there is no improvement in speed. So I believe parfor has to be applied in the first for loop like this -
parfor
for
% code
end
end
This is my dummy nested for-loop code -
fin_ans = zeros(length(dummy), length(Iteration));
for ij = 1 : length(dummy)
some_vec = dummy(ij);
for ji = 1 : Iteration
H = random(something);
[S, V, D] = svd(H);
my_vec(:,ji) = diag(V);
fin_ans(ij, ji) = myfunc(some_vec, my_vec(:,ji));
end
end
fin_ans1 = mean(fin_ans');
Thanks,

1 commentaire

androidguy
androidguy le 24 Sep 2016
Modifié(e) : androidguy le 24 Sep 2016
edit -
I corrected some of my code and the execution time is approx 4.5 hours now!! But the core part, which is the looping part has no changes in code. So I would really appreciate any help to speed things up. I am also looking up for solutions and I will post it here if i find any..
Thank you,

Connectez-vous pour commenter.

 Réponse acceptée

fin_ans = zeros(length(dummy), length(Iteration));
parfor ij = 1 : length(dummy)
some_vec = dummy(ij);
for ji = 1 : Iteration
H = random(something);
[S, V, D] = svd(H);
my_vec(:,ji) = diag(V);
row_ans(ji) = myfunc(some_vec, my_vec(:,ji));
end
fin_ans(ij, :) = row_ans;
end
fin_ans1 = mean(fin_ans');

5 commentaires

Thank you for the reply sir. I have tried what you said just now and I still get an error at the same point -
Error: The variable my_vec in a parfor cannot be classified.
Do you ever need my_vec again after the calculation? If so then the content is not well defined for it, because it gets entirely replaced for each value of ij. If you do not need it again then do not bother to store the whole thing:
fin_ans = zeros(length(dummy), length(Iteration));
parfor ij = 1 : length(dummy)
some_vec = dummy(ij);
for ji = 1 : Iteration
H = random(something);
[S, V, D] = svd(H);
my_vec = diag(V);
row_ans(ji) = myfunc(some_vec, my_vec);
end
fin_ans(ij, :) = row_ans;
end
fin_ans1 = mean(fin_ans');
If you do need it then you would need to store the complete content for all ij and all iterations. I am not going to write that out at the moment because I think you do not need it.
androidguy
androidguy le 24 Sep 2016
Modifié(e) : androidguy le 24 Sep 2016
Yeah I tried that before by clearing it at the end of every iteration as it is not important to store it. But I still get the same error. Also I am trying to slice the variable my_vec as follows and get an error like this -
parfor ij = 1 : length(dummy)
some_vec = dummy(ij);
for ji = 1 : Iteration
H = random(something);
[S, V, D] = svd(H);
my_vec{ij}(:,ji) = diag(V);
fin_ans{ij}(ij, ji) = my_func(some_vec, my_vec{ij}(:,ji));
end
end
final_ans = mean(fin_ans');
And this is the error I get -
Analyzing and transferring files to the workers ...done.
An UndefinedFunction error was thrown on the
workers for 'my_vec'. This might be because
the file containing 'my_vec' is not accessible
on the workers. Use addAttachedFiles(pool,
files) to specify the required files to be
attached. See the documentation for
'parallel.Pool/addAttachedFiles' for more
details.
Error in my_file (line 514)
parfor ij = 1 : length(dummy)
Caused by:
Undefined function 'my_vec' for input
arguments of type 'double'.
fin_ans = zeros(length(dummy), length(Iteration));
parfor ij = 1 : length(dummy)
some_vec = dummy(ij);
row_ans = zeros(1,Iteration);
for ji = 1 : Iteration
H = random(something);
[S, V, D] = svd(H);
my_vec = diag(V);
row_ans(ji) = myfunc(some_vec, my_vec);
end
fin_ans(ij, :) = row_ans;
end
fin_ans1 = mean(fin_ans');
The code is running without any errors. The execution time was 15 mins without using parfor. Now its 4.9934424 mins!!!
Thank you so much for your help!! I really do appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by