How to build a table inside a parfor loop
14 views (last 30 days)
Show older comments
I am trying to build a lookup table inside a for loop. A minimal example of what I would like to do is:
% I start with a lookup table, first column is input, second is output (2*input)
table= [ 1 2
3 6
5 10
2 4];
n = 30; % number of input values
inputs=ceil(unifrnd(0,100,n,1)); %inputs are random numbers from 1 to 100
doubles=zeros(n,1); % doubles collects the outputs... that are doubles
parfor ii=1:n
comparison= table(:,1)==inputs(ii); % check if already computed:
if any(comparison) % if already computed
doubles(ii)=table(comparison,2); % then, retrieve it from table;
else % if never computed before
twice=2*inputs(ii); % then, compute it ...
doubles(ii)=twice; % ... use it ...
table=[ table
inputs(ii) twice]; % ... and store it in the table.
end
end
Unfortunately, this doesn't work because table is seen as a temporary variable by the parfor loop. How can I solve this? Note, I cannot preallocate the vector doubles, because in the real problem n=1e40.
Thank you, Paolo
0 Comments
Answers (1)
Edric Ellis
on 11 Mar 2011
PARFOR is designed for "embarassingly parallel" problems where each iteration of the loop is independent. Unfortunately, the problem here is that you are updating the variable "table" in the last line of the loop. This means that the iterations are not independent, because the value of "table(:,1)" can change as the loop progresses.
I can't immediately see how to refactor this algorithm. You may need to use SPMD to allow communication between the loop iterates to implement your algorithm.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!