need to kill worker process in parfor loop

  • Follow


Ok, so i have code roughly as follows form this pseudo code:

XYZI = [];

parfor i=1:iter
       %bunch of code to set up linear system
       I = lsqnonneg(A,b);
       XYZI = [XYZI;[X Y Z I]];
end

the code works, but once in every 20 or so runs, lsqnonneg hangs and takes 10 hours until it complains saying it reached max iterations. When it doesnt hang, it takes roughly 2-3 minutes. I dont want to increase the tolerance because i want to keep it at what its set to because the other 95% of runs finish fast.

Is there any way that i can just stop the worker from doing that iteration and have it go on to the next one? My goal is to have it constantly grab the cpu time and if the different form start to present is grater than 5 minutes i want to have that worker stop the iteration and continue with the next.

If iter is set to 50, its ok if i only get 48 iterations out of the code. 

Thanks,

Adnan
0
Reply Adnan 5/10/2010 7:12:07 PM

Adnan Abdulally wrote:
> Ok, so i have code roughly as follows form this pseudo code:
> 
> XYZI = [];
> 
> parfor i=1:iter
>       %bunch of code to set up linear system
>       I = lsqnonneg(A,b);
>       XYZI = [XYZI;[X Y Z I]];
> end
> 
> the code works, but once in every 20 or so runs, lsqnonneg hangs and 
> takes 10 hours until it complains saying it reached max iterations. When 
> it doesnt hang, it takes roughly 2-3 minutes. I dont want to increase 
> the tolerance because i want to keep it at what its set to because the 
> other 95% of runs finish fast.
> 
> Is there any way that i can just stop the worker from doing that 
> iteration and have it go on to the next one?

Perhaps if you set a timer (which would probably have to be an elapsed time 
timer rather than a CPU time timer), and in the timer callback, error() to 
throw an error, and in the parfor loop, try/catch to catch the timeout ?

On the other hand, I don't know if it is possible to set a per-worker timer.
0
Reply Walter 5/10/2010 7:48:35 PM


"Adnan Abdulally" <adnan.abdulally@tsc.com> wrote in message <hs9lq7$8ag$1@fred.mathworks.com>...
> Ok, so i have code roughly as follows form this pseudo code:
> 
> XYZI = [];
> 
> parfor i=1:iter
>        %bunch of code to set up linear system
>        I = lsqnonneg(A,b);
>        XYZI = [XYZI;[X Y Z I]];
> end
> 
> the code works, but once in every 20 or so runs, lsqnonneg hangs and takes 10 hours until it complains saying it reached max iterations. When it doesnt hang, it takes roughly 2-3 minutes. I dont want to increase the tolerance because i want to keep it at what its set to because the other 95% of runs finish fast.
> 
> Is there any way that i can just stop the worker from doing that iteration and have it go on to the next one? My goal is to have it constantly grab the cpu time and if the different form start to present is grater than 5 minutes i want to have that worker stop the iteration and continue with the next.
> 
> If iter is set to 50, its ok if i only get 48 iterations out of the code. 
> 
> Thanks,
> 
> 

Adnan:

I do not think there is a graceful way to stop lsqnonneg based on cpu time, but you can certainly limit the number of (internal) iterations done in any call to it.

[X, RESNORM, RESIDUAL, EXITFLAG] = LSQNONNEG(A, b, X0, OPTIONS)

use OPTIMSET to specify the MaxIter field in the OPTIONS structure. With recent versions of Matlab you can replace X0 with ~ (if you don't have a reasonable initial estimate).

You probably  want to test EXITFLAG - if it's zero the MaxIter limit was exceeded and you may want to set I to something to so indicate (e.g. NaN's or zeros)

emc
0
Reply Gene 5/10/2010 8:03:06 PM

"Gene " <ecliff@vt.edu> wrote in message <hs9opq$qqs$1@fred.mathworks.com>...

> 
> Adnan:
> 
> I do not think there is a graceful way to stop lsqnonneg based on cpu time, but you can certainly limit the number of (internal) iterations done in any call to it.
> 
> [X, RESNORM, RESIDUAL, EXITFLAG] = LSQNONNEG(A, b, X0, OPTIONS)
> 
> use OPTIMSET to specify the MaxIter field in the OPTIONS structure. With recent versions of Matlab you can replace X0 with ~ (if you don't have a reasonable initial estimate).
> 
> You probably  want to test EXITFLAG - if it's zero the MaxIter limit was exceeded and you may want to set I to something to so indicate (e.g. NaN's or zeros)
> 
> emc

