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
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
2011년 1월 23일
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
2011년 1월 31일
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
2011년 1월 31일
Amazing what one can pick up by reading random cssm answers for products one doesn't even use ;-)
이 질문은 마감되었습니다.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!