By default mex file cannot be executed in parallel 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:

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