1

Is it possible to force the parfor iteration numbers in MATLAB? For example, if the loop is i=1:1000, then is it possible to assign it in the following manner:

i=1:250 ---> core 1

i=251:500 ---> core 2

i=501:750 ---> core 3

i=751:1000 ---> core 4

4
  • I don't think so, but more importantly: why would you want to do this? MATLAB's pretty good at distributing computational effort. I wouldn't be surprised if it used an asymmetric distribution in cases where the iterations vary with respect to computational effort (though I've never checked the docs to find out if this was indeed the case) Nov 11, 2015 at 0:59
  • 2
    You can't assign to specific cores, but if you want the cores to process 4 chunks of your problem, you could do parfor j=1:4;for i=(1+250*(j-1)):(250*j);... which ought to work, but as Andras says, I can't think why this would be a good idea.
    – David
    Nov 11, 2015 at 1:00
  • This may seem naive, but is vital: is it guaranteed that with j=1:4, all the four different j-computations will go to 4 different cores? Nov 11, 2015 at 2:30
  • Sorry, just to rephrase: how to assure that all the four j-iterations go to 4 different cores - and better still, to know which core has got which j-value computation (e.g. core 3 has j=1; core 1 has j=2 etc). This is assuming that at this point parfor cannot be forced into j=1-->core1, j==2-->core2 etc. Nov 11, 2015 at 5:23

1 Answer 1

1

You can't do this with parfor, but you can do something basically equivalent with either with spmd or parfeval.

In parfeval:

for fidx = 1:4
    range = (((fidx - 1)*250)+1):(fidx*250)
    f(fidx) = parfeval(@bodyFcn, 1, range);
end

In spmd:

spmd
    for i = drange(1, 1000)
        ...
    end
end

See http://www.mathworks.com/help/distcomp/using-a-for-loop-over-a-distributed-range-for-drange.html for documentation about drange.

2
  • Tried both. But cannot force assign 4 blocks of codes to four individual cores. Nov 19, 2015 at 3:06
  • I don't understand - the code above divides up the work into 4 so that different workers operate on the body. (Well, I suppose the parfeval version doesn't guarantee that different workers operate on different evaluations, but the spmd version does). What happens when you try?
    – Edric
    Nov 19, 2015 at 14:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.