I have figured out some awesome ways of speeding up my MATLAB code: vectorizing, arrayfun, and basically just getting rid of for loops (not using parfor). I want to take it to the next step.
Suppose I have 2 function calls that are computationally intensive.
x = fun(a);
y = fun(b);
They are completely independent, and I want to run them in parallel rather than serially. I dont have the parallel processing toolbox. Any help is appreciated.
thanks
The MATLAB interpreter is single-threaded, so the only way to achieve parallelism across MATLAB functions is to run multiple instances of MATLAB. Parallel Computing Toolbox does this for you, and gives you a convenient interface in the form of PARFOR/SPMD/PARFEVAL etc. You can run multiple MATLAB instances manually, but you'll probably need to do a fair bit of work to organise the work that you want to be done.
If I am optimistic I think you ask "How Can I simply do parallel processing in Matlab". In that case the answer would be:
Parallel processing can most easily be done with the parallel computing toolbox. This gives you access to things like
parfor
.I guess you can do:
Of course there are other ways, but that should be the simplest.
The usual examples involve
parfor
, which is probably the easiest way to get parallelism out of MATLAB's Parallel Computing Toolbox (PCT). Theparfeval
function is quite easy, as demonstrated in this other post. A less frequently discussed functionality of the PCT is the system ofjob
s andtask
s, which are probably the most appropriate solution for your simple case of two completely independent function calls. Spoiler: thebatch
command can help to simplify creation of simple jobs (see bottom of this post).Unfortunately, it is not as straightforward to implement; for the sake of completeness, here's an example:
The key here is the function
myCoarseFunction
given tocreateTask
as the function to evaluate in the task objects to creates. This can be yourfun
or a wrapper if you have complicated inputs/outputs that might require a struct container.Note that for a single task, the entire workflow above of creating a job and task, then starting them with
submit
can be simplified withbatch
as follows:Also, keep in mind that as with
matlabpool
(now calledparpool
), usingparcluster
requires time to startup the MATLAB.exe processes that will run your job.