This is a static copy of a profile reportHome
chunk_start (202 calls, 1.652 sec)
Generated 05-Nov-2014 07:52:32 using cpu time.
function in file /usr0/home/jenkins/workspace/cellorganizer-demo3D11-glnx64/utilities/3D/diffeomorphic/chunk_start.m
Copy to new window for comparing multiple runs
Parents (calling functions)
Function Name | Function Type | Calls |
img2model | function | 202 |
Lines where the most time was spent
Line Number | Code | Calls | Total Time | % Time | Time Plot |
41 | if ~exist(temp_name) &&... | 202 | 0.360 s | 21.8% |  |
71 | [status, output] = system(curr... | 202 | 0.200 s | 12.1% |  |
48 | [~, out] = system('uname -mrsn... | 202 | 0.180 s | 10.9% |  |
75 | [status, output] = system(curr... | 202 | 0.160 s | 9.7% |  |
76 | [status, stat_output2] = syste... | 202 | 0.140 s | 8.5% |  |
All other lines | | | 0.611 s | 37.0% |  |
Totals | | | 1.652 s | 100% | |
Children (called functions)
Function Name | Function Type | Calls | Total Time | % Time | Time Plot |
num2str | function | 202 | 0.090 s | 5.5% |  |
strread | function | 404 | 0.040 s | 2.4% |  |
opaque.char | function | 202 | 0.040 s | 2.4% |  |
java.net.Inet4Address | Java method | 202 | 0.010 s | 0.6% |  |
Self time (built-ins, overhead, etc.) | | | 1.472 s | 89.1% |  |
Totals | | | 1.652 s | 100% | |
Code Analyzer results
Line number | Message |
41 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
41 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
71 | The value assigned here to 'status' appears to be unused. Consider replacing it by ~. |
71 | The value assigned to variable 'output' might be unused. |
73 | The value assigned here to 'status' appears to be unused. Consider replacing it by ~. |
75 | The value assigned here to 'status' appears to be unused. Consider replacing it by ~. |
75 | The value assigned to variable 'output' might be unused. |
76 | The value assigned here to 'status' appears to be unused. Consider replacing it by ~. |
83 | STRREAD will be removed in a future release. Use TEXTSCAN instead. |
84 | STRREAD will be removed in a future release. Use TEXTSCAN instead. |
100 | The value assigned here to 'status' appears to be unused. Consider replacing it by ~. |
100 | The value assigned to variable 'output' might be unused. |
106 | EXIST with two input arguments is generally faster and clearer than with one input argument. |
Coverage results
[ Show coverage for parent directory ]
Total lines in function | 112 |
Non-code lines (comments, blank lines) | 55 |
Code lines (lines that can run) | 57 |
Code lines that did run | 44 |
Code lines that did not run | 13 |
Coverage (did run/can run) | 77.19 % |
Function listing
time calls line
1 function [can_start, final_name, final_exists, temp_name] = chunk_start( fname, final_extension, should_block)
2 %[can_start, final_name, final_exists] = chunk_start(path, fname, final_extension, should_block)
3 %chunk_finish(path, fname, unique_filename)
4 % chunk_start checks if a certain unit of work has been done or is being
5 % computed. The path is the location of both the final and temporary/lock
6 % files. Final files are assumed to be named [fname, '.mat'] by default,
7 % but the extension can be changed with the optional argument
8 % final_extension. Temporary files are named [fname, '.tmp']. Save to
9 % final_name and then call chunk_finish to delete the temporary file.
10 %
11 % Copyright 2008-2013 Taraz Buck/tebuck at cmu.
12 %
13 % Tests:
14 % [can_start, final_name, final_exists] = chunk_start('.', 'atomic_test')
15 % dir('./*atomic_test*')
16 % [can_start, final_name, final_exists] = chunk_start('.', 'atomic_test')
17 % dir('./*atomic_test*')
18 % a = pi; save(final_name, 'a'); chunk_finish('.', 'atomic_test');
19 % dir('./*atomic_test*')
20 % [can_start, final_name, final_exists] = chunk_start('.', 'atomic_test')
21 % dir('./*atomic_test*')
22 %
23 % 2011-12-30 tebuck: Adding job id, if applicable, for greater
24 % differences between cluster jobs without hoping for clock or
25 % hostname differences.
26 % 2012-11-03 tebuck: added an initial file existence check for quick fail.
27 % 2013-04-20 tebuck: making final_extension argument optional, adding fourth to sleep until a computing file becomes available.
28 % 2013-05-19 tebuck: modifying process so temporary file creation/locking is (hopefully) atomic, jobs do not get done multiple times, and we do not end up with corrupted final files.
29
30 % Process options
202 31 if nargin < 3 || isempty(final_extension)
202 32 final_extension = '.mat';
202 33 end
34
0.02 202 35 if ~exist('should_block', 'var') || isempty(should_block)
202 36 should_block = false;
202 37 end
202 38 temp_name = [ fname, '.tmp'];
202 39 final_name = [ fname, final_extension];
40
0.36 202 41 if ~exist(temp_name) && exist(final_name)
42 can_start = false;
43 final_exists = true;
44 return;
45 end
46
47 %Check which os we're using and adjust the stat function accordingly
0.18 202 48 [~, out] = system('uname -mrsn');
0.01 202 49 outtokens = regexp(out, ' ', 'split');
0.01 202 50 osver = outtokens{1};
51
202 52 if strcmpi(osver, 'darwin')
53 statstr = 'stat -L -f"%l" ';
202 54 elseif strcmpi(osver, 'linux')
202 55 statstr = 'stat --format="%h" ';
202 56 end
57
58
202 59 final_exists = false;
60 % Create unique name using job ID and hostname and use the method described in <http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/avoid-race.html>:
0.01 202 61 hostname = regexprep(getenv('HOSTNAME'), '[\r\n]', '');
202 62 if isempty(hostname)
0.12 202 63 hostname = char(getHostName(java.net.InetAddress.getLocalHost()));
202 64 end
202 65 job_id = regexprep(getenv('PBS_JOBID'), '[\r\n]', '');
202 66 if isempty(job_id)
0.09 202 67 job_id = num2str(feature('getpid'));
0.01 202 68 end
202 69 unique_name = [ fname, '_', hostname, '_', job_id, '.tmp'];
202 70 current_command = ['touch "', unique_name, '"'];
0.20 202 71 [status, output] = system(current_command);
0.02 202 72 stat_command = [statstr '"', unique_name, '"'];
0.13 202 73 [status, stat_output] = system(stat_command);
0.01 202 74 current_command2 = ['link "', unique_name, '" "', temp_name, '"'];
0.16 202 75 [status, output] = system(current_command2);
0.14 202 76 [status, stat_output2] = system(stat_command);
77
202 78 work_being_done = false;
79
80 % hard_link_count = str2num(stat_output)
81 % hard_link_count2 = str2num(stat_output2)
82 % When code is pasted into the command line, it is taken up by Matlab as if it were part of the stdout of system, so just read the first integer instead of the above two lines:
0.03 202 83 hard_link_count = strread(stat_output, '%d', 1);
0.03 202 84 hard_link_count2 = strread(stat_output2, '%d', 1);
85 % if isempty(hard_link_count) || isempty(hard_link_count2)
86 % keyboard
87 % end
88 % whos hard_link_count hard_link_count2
202 89 if hard_link_count2 == 2 && hard_link_count == 1
90 % Go ahead with work, we successfully created the first .tmp file:
202 91 can_start = true;
92 else
93 % Someone else, possibly another matlabpool worker on the same machine and job, has already linked this or another file, do not start:
94 can_start = false;
95 work_being_done = true;
96 end
97
98 % Unlink the unique file, stat_output2 should be 1 if creating a new link fails:
0.01 202 99 current_command = ['unlink "', unique_name, '"'];
0.11 202 100 [status, output] = system(current_command);
101
102 % final_exists, can_start, pause
103
202 104 while true
105 % If another job is doing this work but is unfinished and we should wait until the work finishes, just pause and keep checking again if work and writing are both finished:
202 106 if work_being_done && should_block && exist(temp_name)
107 pause(1);
108 continue;
202 109 else
202 110 break;
111 end
112 end