I'm using parallel computing for the first time (spmd).
After starting the pool and doing some parallel computations, I only have composite variables in my workspace and can't open them. Or when I open them by double clicking they are empty. How can I use the data?
Here is my code:
matlabpool open local 4
spmd
if labindex==1
a = randn(300,1);
end
if labindex==2
b = randn(300,1);
end
if labindex==3
c = randn(300,1);
end
if labindex==4
d = randn(300,1);
end
end
matlabpool close
If you want to generate 4 arrays of size (300,1) on an individual worker each, it is better to do like the following. Note that I have 4 cores on my computer/Matlab pool.
clc
clear
spmd
RandomArray = rand(300,1); % Matlab automatically creates a (300,1) array in each worker.
end
FinalArray = [RandomArray{:}]; % Concatenate everything outside of the spmd block.
whos % Check the results
Name Size Bytes Class Attributes
FinalArray 300x4 9600 double
RandomArray 1x4 1145 Composite
As you see, FinalArray has a size (300,4) as you want. With your code above, it would be quite a pain to put everything together in the 2nd spmd block, because each worker is not aware of what is in the other workers and each variable would be undefined in the workers that did not use them. I don't know the right terminology sorry, but you can read the doc to get a better explanation :)
EDIT:
To answer your comment, here is a simple example. Hopefully this is what you meant :)
clc
clear
% Define different variables.
w = ones(1,10);
x = 1:10;
y = x/2;
z = rand(1,10);
% Use different functions in each worker. Of course you could use the same function with different inputs.
spmd
if labindex==1
a = w;
end
if labindex==2
b = sin(x);
end
if labindex==3
c = y.^2;
end
if labindex==4
d = 4*z;
end
end
% This is the important part
FinalArray = [a{1} ;b{2}; c{3} ;d{4}];
whos
And the output of whos is:
Name Size Bytes Class Attributes
FinalArray 4x10 320 double
a 1x4 497 Composite
b 1x4 497 Composite
c 1x4 497 Composite
d 1x4 497 Composite
w 1x10 80 double
x 1x10 80 double
y 1x10 80 double
z 1x10 80 double