📄 startmulticoremaster.m
字号:
else
% The parameter file has been generated again. The master does
% not do the job, fileNr2 is not decremented.
end % if ~parameterFileExisting
% reset variables
parameterFileFoundTime = NaN;
curPauseTime = startPauseTime;
else
if debugMode
disp(sprintf('Job nr %d has NOT timed out.', curFileNr));
end
% Remove semaphore.
removefilesemaphore(sem);
if ~masterIsWorker
% If the master is only coordinator, wait some time before
% checking again
if debugMode
disp(sprintf('Coordinator is waiting %.2f seconds', curPauseTime));
end
pause(curPauseTime);
curPauseTime = min(maxPauseTime, curPauseTime + startPauseTime);
end
end % if jobTimedOut
if masterIsWorker
% If the master is also a worker, leave the while-loop if the
% result has not been loaded. Either the job timed out and was done
% by the master or the job has not been finished yet but is also
% not timed out, which is normal.
break
else
% If the master is only coordinator, stay in the while-loop.
end
end % if resultLoaded
end % while 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check if all work is done %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (see comment between the two while-loops)
if (fileNr - 1) + 1 == (fileNr2 + 1)
% all results have been collected, leave big while-loop
if debugMode
disp('********************************');
disp(sprintf('All work is done (fileNr = %d, fileNr2 = %d).', fileNr, fileNr2));
end
break
end
end % while 1
% delete waitbar
waitbar__('delete');
if debugMode
disp(sprintf('%2d jobs at all', nrOfFiles));
disp(sprintf('%2d jobs done by master', nrOfFilesMaster));
disp(sprintf('%2d jobs done by slaves', nrOfFilesSlaves));
disp(sprintf('*********** End of function %s **********', mfilename));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [result, resultLoaded] = loadresultfile__(resultFileName, showFileAccessWarnings)
errorMode = 0;
warnMode = 0;
% reset warnings and errors
lastwarn('');
lasterror('reset');
% try to load file
try
result = []; % (only for M-Lint)
load(resultFileName, 'result'); %% file access %%
resultLoaded = true;
catch
resultLoaded = false;
if showFileAccessWarnings
disp(sprintf('Warning: Unable to load file %s.', resultFileName));
if errorMode
displayerrorstruct;
end
end
end
% display warning (if any)
if showFileAccessWarnings && warnMode
lastMsg = lastwarn;
if ~isempty(lastMsg)
disp(sprintf('Warning issued when trying to load file %s:\n%s', ...
resultFileName, lastMsg));
end
end
% check if variable 'result' is existing
if resultLoaded && ~exist('result', 'var')
if showFileAccessWarnings
disp(sprintf('Warning: Variable ''%s'' not existing after loading file %s.', ...
'result', resultFileName));
end
resultLoaded = false;
end
if resultLoaded
% it seems that loading was successful
% try to remove result file
mbdelete(resultFileName, showFileAccessWarnings); %% file access %%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function waitbar__(command, nrOfFiles, nrOfFilesMaster, nrOfFilesSlaves)
persistent initialized waitbarHandle clockStart
if isempty(initialized)
initialized = 0;
end
switch command
case 'init'
% initialization
tag = 'Multicore waitbar';
delete(findobj('Tag', tag)); % remove all waitbars generated before
%message = sprintf('Multicore execution started\n\n\n');
message = sprintf('0%% done by master\n0%% done by slaves\n0%% done overall\n');
waitbarHandle = waitbar(0, message, 'Name', 'Multicore progress', 'Tag', tag);
set(waitbarHandle, 'HandleVisibility', 'on');
initialized = 1;
case 'init2'
clockStart = clock;
case 'update'
if initialized && ishandle(waitbarHandle)
nrOfFilesReady = nrOfFilesMaster+nrOfFilesSlaves;
message = sprintf('%.0f%% done by master\n%.0f%% done by slaves\n%.0f%% done overall\nest. time left: %s', ...
100 * nrOfFilesMaster/nrOfFiles, ...
100 * nrOfFilesSlaves/nrOfFiles, ...
100 * nrOfFilesReady /nrOfFiles, ...
formattime(round(etime(clock, clockStart)*(nrOfFiles-nrOfFilesReady)/nrOfFilesReady), 'short'));
waitbar((nrOfFilesMaster + nrOfFilesSlaves) / nrOfFiles, waitbarHandle, message);
end
case 'delete'
if initialized && ishandle(waitbarHandle)
delete(waitbarHandle);
end
initialized = 0;
otherwise
error('Command "%s" unknown.', command);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function timeString = formattime(time, mode)
%FORMATTIME Return formatted time string.
% STR = FORMATTIME(TIME) returns a formatted time string for the given
% time difference TIME in seconds, i.e. '1 hour and 5 minutes' for TIME =
% 3900.
%
% FORMATTIME(TIME, MODE) uses the specified display mode ('long' or
% 'short'). Default is long display.
%
% Example:
% str = formattime(142, 'long');
%
% FORMATTIME (without input arguments) shows further examples.
%
% Markus Buehren
% Last modified 21.04.2008
%
% See also ETIME.
if nargin == 0
disp(sprintf('\nExamples for strings returned by function %s.m:', mfilename));
time = [0 1e-4 0.1 1 1.1 2 60 61 62 120 121 122 3600 3660 3720 7200 7260 7320 ...
3600*24 3600*25 3600*26 3600*48 3600*49 3600*50];
for k=1:length(time)
disp(sprintf('time = %6g, timeString = ''%s''', time(k), formattime(time(k))));
end
if nargout > 0
timeString = '';
end
return
end
if ~exist('mode', 'var')
mode = 'long';
end
if time < 0
disp('Warning: Time must be greater or equal zero.');
timeString = '';
elseif time >= 3600*24
days = floor(time / (3600*24));
if days > 1
dayString = 'days';
else
dayString = 'day';
end
hours = floor(mod(time, 3600*24) / 3600);
if hours == 0
timeString = sprintf('%d %s', days, dayString);
else
if hours > 1
hourString = 'hours';
else
hourString = 'hour';
end
timeString = sprintf('%d %s and %d %s', days, dayString, hours, hourString);
end
elseif time >= 3600
hours = floor(mod(time, 3600*24) / 3600);
if hours > 1
hourString = 'hours';
else
hourString = 'hour';
end
minutes = floor(mod(time, 3600) / 60);
if minutes == 0
timeString = sprintf('%d %s', hours, hourString);
else
if minutes > 1
minuteString = 'minutes';
else
minuteString = 'minute';
end
timeString = sprintf('%d %s and %d %s', hours, hourString, minutes, minuteString);
end
elseif time >= 60
minutes = floor(time / 60);
if minutes > 1
minuteString = 'minutes';
else
minuteString = 'minute';
end
seconds = floor(mod(time, 60));
if seconds == 0
timeString = sprintf('%d %s', minutes, minuteString);
else
if seconds > 1
secondString = 'seconds';
else
secondString = 'second';
end
timeString = sprintf('%d %s and %d %s', minutes, minuteString, seconds, secondString);
end
else
if time > 10
seconds = floor(time);
else
seconds = floor(time * 100) / 100;
end
if seconds > 0
if seconds ~= 1
timeString = sprintf('%.4g seconds', seconds);
else
timeString = '1 second';
end
else
timeString = sprintf('%.4g seconds', time);
end
end
switch mode
case 'long'
% do nothing
case 'short'
timeString = strrep(timeString, ' and ', ' ');
timeString = strrep(timeString, ' days', 'd');
timeString = strrep(timeString, ' day', 'd');
timeString = strrep(timeString, ' hours', 'h');
timeString = strrep(timeString, ' hour', 'h');
timeString = strrep(timeString, ' minutes', 'm');
timeString = strrep(timeString, ' minute', 'm');
timeString = strrep(timeString, ' seconds', 's');
timeString = strrep(timeString, ' second', 's');
otherwise
error('Mode ''%s'' unknown in function %s.', mode, mfilename);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -