function s060309_run_cmdfile_distributed( par ) if nargin < 1 error( [ mfilename ' needs 1 input argument.' ] ); end % Check for parameters required_param = { 'INPUT_CMD_FILE', 'CHUNK_SIZE', 'LOCK_FILE_TEMPLATE', 'DONE_FILE_TEMPLATE' }; check_param( required_param, fieldnames( par ) ); % ( 1 ) Load command file disp( ' ' ); disp( [ mfilename ': loading command file...' ] ); disp( [ ' "' par.INPUT_CMD_FILE '"' ] ); fid_in = fopen( par.INPUT_CMD_FILE, 'rt' ); qq = [ char( fread( fid_in, 'char' ).' ) sprintf( '\n' ) ]; fclose( fid_in ); % Split it into command lines [ s,f,t_list ] = regexp( qq, '([^\n]+)\n' ); % ( 2 ) Split it into chunks n_cmd = numel( t_list ); n_chunk = ceil( n_cmd / par.CHUNK_SIZE ); disp( ' ' ); disp( sprintf( [ mfilename ': splitted the %d lines of the command file, into %d chunks of %d lines each.' ], n_cmd, n_chunk, par.CHUNK_SIZE ) ); % ( 3 ) Process the chunks that have not been locked already [ s, machine_name ] = system( 'uname -n' ); machine_name = deblank( machine_name ); disp( ' ' ); disp( [ mfilename ': processing the chunks that have not been locked already...' ] ); chrono = chrono_start( n_chunk, 30 ); for i_chunk = 1:n_chunk lock_filename = sprintf( par.LOCK_FILE_TEMPLATE, i_chunk, n_chunk ); if exist( lock_filename, 'file' ) % Chunk already locked -> skip it disp( sprintf( [ mfilename ': chunk %d/%d already locked. Skipped.' ], i_chunk, n_chunk ) ); else % Chunk not locked yet -> lock it fid = fopen( lock_filename, 'wt' ); if fid < 0 error( [ mfilename ' could not create lock file "' lock_filename '".' ] ); end fprintf( fid, [ mfilename ': chunk %d/%d locked by machine %s\n' ], i_chunk, n_chunk, machine_name ); fclose( fid ); disp( sprintf( [ mfilename ': chunk %d/%d locked.' ], i_chunk, n_chunk ) ); % Then extract this chunk of commands into a temporary file % ( should be faster than running each line individually ) start_cmd = 1 + ( i_chunk - 1 ) * par.CHUNK_SIZE; stop_cmd = min( n_cmd, i_chunk * par.CHUNK_SIZE ); while 1 tmp_cmdfile = [ tempname '-' mfilename ]; if ~exist( tmp_cmdfile, 'file' ), break, end; end tmp_fid = fopen( tmp_cmdfile, 'wt' ); if tmp_fid < 0 error( [ mfilenmae ' could not create temp cmd file "' tmp_cmdfile '".' ] ); end for i_cmd = start_cmd:stop_cmd fprintf( tmp_fid, [ qq( t_list{ i_cmd }(1):t_list{ i_cmd }(2) ) '\n' ] ); end fclose( tmp_fid ); disp( sprintf( ' (executing command lines %d to %d)', start_cmd, stop_cmd ) ); % And finally launch it! system( [ 'sh ' tmp_cmdfile ] ); % Erase temp cmd file system( [ 'rm ' tmp_cmdfile ] ); % Create "done" file done_filename = sprintf( par.DONE_FILE_TEMPLATE, i_chunk, n_chunk ); fid = fopen( done_filename, 'wt' ); if fid < 0 error( [ mfilename ' could not create done file "' done_filename '".' ] ); end fprintf( fid, [ mfilename ': chunk %d/%d fully processed by machine %s.\n' ], i_chunk, n_chunk, machine_name ); fclose( fid ); end % If chunk not locked yet chrono = chrono_check( chrono, i_chunk ); end % Loop through chunks chrono_stop( chrono ); disp( ' ' ); disp( [ mfilename ': done.' ] ); disp( ' ' );