Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPMD vs. Parfor

I'm new about parallel computing in matlab. I have a function which creates a classifiers (SVM) and I'd like to test it with several dataset. I've got a 2 core workstation so I'd like to run test in parallel. Can someone explain me the difference between:

 dataset_array={dataset1, dataset2}
 matlabpool open 2
 spmd
      my_function(dataset(labindex));
 end

and

 dataset_array={dataset1, dataset2}
 matlabpool open 2
 parfor i:1=2
      my_function(dataset(i));
 end
like image 273
Titus Pullo Avatar asked Sep 12 '12 09:09

Titus Pullo


1 Answers

spmd is a parallel region, while parfor is a parallel for loop. The difference is that in spmd region you have a much larger flexibility when it comes to the tasks you can perform in parallel. You can write a for loop, you can operate on distributed arrays and vectors. You can program an entire work flow, which in general consists of more than loops. This comes at a price: you need to know more about distributing the work and the data among your threads. Parallelizing the loop for example requires explicitly dividing the loop index ranges amongst the workers (which you did in your code by using labindex), and maybe creating distributed arrays.

parfor on the other hand only does this - a parallelized for loop. Automatically parallelized, you can add, so the work is divided between the workers by MATLAB.

If you only want to run a single loop in parallel and later work on the result on your local client, you should use parfor. If you want to parallelize your entire MATLAB program, you will have to deal with the complexities of spmd and work distribution.

like image 156
angainor Avatar answered Sep 30 '22 13:09

angainor