How to nest multiple parfor loops

后端 未结 3 1440
广开言路
广开言路 2020-12-03 03:53

parfor is a convenient way to distribute independent iterations of intensive computations among several \"workers\". One meaningful restriction is that pa

3条回答
  •  再見小時候
    2020-12-03 04:34

    You should be able to do this with bsxfun. I believe that bsxfun will parallelise code where possible (see here for more information), in which case you should be able to do the following:

    bsxfun(@somefun,(1:6)',1:6);
    

    You would probably want to benchmark this though.

    Alternatively, you could do something like the following:

    function parfor_allpairs(fun, num_rows, num_cols)
    
    parfor i=1:(num_rows*num_cols)
        fun(mod(i-1,num_rows)+1,floor(i/num_cols)+1);
    end
    

    then call with:

    parfor_allpairs(@somefun,6,6);
    

提交回复
热议问题