0

I need to run a for-loop in batches of 1000. But if 'cc' doesn't reach 100 during the first run, I need to re-run more batches of 1000 until cc reaches 100.

I know that this can be done using a while-loop, however I need to use the parallel toolbox parfor (quite possibly GPU's as well), and as far as I know it doesn't support while-loops.

Essentially I need to change (reset) the for-loop index inside the if-loop. However, apparently MATLAB's for-loop is designed so that you cannot change its index, in this case, 'i', within the loop. Is there a way around this? There must be a smarter way to do this without using a while loop. Any suggestions?

for i=1:1000

    if (abs(i) <= gamma)
        % etc..
        cc = cc + 1;
    end

    if cc < 100
        i = 1;
    end

end
7
  • 1
    If you're going to run this in parallel, how does it make sense to change how many parallel threads you execute once they've already started? The whole point of parallel execution is that each of your threads shouldn't depend on the others. Maybe you should use a while-loop to get cc >= 100 first, and then do the rest in parallel afterwards? Commented Mar 20, 2014 at 14:06
  • @Isaac This is the problem, because 'cc' is generated inside the for-loop. I can check if cc < 100 outside the for-loop, but if it isn't, I need it to repeat the same for-loop again until 'cc' reaches 100. Commented Mar 20, 2014 at 14:11
  • Right, so do that, and then end the loop. Then start another loop where you don't need to check. Commented Mar 20, 2014 at 14:14
  • When you say you want to use the parallel toolbox, do you mean you want to turn this for loop in to a parfor loop so it can be run across multiple workers? Commented Mar 20, 2014 at 14:17
  • @AndrewJanke Yes, and hopefully extend it to GPU's later but as long as it works with parfor thats fine for now. Commented Mar 20, 2014 at 14:19

1 Answer 1

2

What you can do is run a whole batch, accumulate each pass's independent cc indicator in an array, and then combine the results outside the parfor to see if you need to continue. This keeps each pass of the loop independent of the results of other passes, so it can be done in parallel.

n = 1000;
cc = 0;
isDone = 1;
while ~isDone
    ccs = zeros(1, n);

    parfor i=1:n
        if (abs(i) <= gamma)
            % ...etc..
            ccs(i) = 1;
        end
    end

    cc = cc + sum(ccs);

    isDone = cc >= 100;
end

This will waste some work. You can adjust n to change the batch size and control the tradeoff between wasted work (due to "fragmentation" in the last batch where cc would have made it to 100 with a smaller batch) vs. the overhead of parallelization.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.