Parallel Computing (Job and task)

2 views (last 30 days)
hi i try to run matlab code that runs parallel . my problem is .i have 8 for loops and i want these loops to run at the same time because they are independent .any way to do that.my code is
a=imread('x.jpg');
[xx,yy]=size(a);
while x-1>0 && y-1>0
r=mean-a(x-1,y-1);
x=x-1;
y=y-1;
end
%north
while x-1>0 && y<yy
r=mean-a(x-1,y+1);
x=x-1;
end
%north west
while x-1>0 && y+1<yy
r=mean-a(x-1,y+2);
x=x-1;
y=y+1;
end
%west
while x>0 && y+3<yy
r=mean-a(x+1,y+3);
y=y+1;
end
%north west
while x+3<xx && y+3<yy
r=mean-a(x+3,y+3);
x=x+1;
y=y+1;
end
%norht
while x-1>0 && y-1>0
r=mean-a(x+3,y+1);
x=x-1;
y=y-1;
end
%north east
while x+3<xx && y-1>0
r=mean-a(x+3,y-1);
x=x+1;
y=y-1;
end
%eset
while x>0 && y-1>0
r=mean-a(x+1,y-1);
y=y-1;
end

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2012
Those loops are not independent: the initial x and y values for any one of the loops depends upon the outcome of the previous loops.
If you want to execute different code per worker, then my understanding is that you would use smpd and use the labindex to switch() to the work to be done. (Or use an array of function handles and index in that by the labindex, I suppose.)
Looking at your code structure, it seems to me likely that the amount of work done would be unbalanced between the workers, with some doing little or no work. There is a high overhead cost to starting a worker, so it is best if each worker does sufficient work that the startup overhead is relatively negligible.
I cannot tell at the moment what your code is intended to do. Please do not use "mean" as a variable name, as it conflicts with the name of the important MATLAB routine mean()
  5 Comments
Walter Roberson
Walter Roberson on 29 Feb 2012
See Example 3
http://www.mathworks.com/help/techdoc/ref/function_handle.html
Isee You
Isee You on 29 Feb 2012
can you see my code and tell me your opinion?

Sign in to comment.

More Answers (1)

Isee You
Isee You on 29 Feb 2012
i try this code , do you think it true??
function do_test(avg,x,y)
matlabpool open
a=imread('x.jpg');
% [xx,yy]=size(a);
c = {@northest, @north, @northwest,@west, @southwest, @south,@southeset,@eset};
parfor f=1:8
c{f}(avg,a,x,y) ;
end
function northest(avg,a,x,y)
%north eset
while x-1>0 && y-1>0
r=avg-a(x-1,y-1);
x=x-1;
y=y-1;
end
function north(avg,a,x,y)
[xx,yy]=size(a);
%north
while x-1>0 && y<yy
r=avg-a(x-1,y+1);
x=x-1;
end
function northwest(avg,a,x,y)
[xx,yy]=size(a);
%north west
while x-1>0 && y+1<yy
r=avg-a(x-1,y+2);
x=x-1;
y=y+1;
end
function west(avg,a,x,y)
[xx,yy]=size(a);
%west
while x>0 && y+3<yy
r=avg-a(x+1,y+3);
y=y+1;
end
function southwest(avg,a,x,y)
[xx,yy]=size(a);
%south west
while x+3<xx && y+3<yy
r=avg-a(x+3,y+3);
x=x+1;
y=y+1;
end
function south(avg,a,x,y)
[xx,yy]=size(a);
% south
while x-1>0 && y-1>0
r=avg-a(x+3,y+1);
x=x-1;
y=y-1;
end
function southeset(avg,a,x,y)
[xx,yy]=size(a);
%south east
while x+3<xx && y-1>0
r=avg-a(x+3,y-1);
x=x+1;
y=y-1;
end
function eset(avg,a,x,y)
[xx,yy]=size(a);
%eset
while x>0 && y-1>0
r=avg-a(x+1,y-1);
y=y-1;
end
  2 Comments
Walter Roberson
Walter Roberson on 29 Feb 2012
Well, that code will do something in parallel.
It won't do anything _useful_ as you do not return or store any values in the functions.
It will also not do the equivalent of the serialized code, as in the serialized code, the input of each stage is the output of the previous stage.
But in terms of setting up parallelism... yes, those routines should get executed in parallel.
Isee You
Isee You on 29 Feb 2012
thank u "Walter Roberson".
my code is a step in many steps .

Sign in to comment.

Categories

Find more on MATLAB Parallel Server in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!