MATLAB Answers

Parallel computing for images processing

20 views (last 30 days)
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];
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;
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.


Walter Roberson
Walter Roberson on 16 Aug 2018
Please learn to use fullfile() instead of concatenating together parts of filenames.
Walter Roberson
Walter Roberson on 17 Aug 2018
When the states only ever change in plain increments with wrapping back to the beginning after a fixed number, then it is hardly worth using a Finite State Machine approach.
Oh, there might be some point in doing a FSM-ish approach if you were doing pipelined HDL, or were doing real-time work in which the work was pretty well balanced between states. Might even be a point if you were using cooperative multitasking in which you had to deliberately give up control to give the CPU a chance to service mouse interactions or whatever. But not in this situation.

Sign in to comment.

Accepted Answer

Edric Ellis
Edric Ellis on 17 Aug 2018
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.


Show 2 older comments
Edric Ellis
Edric Ellis on 20 Aug 2018
There are a couple of ways you could use spmd to do pretty much the same thing - but I don't think either of them would be as efficient as the parfor loop.
Firstly, you could fix up the approach you were trying to make with a pipeline. This is likely to be inefficient because you can only keep as many workers busy as you have stages in your pipeline, and also you're doing a lot of unnecessary communication to shuffle the images down the pipeline, and you will end up introducing a synchronisation point to do the communication, and this will mean that all stages will take as long as the slowest stage. The right way to perform the communication is to have all labs call labSendReceive to shuffle the data down the pipeline.
Secondly, you could use the "for-drange" loop idiom. This is effectively the same as a parfor loop, but operates inside spmd. It often turns out to be more effort to write the loop than the equivalent parfor loop, and it can be less efficient because it uses a fixed work partitioning rather than the parfor dynamic partitioning.
In short - if you can use parfor to get your job done, use that - it will almost always beat spmd.
shital shinde
shital shinde on 13 Feb 2020
actually I also try to work with parallelization. I am currently working for digital image processing. Anybody please help me to parallelize the code. I attached the filefor watermarking that i want to make parallel in matlab. please help me for code
Walter Roberson
Walter Roberson on 13 Feb 2020
When you posted your code in another question I replied back showing you exactly which for to change to parfor

Sign in to comment.

More Answers (1)

shital shinde
shital shinde on 15 Feb 2020
will you please tell me how the above code is work with parfor loop.


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by