Parallel computation of several centroids on a single frame

1 view (last 30 days)
Hello everyone,
There is a 40x40 (or any other size) matrix M. There is a centroid inside each 10x10 pixels area, so 16 centroids total. How to compute these centroids in parallel?
Here are some thoughts. Computing these centroids without using parallel approach is straightforward:
M = rand(40, 40);
centroids = NaN(2, 4, 4); % [ (x,y), row, col]
for rowIndex = 1:1:4
for colIndex = 1:1:4
limRow = (rowIndex-1)*10+1 : (rowIndex-1)*10+10;
limCol = (colIndex-1)*10+1 : (colIndex-1)*10+10;
centroids(:, rowIndex, colIndex) = fCentroid( M(limRow, limCol) ); % fCentroid returns (X,Y)- center of gravity for subset of matrix M(limRow, limCol)
end
end
If I would like to make the nested loop parallel, Matlab tells me that M is a broadcast variable and I have to break down it into individual columns:
M1 = M(1:10, :);
M2 = M(11:20, :);
...
M4 = M(31:40, :);
and now the nested loop should look like this:
centroids1 = NaN(2, 4);
centroids2 = same
centroids3 = same
centroids4 = same
parfor colIndex = 1:1:4
limCol = (colIndex-1)*10+1 : (colIndex-1)*10+10;
centroids1(:, colIndex) = fCentroid( M1( limCol) ); % fCentroid returns X;Y center of gravity for subset of matrix M(limRow, limCol)
centroids2() = bla=bla-bla
centroids3() = bla-bla
bla-bla
end
As one can see, this is definitly not an ideal solution. Can someone please suggest a way of improving it. In the case of 16 centroids, one may try this approach, but I actually have 33x33 centroids, which is really a pain.

Answers (1)

Prasad Mendu
Prasad Mendu on 18 Oct 2016
Using "parfor" construct will let you run code on different workers in parallel, but there is no communication between these workers and workers does not have access to other worker's data. Instead, you can consider using "spmd" construct along with distributed arrays which allows different workers to communicate between each other and share the data. Refer to the links below to explore more on these topics:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!