www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mbcfoundation/@DCTManager/getJobTracker.m
function S = getJobTracker(manager, Job) %GETJOBTRACKER Create functions for tracking job progress % % GETJOBTRACKER(MANAGER, JOB) creates and returns a structure of function % handles that return data about the progress of JOB. These function % handles can be used to maintain a progress meter for the tasks in JOB. % Copyright 2006-2013 The MathWorks, Inc. S = struct(... 'SetSampleFrequency', @nSetSampleFreq, ... 'JobState', @() Job.State, ... 'QueueNumber', @nCountQueuedJobs, ... 'CurrentRunnerInfo', @nGetRunningInfo, ... 'NumTasksCompleted', @nCountTasksDone, ... 'NumTasksRunning', @nCountTasksExecuting); % List of completed task indices. Once a task is complete, it cannot go % back to be run again, so we don't need to keep looking at tasks once they % reach this state. TasksDone = []; % List of running task indices TasksRunning = []; % Time that we last updated task information TaskInfoLastUpdated = 0; % Queue number QueueNumber = 0; % Running job info RunningJobInfo = '<unavailable>'; % Time that we last updated job status information JobInfoLastUpdated = 0; % Sample Frequency UpdateTime = datenum([0 0 0 0 0 1]); % 1 second function nSetSampleFreq(Seconds) UpdateTime = datenum([0 0 0 0 0 Seconds]); end function NPend = nCountQueuedJobs nUpdateJobInformation; NPend = QueueNumber; end function S = nGetRunningInfo nUpdateJobInformation; S = RunningJobInfo; end function NDone = nCountTasksDone if i_isStarted(Job) nUpdateTasksDone; NDone = length(TasksDone); else NDone = 0; end end function NRunning = nCountTasksExecuting if i_isExecuting(Job) nUpdateTasksDone; NRunning = length(TasksRunning); else NRunning = 0; end end function nUpdateJobInformation % Update job status information if (now-JobInfoLastUpdated)>UpdateTime JobInfoLastUpdated = now; if strcmp(Job.State, 'queued') % Step through all jobs above this one and count the number % queued until we hit a running job. We assume that there % are no others queued above a running job cluster = Job.Parent; allJobsOnCluster = cluster.Jobs; queueLength = nnz(strcmp({allJobsOnCluster.State}, 'queued')); Jrunning = allJobsOnCluster(strcmp({allJobsOnCluster.State}, 'running')); QueueNumber = queueLength; if isscalar(Jrunning) % The while loop exited when it found a running job. % Construct a string containing info about this job. RunningJobInfo = sprintf('''%s'' by %s', Jrunning.Name, Jrunning.Username); else RunningJobInfo = '<unavailable>'; end else % We are not in the queue QueueNumber = 0; RunningJobInfo = '<unavailable>'; end end end function nUpdateTasksDone % Check whether an update is required if (now-TaskInfoLastUpdated)>UpdateTime TaskInfoLastUpdated = now; % Update the list of completed tasks This is done by iterating % through the previously non-complete tasks until we get to one % that hasn't yet started running. Because tasks are picked up for % execution serially we know that no tasks beyond this point can be % done yet. We assume that no completed tasks have been destroyed. T = Job.Tasks; Done = zeros(size(T)); Done(TasksDone) = 1; for n = 1:length(T) if ~Done(n) State = T(n).State; if strcmp(State, 'running') Done(n) = 2; elseif strcmp(State, 'finished'); Done(n) = 1; elseif strcmp(State, 'pending'); break end end end TasksDone = find(Done==1); TasksRunning = find(Done==2); end end end function ok = i_isStarted(Job) State = Job.State; ok = strcmp(State, 'running') || strcmp(State, 'finished') || strcmp(State, 'failed'); end function ok = i_isExecuting(Job) State = Job.State; ok = strcmp(State, 'running'); end