Gene,

I have looked into optimset earlier. Unfortunately the MaxIter value has no effect on lsqnonneg through optimset and there is no way to set the maximum iterations. Even if i know roughly how many iterations it takes, it does me no good. 
0
Reply Adnan 5/10/2010 8:41:05 PM

Gene wrote:
> [X, RESNORM, RESIDUAL, EXITFLAG] = LSQNONNEG(A, b, X0, OPTIONS)
> 
> use OPTIMSET to specify the MaxIter field in the OPTIONS structure. With 
> recent versions of Matlab you can replace X0 with ~ (if you don't have a 
> reasonable initial estimate).

The documentation, 
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonneg.html
appears to indicate that in order to supply X0, you must call lsqnonneg 
passing it a "problem", where a "problem" is a structure with various fields, 
including X0 and options . There does not appear to be any way to create a 
"problem" without an X0. If, however, one were simply to omit X0 from the call 
shown above, the call with just A, b, and OPTIONS *would* be a supported syntax.


I have checked the documentation for lsqnonneg, and I have checked the 
documentation describing the use of tilde,

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1

and I do not see anything in either that would indicate general support for 
using ~ to indicate an unknown value when making a call to a function. ~ is 
supported at function *definition* to indicate an argument that the caller 
will provide an argument in that position that should be ignored.

Gene, would you be able to provide a link to documentation of using ~ the way 
you describe?
0
Reply Walter 5/10/2010 11:45:23 PM

"Walter Roberson" <roberson@hushmail.com> wrote in message 
news:hsa5r2$knr$1@canopus.cc.umanitoba.ca...
> Gene wrote:

*snip*

> I have checked the documentation for lsqnonneg, and I have checked the 
> documentation describing the use of tilde,
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1
>
> and I do not see anything in either that would indicate general support 
> for using ~ to indicate an unknown value when making a call to a function. 
> ~ is supported at function *definition* to indicate an argument that the 
> caller will provide an argument in that position that should be ignored.

The tilde operator can be used to indicate an argument should be ignored in 
two cases.

1) In the _declaration_ of the function, a tilde on the RIGHT side of the 
equals sign (as an input) indicates that the function will not use the input 
argument you provide when you call the function.
2) In the _function call_, a tilde on the LEFT side of the equals sign (as 
an output) indicates that the corresponding output should not be assigned to 
a variable in the calling workspace.

