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

Walter Roberson
Walter Roberson 2011년 1월 23일
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 2011년 1월 23일
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 2011년 1월 23일
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 : .

답변 (1개)

Edric Ellis
Edric Ellis 2011년 1월 31일

4 개 추천

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

Walter Roberson
Walter Roberson 2011년 1월 31일
Amazing what one can pick up by reading random cssm answers for products one doesn't even use ;-)

이 질문은 마감되었습니다.

질문:

2011년 1월 23일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by