forked from JAAdrian/MatlabProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateParallel.m
95 lines (81 loc) · 3.4 KB
/
updateParallel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function [pattern] = updateParallel(stepSize, workerDirName)
%UPDATEPARALLEL Update function when ProgressBar is used in parallel setup
% -------------------------------------------------------------------------
% This function replaces the update() method of the ProgressBar() class
% when a progress in a parfor loop should be displayed. The function writes
% by default to a temp file in the local temp dir. Each worker will call a
% copy of this function and if a persistent file name variable is not yet
% set a unique file name will be generated and a binary file will be
% initialized. Each worker remembers its own file name to write to and will
% update its own current progress and write it to file. The ProgressBar()
% class will handle the management of all worker files.
%
%
% Usage: [pattern] = updateParallel(stepSize, workerDirName)
%
% Input: ---------
% stepSize - the size of the progress step when the function is
% called. This can be used to pass the number of
% processed bytes when using 'Bytes' as units. If
% bytes are used be sure to pass only integer values.
% [default: stepSize = 1]
% workerDirName - directory where the worker aux. files will be
% saved. This can be specified for debug purposes
% or if multiple progress bars in a parallel
% setup would get in each other's way since all
% have the same file pattern and would distract
% each progress bar's progress state.
% [default: workerDirName = tempdir()]
%
% Output: ---------
% filePattern - the common beginning of every file name before the
% unique part begins. This is an auxiliary function
% output which is used by the ProgressBar() class.
% Typically not be of interest for the user. The
% variable is only returned if no input arguments were
% passed!
%
%
%
% Author: J.-A. Adrian (JA) <jensalrik.adrian AT gmail.com>
%
% some constants
persistent workerFileName;
filePattern = 'progbarworker_';
% input parsing and validation
narginchk(0, 2);
if nargin < 2 || isempty(workerDirName)
workerDirName = tempdir;
end
if nargin <1 || isempty(stepSize)
stepSize = 1;
end
if ~nargin && nargout
pattern = [filePattern, '*'];
return;
end
validateattributes(stepSize, ...
{'numeric'}, ...
{'scalar', 'positive', 'integer', 'real', 'nonnan', ...
'finite', 'nonempty'} ...
);
validateattributes(workerDirName, {'char'}, {'nonempty'});
% if the function is called the first time the persistent variable is
% initialized and the worker file is created. The condition is skipped in
% the following calls.
if isempty(workerFileName)
uuid = char(java.util.UUID.randomUUID);
workerFileName = fullfile(workerDirName, [filePattern, uuid]);
fid = fopen(workerFileName, 'wb');
fwrite(fid, 0, 'uint64');
fclose(fid);
end
% this part is executed every time the function is called:
% open the binary file and increment the existing progress with stepSize
fid = fopen(workerFileName, 'r+b');
if fid > 0
status = fread(fid, 1, 'uint64');
fseek(fid, 0, 'bof');
fwrite(fid, status + stepSize, 'uint64');
fclose(fid);
end