You can't specify a tilde on the LEFT side of the declaration or the RIGHT 
side of the function call (well, not on its own.  You can call a function 
with something like ~x as an input if you want, but in that situation it 
doesn't mean "ignore this" but means "logical not".)

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ 


0
Reply Steven 5/11/2010 1:37:37 PM

Walter Roberson <roberson@hushmail.com> wrote in message <hsa5r2$knr$1@canopus.cc.umanitoba.ca>...
> Gene wrote:
> > [X, RESNORM, RESIDUAL, EXITFLAG] = LSQNONNEG(A, b, X0, OPTIONS)
> > 
> > use OPTIMSET to specify the MaxIter field in the OPTIONS structure. With 
> > recent versions of Matlab you can replace X0 with ~ (if you don't have a 
> > reasonable initial estimate).
> 
> The documentation, 
> http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/lsqnonneg.html
> appears to indicate that in order to supply X0, you must call lsqnonneg 
> passing it a "problem", where a "problem" is a structure with various fields, 
> including X0 and options . There does not appear to be any way to create a 
> "problem" without an X0. If, however, one were simply to omit X0 from the call 
> shown above, the call with just A, b, and OPTIONS *would* be a supported syntax.
> 
> 
> I have checked the documentation for lsqnonneg, and I have checked the 
> documentation describing the use of tilde,
> 
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1
> 
> and I do not see anything in either that would indicate general support for 
> using ~ to indicate an unknown value when making a call to a function. ~ is 
> supported at function *definition* to indicate an argument that the caller 
> will provide an argument in that position that should be ignored.
> 
> Gene, would you be able to provide a link to documentation of using ~ the way 
> you describe?

Walter & Adnan:

I think I must request forgiveness for going (less than ? ) half-cocked.

On the use of  tilde (~), I recalled a blog from L. Shure:
http://blogs.mathworks.com/loren/2009/09/11/matlab-release-2009b-best-new-feature-or/
I have not tested this in the lsqnonneg application. Moreover, the documentation states that one can simply 'send' an empty array as a place-holder.

On the specification of MaxIter, I assumed there was an underlying 'active-set' method in which the algorithm has a current estimate for which components of x = 0 (the active set). Such an algorithm would then 'solve' an equality constrained problem (in this case a 'quadratic programming problem'. The resulting Lagrange multipliers would be tested for KKT conditions, while the non-zero components of 'x' would tested for sign and the active-set might then be 'adjusted'.  This class of algorithms would have a clear major iteration step.

I shall put the Lawson and Hanson reference on my 'to read' list.

Mea Culpa

gene
0
Reply Gene 5/11/2010 1:57:04 PM

Gene and others,

Thanks for the help so far. Ive been going through the parallel toolbox manual and i still cannot find a solution to this problem. I figured this would have been a pretty common problem amongst people using the toolbox, but i guess i was wrong. 

Other than using optimset (which doesn't work for MaxIter) i thought of actually mexing the fortran code where i can probably set a maximum iteration value myself, but that just seems very convoluted. Plus, i can foresee this problem occurring again in the future, so it would be good if i figure this out for reference. 

So here are my thoughts on the matter so far. I am also very new to the parallel toolbox, so i might say something wrong or that just doesnt work.

What if i:

1. have a worker that has time counters for each worker. This worker has a while loop that runs for the duration of the program. 

2.Every time a worker finishes, the time counter resets to the current time. If the time exceeds, lets say, 500 seconds, it sends a flag out to kill the workers process. 

3. the workers process is killed, and the worker starts the new iteration. 

I will try to come up with proper code/pseudo code for this, but if anyone has inputs, please share them.

Thanks
0
Reply Adnan 5/12/2010 6:40:21 PM

Adnan:
At the risk of further embarrassment, I'll paste in an 'alternative lsqnonneg' code. This seems to implement the algorithm as in the Lawson & Hanson, except that they 'solve' using the normal equations where we use Matlab's left-divide. The current code allows a user-specified it_max. I have not (yet) looked into the implementation provide in TMW's optimization toolbox.

For further correspondence, please emai me as
ecliff at vt/edu (making the obvious changes).

Have fun

gene

function [ x, res_norm, eJ, flag, lambda ] = my_lsqnoneg( A, b, it_max )
%Seek a nonnegative vector x to minimize \| Ax - b \|^2

% We implement an active set strategy

%% Initialization
    [nb, nx] = size(A);
    if length(b) ~= nb
        error('A and b don''t have the same numer of rows')
    end

    if nargin < 3; it_max = 10*nx; end;  % replace with some heuristic
        
    Full  = 1:nx;
    xzero = zeros(nx, 1);

    I_act = [];  % indices for the initial active set
%   e_min = inf; % a min-norm, feasible point can be stored by
                 % un-commenting this line and lines 39 - 42.
                 
    iter  = 0;

%  Begin main loop
    while iter < it_max
        iter = iter + 1;
        
%% least-squares solution using available variables
        J  = setdiff(Full, I_act);
        xJ = A(:,J)\b; 
       
%%   test feasibility

        Iv = find(xJ < 0); %#ok 
        if isempty(Iv)
            
%%   compute the residual
           eJ = A(:,J)*xJ - b; 
           x = xzero; x(J) = xJ; % full x array (with zeros)
        
%            if norm(eJ) < e_min
%               e_min = eJ;
%               x_best= x;
%            end % e_min
           
%% Compute KKT multipliers           
           lambda = eJ(:)'*A(:, I_act);
           Jv = find(lambda < 0); %#ok
           if isempty(Jv)
               
               break
               
           else
%% Find worst KKT violator and remove constraint from the active set
               [~, Jv_worst] = min(lambda); 
               I_act = setdiff(I_act, I_act(Jv_worst));
           end % KKT  check
           
        else
            
%% Find worst constraint violator and augment the active set  
           [~, Iv_worst] = min(xJ);            
           I_act = union(I_act, J(Iv_worst));  
           
        end % feasibility check

    end % while
    
    res_norm = norm(eJ);
    flag     = (iter < it_max);
end % function
0
Reply Gene 5/13/2010 3:28:04 PM

8 Replies
396 Views

(page loaded in 0.102 seconds)

Similiar Articles:









7/23/2012 2:44:00 AM


Reply: