Usage of parfor (parallel computing)

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 comentarios

Walter Roberson
Walter Roberson el 23 de En. de 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
Stefan Depeweg el 23 de En. de 2011
Hello,
thanks for your quick answer!
1. Yes, each network simulation involves random seeds, the trials are neccessary.
2. Just saw that the count variable is useless, thanks :)
3. However, even if I switch the third and the last dimension, parfor still does not work. "parfor loop cannot run due to the way variable 'data' is used"
Walter Roberson
Walter Roberson el 23 de En. de 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 : .

Respuestas (1)

Edric Ellis
Edric Ellis el 31 de En. de 2011

4 votos

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 comentario

Walter Roberson
Walter Roberson el 31 de En. de 2011
Amazing what one can pick up by reading random cssm answers for products one doesn't even use ;-)

La pregunta está cerrada.

Etiquetas

Preguntada:

el 23 de En. de 2011

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by