Usage of parfor (parallel computing)
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
I have a question regarding parallel computing with MATLAB. Suppose I have the following code:
----
trials = 30;
data = zeros(trials,11,3);
for k=1:trials
count = 1;
for i=0:1:10
b = simulate_network_inhibitory_testing(i/10);
data(k,count,: ) = b;
count = count + 1;
end
end
save('data2301performance.mat', 'data');
---
The called function 'simulate_network_inhibitory_testing' returns a 3x1 Matrix.
I now want to parallelize this script using parfor. However, if I just substitute the first for loop by a parfor loop this does not work, because MATLAB says I am not allowed to use the data matrix in that way.
What is the problem with this and how would a work-around look like?
3 Kommentare
Walter Roberson
am 23 Jan. 2011
Does simulate_network_inhibitory_testing return a value involving random seeds? If not then there is no need to do repeated trials.
Remove your count loop and replace its value in the loop with i+1
data(k,i+1,:) = b;
You would have better efficiency if you moved the trial number to the last dimension and the 3 outputs of b to the first:
data(:, i+1, trial) = b;
This would allow the array to be split along complete panes.
Stefan Depeweg
am 23 Jan. 2011
Walter Roberson
am 23 Jan. 2011
Use a local array to hold all the 11 x 3 results calculated in the loop, and then after the end of the inner loop, write the block in, so that the only index named in data() in the outer loop is k and the other two are : .
Antworten (1)
Edric Ellis
am 31 Jan. 2011
Walter is quite right to suggest the local array approach. Just to expand what he described:
trials = 30;
data = zeros(trials,11,3);
parfor k=1:trials
datak = zeros(11,3);
for i=0:1:10
b = simulate_network_inhibitory_testing(i/10);
datak(i+1, :) = b;
end
data(k,:,:) = datak;
end
1 Kommentar
Walter Roberson
am 31 Jan. 2011
Amazing what one can pick up by reading random cssm answers for products one doesn't even use ;-)
Diese Frage ist geschlossen.
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!