Parallel mex file in Matlab

By default mex file cannot be parallel executed in Matlab, which will be a major bottleneck for Matlab program. Fortunately, there is a way to execute loop operations in parallel -- parfor. Matlab provides a good tutorial on parfor: Getting Started with parfor.

One tricky problem with parfor is dealing with reduction assignments, which means the value of some variables are updated by each iteration, such as assignment to an array/matrix indexed by loop index.  My suggestion is to separate the loop into two parts: one part deal with non-reduction assignment with parfor, and the other part do the reduction assignment with traditional for loop.

If you cannot save all the parfor-assigned variables in memory, then you must save them into mat file and load them in the next for-loop. In the parfor-loop, command "save" cannot  be used directly because it violates the transparency(God knows what does it mean!). As an alternative, you can create a wrapper function of save and call that function instead. FOr example:

Save the following as "parsave.m":

function parsave(fname, x,y)
    save(fname, 'x', 'y')
end


Then run it with:

parfor ii = 1:4
    x = rand(10,10);
    y = ones(1,3);
    parsave(sprintf('output%d.mat', ii), x, y);
end


References:

  1. parfor manual: http://www.mathworks.com/help/distcomp/parfor.html
  2. Getting Started with parfor: http://www.mathworks.com/help/distcomp/getting-started-with-parfor.html
  3. How do I use SAVE with a PARFOR loop: http://www.mathworks.com/support/solutions/en/data/1-D8103H/?product=DM&solution=1-D8103H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值