The spmd command is not as easy to implement as parfor but it offers the programmer more control over the code. In comparison to the parfor loop we can highlight that spmd lets us:
- Define which tasks are assigned to each individual worker.
- Control how data is shared between the workers.
- See the work of each individual worker.
- Determine which loop iterations are assigned to each individual worker.
- Assign not only different data but also different tasks to different workers.
- Work with distributed arrays.
NOTE: You can also test this function by opening the parallel command window. In this interface you can see how the commands run in each worker. You can access it by typing pmode start (you can't have any Matlab pool open).Another useful function is numlabs, which returns the number of workers available.
Let's try exploring the spmd command then. Any spmd block has the following syntax:
The spmd only tells matlab that each lab will run the code inside the block concurrently and independently, without communicating or sharing results with other workers. There are ways for the labs to communicate with each other, but the programmer must explicitly say that in his code. I'll cover it later.
So how do we run a loop? If we write a for-loop inside it we'll verify that each worker will run it identically.
NOTE: If you took the time to run this code you probably noticed that a composite object appeared in your workspace. Don't worry about it right now. I'll talk about them later.This is not what we desire. We want each lab working on different iterations of the loop to cut down the time of execution to the minimum. The easiest way to do this is by writing conditions, taking advantage of either the switch/case or the if/elseif function. Below I wrote some random code using switch. The use of switch instead of if is a matter of preference. The performance is identical.
As you can see we have total control over which iterations are assigned to each lab.
We can also write a for loop in a more compact manner in the following way:
This way each lab will be assigned the iterations associated with labindex, labindex + numlabs, ... Since labindex is different for each worker and the step is equal every lab will take care of a different set of iterations.
I think the basics are covered. In my next post I'll write about the objects returned after an spmd execution (composite) and how to return data from them. I'll also write some examples on how to implement spmd.
Cheers.
Hey,
ReplyDeletespmd(2)
will allow me to access two workers. but I want to use only second worker for computation and want to bypass 1st worker because something else is already going on first worker. So even if I use if labindex==2, then also my worker 1 is atleast engaged in spmd loop. Can you suggest how can I break SPMD for worker 1 but want to continue for worker 2!
give me a sample code on how to use labindex and numlabs in for loop, so that each iteration is assigned to different workers!
ReplyDelete