Home > vbmeg > functions > tool_box > dmri_processor > functions > util > tck_file_processor > dmri_fiber_tck_file_worker_start.m

dmri_fiber_tck_file_worker_start

PURPOSE ^

Process tck files.

SYNOPSIS ^

function dmri_fiber_tck_file_worker_start(data_dir, worker_number)

DESCRIPTION ^

 Process tck files.

 [Usage]
    dmri_fiber_tck_file_worker_start(data_dir, worker_number);

 [Input]
         data_dir : tck file directory.
    worker_number : This is a number given by manager program

 [Output]
    none

 [Note]
    Many workers(MATLAB) concurrently launched to process tck files.
    So, synchronous processing is required.
    To realize it, directory is used as a exclusion access control.

    1.Fiber tracking program creates directory.
         data_dir/finished/tck_number
    2.Worker program try to delete the directory.
      if success, it means that the worker got a authority to 
      process the tck file.

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function dmri_fiber_tck_file_worker_start(data_dir, worker_number)
0002 % Process tck files.
0003 %
0004 % [Usage]
0005 %    dmri_fiber_tck_file_worker_start(data_dir, worker_number);
0006 %
0007 % [Input]
0008 %         data_dir : tck file directory.
0009 %    worker_number : This is a number given by manager program
0010 %
0011 % [Output]
0012 %    none
0013 %
0014 % [Note]
0015 %    Many workers(MATLAB) concurrently launched to process tck files.
0016 %    So, synchronous processing is required.
0017 %    To realize it, directory is used as a exclusion access control.
0018 %
0019 %    1.Fiber tracking program creates directory.
0020 %         data_dir/finished/tck_number
0021 %    2.Worker program try to delete the directory.
0022 %      if success, it means that the worker got a authority to
0023 %      process the tck file.
0024 %
0025 % Copyright (C) 2011, ATR All Rights Reserved.
0026 % License : New BSD License(see VBMEG_LICENSE.txt)
0027 
0028 % Worker create worker directory to notify a manager to ready to process.
0029 % Note: The directory will be deleted by manager.
0030 worker_dir = fullfile(data_dir, ['worker', num2str(worker_number)]);
0031 mkdir(worker_dir);
0032 log_file = fullfile(worker_dir, 'log.txt');
0033 diary_flush(log_file);
0034 
0035 try
0036 parcel_mm_file = fullfile(data_dir, filesep, 'parcel_mm_coord.mat');
0037 
0038 % track file
0039 finished_dir   = fullfile(data_dir, filesep, 'finished');
0040 processed_dir  = fullfile(data_dir, filesep, 'processed');
0041 % start processing
0042 while(exist(worker_dir, 'dir') == 7)
0043 
0044     d = dir(finished_dir);
0045 
0046     for k=1:length(d)
0047         name = d(k).name;
0048         if strcmp(name, '.') || strcmp(name, '..')
0049             continue;
0050         end
0051         % To obtain an authority to process tck file, try to make a directory.
0052         % if the return value is success, the worker got an authority.
0053         [ret, warn] = mkdir(fullfile(processed_dir, filesep, name));
0054         % (mkdir always retuns 1.... second ret value check)
0055 
0056         % Get process authority
0057         if isempty(warn)
0058             % Success
0059             log_write(sprintf('%s.tck processing start\n', name));
0060             % Process tck file.
0061             tck_filename      = ['parcel', name, '.tck'];
0062             output_fiber_file = fullfile(data_dir, filesep, ['parcel_fiber', name, '.mat']);
0063             
0064             if exist(fullfile(data_dir, tck_filename), 'file') ~= 2
0065                 error(sprintf('%s not found.', tck_filename));
0066             end
0067             save_connection(data_dir, tck_filename, parcel_mm_file, output_fiber_file);
0068             log_write(sprintf('%s.tck processing end\n', name));
0069 
0070             % delete processed name from tck list
0071             ret = rmdir(fullfile(finished_dir, filesep, name));
0072             diary_flush(log_file);
0073         else
0074             % Failure
0075             % log_write(sprintf('Failed to get a process authority %s.tck\n', name));
0076         end
0077         % Terminate
0078         if exist(worker_dir, 'dir') ~= 7, break; end
0079         pause(0.01);
0080     end
0081     pause(1);
0082 end
0083 catch
0084     errinfo = lasterror;
0085     log_write(errinfo.message);
0086     diary_flush;
0087     rethrow(errinfo);
0088 end
0089 diary_flush;
0090 exit;
0091 
0092 function log_write(message)
0093    fprintf('%s:%s', datestr(now, 31), message);
0094    
0095 function diary_flush(diary_file)
0096     diary off;
0097     if exist('diary_file', 'var') && ~isempty(diary_file)
0098         diary(diary_file);
0099     end
0100

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005