Take the 2-minute tour ×

I'm processing data in parallel using parfor in this way:

iteration = 10;
result = zeros(1, iteration);

matlabpool open local 2
    parfor i = 1:iteration
        data = generate_data();
        result(i) = process_data(data);
matlabpool close

It works fine, but I have one problem. My function generate_data generates unique data (i.e. 0, 1, 2, 3, 4 ...) but in practice sometimes I give same value two times (and I give 0, 1, 1, 2, 3, 4, 4, 5, ...). In simple my function looks like this:

function data = generate_data()

persistent counter generated_data;

    counter = 1;
    generated_data = [0 1 2 3 4 5 6 7 8 9]; 

data = generated_data(counter);
counter = counter + 1;

How can I fix this ?

share|improve this question
add comment

2 Answers

If I've understood correctly, you want to ensure that your generate_data doesn't return the same value to two iterations of your PARFOR loop. Unfortunately, you cannot do this directly in the PARFOR loop, since no communication is allowed. Your options are basically: either call generate_data on the MATLAB client; or run two PARFOR loops perhaps like this:

parfor ii = 1:iteration
  generated(ii) = generate_data();

% omit duplicated values - perhaps you might wish to generate
% some more here too...
generated = unique(generated);

parfor ii=1:numel(generated)
  result(ii) = process_data(generated(ii));
share|improve this answer
But I can't generate whole data set at once. This is example. In real I'm generating large sets of data. Ideally, the operations performed in generate_data were treated as a single atomic instruction. –  veeveeoor Jan 25 '13 at 10:16
add comment
up vote 0 down vote accepted

I tried this:

function data = generate_data(id)

persistent generated_data;

data = generated_data(mod(id - 1, length(generated_data)) + 1);

In each call I can generate data set on demand using id. I have to remember that one data set can be reached only for one id. It works, but not solve my problem. I would eliminate parametr id and use internal counter like in first post.

share|improve this answer
add comment

Your Answer


