MATLAB: Parallel computing for images processing

image processingImage Processing Toolboxparallel computingParallel Computing Toolboxspmd

Good morning everyone, I've recently embarked on using the parallel computing toolbox and one problem I'd like to solve concerns a very basic scenario related to images processing. Suppose you have a directory where N different images, which can be distinguished by their name that is in the form of "imgX.jpg" where X is an increasing index, are stored. I want to create a pool of M workers that ought to perform some predefined operations over these images. In particular, the essential tasks that these workers are requested to accomplish are: 1. importing the i-th image 2. creating a filter by using the "fspecial" function 3. filtering the i-th image by employing the filter created at step 2 4. saving the processed image Of course, the list of the tasks may be further extended in the future, depending on what kind of operations are required. I wrote some code from scratch trying to employ a FSM-like structure and fit in with the SPMD model:
workers = Open_Pool(profile, numWorkers);
state = 1;
directory = 'Immagini_Esercizio2/';
list = dir([directory '*.jpg']);
images = cell(1, length(list));
indexImage = 1;
for i = 1 : length(images), images{i} = [directory, list(i).name];
end
spmd
pre = mod(labindex - 2 + numlabs, numlabs) + 1;
post = mod(labindex, numlabs) + 1;
while(indexImage <= length(images))
% fsm update
switch state
case 1
I = imread(images{indexImage});
labSend(I, post, 1);
state = 2;
case 2
H = fspecial('laplacian');
I = labReceive(pre, 1);
labSend(post, I, 1);
labSend(post, H, 2);
state = 3;
case 3
I = labReceive(pre, 1);
H = labReceive(pre, 2);
Out = imfilter(I, H);
results = Out
labSend(post, Out, 3);
state = 4;
case 4
Out = labReceive(pre, 3);
filename = sprintf('risultato%d.jpg', indexImage);
imwrite(Out, filename);
state = 1;
indexImage = indexImage + 1;
end
end
end
In the previous code, "Open_Pool" is a function I created to manage pools of workers (essentially, it allows the creation of M workers by using a specified profile). However, not surprisingly the execution of this code leads to deadlocks because I can't figure out how to enable a proper communication among all the workers. Instead, all the aforementioned steps must be executed orderly in a chain-like fashion.

Best Answer

Rather than using labSend and labReceive inside an spmd block, I would suggest simply re-writing this as a parfor loop where each iteration of the loop loads one file, processes it, and then writes out the result.