Friday, February 15, 2013

Retrieving data from composite objects

After you execute some code inside an spmd block you're left with a composite object. Example:
spmd
    y = zeros(1,100);
    for n = labindex:numlabs:100
        y(n) = n;
    end
end
If you run this code you'll be left with two variables in the workspace, y and n. Let's focus on the variable y:
>> whos y
  Name      Size            Bytes  Class      
  y         1x4              1145  Composite
 
As you can see we're dealing with a composite object. And what is that? A composite object is pretty similar to a cell array. It has as many elements as the number of labs and each element corresponds to the part of the variable y that is stored in the respective element.
>> y
y =
   Lab 1: class = double, size = [1  97]
   Lab 2: class = double, size = [1  98]
   Lab 3: class = double, size = [1  99]
   Lab 4: class = double, size = [1  100]
You can manipulate a composite object in the same way you deal with a cell array. Running the command y{1} will return the vector y as stored in the first lab.
y{1}
ans =
  Columns 1 through 21
     1     0     0     0     5     0     0     0     9 ...
In our case each lab stores a different portion of the variable. This could not be the case. Various labs can store the same portion of the variable, or no part of it at all.
If you want to know in which workers the variable exists just run the function:
>> exist(y)
ans =
     1     1     1     1
The function exist returns 1 when the variable exists in the respective lab, and 0 otherwise.

There's not much left to say. If you are familiar with cell arrays then then I'm sure you haven't learned anything new. If you didn't then I hope this was enough to get you started.

Cheers. 

No comments:

Post a Comment