Changes between Initial Version and Version 1 of Workshops/JobCheckpointing/Examples/MATLAB


Ignore:
Timestamp:
04/20/2026 03:38:07 PM (3 months ago)
Author:
Carl Baribault
Comment:

Draft for MATLAB

Legend:

Unmodified
Added
Removed
Modified
  • Workshops/JobCheckpointing/Examples/MATLAB

    v1 v1  
     1[[PageOutline]]
     2= MATLAB Checkpointing Example =
     3
     4Here is an example of a checkpointing application implemented in MATLAB script.
     5
     6By default, the MATLAB checkpointing application behaves as follows.
     7
     8 * checkpointing every 20 application iterations (one second per iteration) and
     9 * running a total of 500 iterations.
     10
     11== Running the MATLAB checkpointing example with defaults ==
     12
     13To run the application on Cypress, perform the following.
     14
     151. Create your own versions of the following files in your current directory on Cypress. (For file editing with '''nano''' on Cypress, see [[https://wiki.hpc.tulane.edu/trac/wiki/cypress/FileEditingSoftware/Example|File Editing Example]].)
     16
     17 * [wiki://Workshops/JobCheckpointing/Examples#checkpoint_submitter.sh checkpoint_submitter.sh]
     18 * [wiki://Workshops/JobCheckpointing/Examples#checkpoint_runner.sh checkpoint_runner.sh]
     19 * [wiki://Workshops/JobCheckpointing/Examples#aggregate_usage.py aggregate_usage.py]
     20 * [wiki://Workshops/JobCheckpointing/Examples#plot_progress.py plot_progress.py]
     21
     222. Create your own version of the following MATLAB application script file '''checkpoint_signal_iter.m'''.
     23
     24{{{
     25%--------------------------------------------------------------------------
     26% checkpoint_signal_iter.m
     27%
     28% MATLAB R2023a application-level checkpointing example
     29% for submitter/runner HPC workflows.
     30%
     31% Contract:
     32%   - Text checkpoint (state_iter.txt): orchestration progress ONLY
     33%   - MAT-file checkpoint (state_iter.mat): application state ONLY
     34%--------------------------------------------------------------------------
     35clc;
     36%% ---------------- Global state (required for onCleanup) ----------------
     37global iter result TXT_CKPT MAT_CKPT MAX_ITER CHECKPOINT_EVERY
     38%% ---------------- Configuration ----------------
     39% Text checkpoint (submitter reads this)
     40TXT_CKPT = getenv('CKPT_PATH');
     41if isempty(TXT_CKPT)
     42  TXT_CKPT = 'state_iter.txt';
     43end
     44% MATLAB binary checkpoint (application state)
     45MAT_CKPT = strrep(TXT_CKPT, '.txt', '.mat');
     46MAX_ITER = getenv('MAX_ITER');
     47if isempty(MAX_ITER)
     48  MAX_ITER = 500;
     49else
     50  MAX_ITER = str2double(MAX_ITER);
     51end
     52CHECKPOINT_EVERY = getenv('CHECKPOINT_EVERY');
     53if isempty(CHECKPOINT_EVERY)
     54  CHECKPOINT_EVERY = 20;
     55else
     56  CHECKPOINT_EVERY = str2double(CHECKPOINT_EVERY);
     57end
     58fprintf('MATLAB checkpointing application\n');
     59fprintf(' Text checkpoint: %s\n', TXT_CKPT);
     60fprintf(' State checkpoint: %s\n', MAT_CKPT);
     61fprintf(' MAX_ITER=%d, CHECKPOINT_EVERY=%d\n', MAX_ITER, CHECKPOINT_EVERY);
     62%% ---------------- Restore checkpoint ----------------
     63if isfile(MAT_CKPT)
     64  S = load(MAT_CKPT, 'iter', 'result');
     65  iter   = S.iter;
     66  result = S.result;
     67  fprintf('Resuming from checkpoint: iter=%d\n', iter);
     68else
     69  iter   = 0;
     70  result = 0;
     71  fprintf('Starting fresh (no checkpoint found)\n');
     72end
     73%% ---------------- Install cleanup handler ----------------
     74cleanupObj = onCleanup(@checkpoint_and_exit);
     75%% ---------------- Main computation ----------------
     76try
     77  while iter < MAX_ITER
     78    iter = iter + 1;
     79    % ---- Simulated work (replace with real computation) ----
     80    pause(1);
     81    result = result + sqrt(iter);
     82    if mod(iter, CHECKPOINT_EVERY) == 0
     83      write_checkpoint();
     84      fprintf('[periodic] checkpoint saved at iter=%d\n', iter);
     85    end
     86  end
     87  % Final checkpoint
     88  write_checkpoint();
     89  fprintf('Completed all iterations\n');
     90  % Exit code 0 = workflow complete
     91  exit(0);
     92catch ME
     93  fprintf(2, 'ERROR: %s\n', ME.message);
     94  exit(1);
     95end
     96%% ---------------- Local helper functions ----------------
     97function write_checkpoint()
     98  global iter result TXT_CKPT MAT_CKPT
     99  % Application state
     100  save(MAT_CKPT, 'iter', 'result');
     101  % Orchestration progress (text)
     102  fid = fopen(TXT_CKPT, 'w');
     103  fprintf(fid, '%d\n', iter);
     104  fclose(fid);
     105end
     106function checkpoint_and_exit()
     107  global iter result TXT_CKPT MAT_CKPT
     108  fprintf('MATLAB exiting; saving checkpoint at iter=%d\n', iter);
     109  write_checkpoint();
     110  fprintf('Exit code 99 (checkpoint written)\n');
     111  exit(99);
     112end
     113}}}
     114
     115
     1163. In order to satisfy the amount of RAM required by MATLAB, modify the requested memory in your local copy of checkpoint_runner.sh according to the following.
     117
     118{{{
     119...
     120#SBATCH --mem=4G
     121...
     122}}}
     123
     1244. Submit the job via the following command.
     125
     126{{{
     127[tulaneID@cypress1 ~]$MODULE_LIST="matlab/r2023a" \
     128APP_CMD="matlab -batch checkpoint_signal_iter" \
     129CKPT_PATH=state_iter_mat.txt \
     130sbatch checkpoint_submitter.sh
     131}}}
     132
     1335. You can monitor job outputs of both the checkpoint_submitter.sh and the checkpoint_runner.sh jobs as they are updated by executing the following '''tail''' command using the '''-F''' option to retry once per second, then using Ctrl+C to exit the retry process entirely.
     134
     135{{{
     136[tulaneID@cypress1 ~]$ tail -F log*
     137}}}
     138
     139== Overriding the default settings ==
     140
     141To customize your workflow, see [wiki://Workshops/JobCheckpointing/Examples#Overridingdefaultsettings Overriding default settings] for overriding the default settings in the checkpoint submitter according to your needs.
     142
     143== Sample run outputs ==
     144
     145=== List of output files ===
     146
     147Here is a list of all output files from a sample run sorted by time, latest-to-earliest
     148
     149{{{
     150[tulaneID@cypress2 ~]$ls -lt *.csv *.err *.mat *.out *.png *.txt
     151-rw-r--r-- 1 tulaneID groupID   474 Apr 20 15:09 anomaly_details.csv
     152-rw-r--r-- 1 tulaneID groupID 26376 Apr 20 15:09 anomaly_stats_table.png
     153-rw-r--r-- 1 tulaneID groupID  1383 Apr 20 15:09 log_submitter_3306584.out
     154-rw-r--r-- 1 tulaneID groupID 78666 Apr 20 15:09 resource_trend.png
     155-rw-r--r-- 1 tulaneID groupID 60274 Apr 20 15:09 progress_with_anomalies.png
     156-rw-r--r-- 1 tulaneID groupID   519 Apr 20 15:09 worker_usage.csv
     157-rw-r--r-- 1 tulaneID groupID   491 Apr 20 15:09 log_submitter_3306582.err
     158-rw-r--r-- 1 tulaneID groupID   760 Apr 20 15:09 log_submitter_3306582.out
     159-rw-r--r-- 1 tulaneID groupID     0 Apr 20 15:09 log_submitter_3306584.err
     160-rw-r--r-- 1 tulaneID groupID    18 Apr 20 15:09 submitter_state.txt
     161-rw-r--r-- 1 tulaneID groupID  1216 Apr 20 15:08 log_runner_3306583.out
     162-rw-r--r-- 1 tulaneID groupID   234 Apr 20 15:08 state_iter_mat.mat
     163-rw-r--r-- 1 tulaneID groupID     4 Apr 20 15:08 state_iter_mat.txt
     164-rw-r--r-- 1 tulaneID groupID     0 Apr 20 15:06 log_runner_3306583.err
     165-rw-r--r-- 1 tulaneID groupID   460 Apr 20 15:06 log_submitter_3306578.err
     166-rw-r--r-- 1 tulaneID groupID   760 Apr 20 15:06 log_submitter_3306578.out
     167-rw-r--r-- 1 tulaneID groupID    40 Apr 20 15:06 worker_ids.txt
     168-rw-r--r-- 1 tulaneID groupID  1111 Apr 20 15:05 log_runner_3306580.out
     169-rw-r--r-- 1 tulaneID groupID     0 Apr 20 15:03 log_runner_3306580.err
     170-rw-r--r-- 1 tulaneID groupID   613 Apr 20 15:03 log_submitter_3306576.err
     171-rw-r--r-- 1 tulaneID groupID   760 Apr 20 15:03 log_submitter_3306576.out
     172-rw-r--r-- 1 tulaneID groupID  1111 Apr 20 15:03 log_runner_3306577.out
     173-rw-r--r-- 1 tulaneID groupID     0 Apr 20 15:01 log_runner_3306577.err
     174-rw-r--r-- 1 tulaneID groupID   460 Apr 20 15:01 log_submitter_3306573.err
     175-rw-r--r-- 1 tulaneID groupID   760 Apr 20 15:01 log_submitter_3306573.out
     176-rw-r--r-- 1 tulaneID groupID  1111 Apr 20 15:00 log_runner_3306574.out
     177-rw-r--r-- 1 tulaneID groupID     0 Apr 20 14:58 log_runner_3306574.err
     178-rw-r--r-- 1 tulaneID groupID   491 Apr 20 14:58 log_submitter_3306570.err
     179-rw-r--r-- 1 tulaneID groupID   767 Apr 20 14:58 log_submitter_3306570.out
     180-rw-r--r-- 1 tulaneID groupID  1107 Apr 20 14:58 log_runner_3306571.out
     181-rw-r--r-- 1 tulaneID groupID     0 Apr 20 14:56 log_runner_3306571.err
     182}}}
     183
     184=== Sample checkpoint submitter logs ===
     185
     186Here are the first and last instances from among the multiple log files generated by the sequence of jobs running the job script [wiki://Workshops/JobCheckpointing/Examples#checkpoint_submitter.sh checkpoint_submitter.sh].
     187
     188The first job in the sequence was submitted by the user, and all other jobs thereafter in that sequence were submitted by the job script itself via '''self-restarting'''.
     189
     190==== Listing of the initial checkpoint submitter log - starting workflow ====
     191
     192Here is the listing of the first log file, '''log_submitter_3306570.out''', generated by [wiki://Workshops/JobCheckpointing/Examples#checkpoint_submitter.sh checkpoint_submitter.sh] ending with the message "Submitter approaching timeout -> restarting self. Submitted batch job 3306573."
     193
     194{{{
     195[tulaneID@cypress2 ~]$cat log_submitter_3306570.out
     196Info[20260420-14:56:04]: Start on cypress01-057; JOBID=3306570
     197Info[20260420-14:56:04]: Submitter settings:
     198Info[20260420-14:56:04]: CKPT_PATH=state_iter_mat.txt
     199Info[20260420-14:56:04]: MAX_ITER=500
     200Info[20260420-14:56:04]: RUNNER_SCRIPT=checkpoint_runner.sh
     201Info[20260420-14:56:04]: SUBMITTER_MARGIN_SEC=60
     202Info[20260420-14:56:04]: SUBMITTER_TIME_LIMIT=3:30
     203Info[20260420-14:56:04]: No previous state found; starting fresh.
     204Info[20260420-14:56:04]: Submitter cycle: 0
     205Info[20260420-14:56:04]: Current checkpoint iteration: 0
     206Info[20260420-14:56:04]: Submitted worker: 3306571
     207Info[20260420-14:56:04]: Waiting on first of worker 3306571 to finish or self-restart.
     208Info[20260420-14:58:44]: Submitter approaching timeout -> restarting self
     209Submitted batch job 3306573
     210}}}
     211
     212==== Listing of the final checkpoint submitter log - completed workflow ====
     213
     214Here is the listing of the final log file, '''log_submitter_3306584.out''', generated by [wiki://Workshops/JobCheckpointing/Examples#checkpoint_submitter.sh checkpoint_submitter.sh] ending with the message "Workflow complete. Exiting submitter."
     215
     216{{{
     217[tulaneID@cypress2 ~]$cat log_submitter_3306584.out
     218Info[20260420-15:09:06]: Start on cypress01-057; JOBID=3306584
     219Info[20260420-15:09:06]: Submitter settings:
     220Info[20260420-15:09:06]: CKPT_PATH=state_iter_mat.txt
     221Info[20260420-15:09:06]: MAX_ITER=500
     222Info[20260420-15:09:06]: RUNNER_SCRIPT=checkpoint_runner.sh
     223Info[20260420-15:09:06]: SUBMITTER_MARGIN_SEC=60
     224Info[20260420-15:09:06]: SUBMITTER_TIME_LIMIT=3:30
     225Info[20260420-15:09:06]: Loaded state: submitter_cycle=5
     226Info[20260420-15:09:06]: Submitter cycle: 5
     227Info[20260420-15:09:06]: Current checkpoint iteration: 500
     228Info[20260420-15:09:06]: Reached MAX_ITER=500; running final analytics.
     229Info[20260420-15:09:06]: Running analytics aggregate_usage.py ...
     230Wrote worker_usage.csv
     231     JobID  ElapsedSeconds   TotalCPU  ... ReqMem       NodeList   State
     2320  3306571             120  00:39.177  ...    4Gn  cypress01-057  FAILED
     2331  3306574             121  00:38.906  ...    4Gn  cypress01-057  FAILED
     2342  3306577             120  00:39.133  ...    4Gn  cypress01-057  FAILED
     2353  3306580             121  00:38.991  ...    4Gn  cypress01-057  FAILED
     2364  3306583             120  00:39.425  ...    4Gn  cypress01-057  FAILED
     237
     238[5 rows x 9 columns]
     239Info[20260420-15:09:08]: Running analytics plot_progress.py ...
     240Saved progress_with_anomalies.png
     241Saved resource_trend.png
     242Saved anomaly_details.csv
     243Saved anomaly_stats_table.png
     244Info[20260420-15:09:12]: Workflow complete. Exiting submitter.
     245}}}
     246
     247=== Sample checkpoint runner logs ===
     248
     249Here are the first and last output logs of the sequence of jobs generated by the job script [wiki://Workshops/JobCheckpointing/Examples#checkpoint_runner.sh checkpoint_runner.sh].
     250
     251All of the jobs in that sequence were submitted by the [wiki://Workshops/JobCheckpointing/Examples#checkpoint_submitter.sh checkpoint_submitter.sh] job script.
     252
     253==== Listing of the initial checkpoint runner log - starting application ====
     254
     255Here is the listing of the intial log file, '''log_runner_3306571.out''', generated by the job script [wiki://Workshops/JobCheckpointing/Examples#checkpoint_runner.sh checkpoint_runner.sh] ending with message "Timeout + checkpoint advance detected (0->100)."
     256
     257{{{
     258[tulaneID@cypress2 ~]$cat log_runner_3306571.out
     259Info[20260420-14:56:04]: Start on cypress01-057; JOBID=3306571
     260Info[20260420-14:56:04]: Runner settings:
     261Info[20260420-14:56:04]: APP_CMD=matlab -batch checkpoint_signal_iter
     262Info[20260420-14:56:04]: CHECKPOINT_EVERY=20
     263Info[20260420-14:56:04]: CKPT_PATH=state_iter_mat.txt
     264Info[20260420-14:56:04]: LAUNCH_MODE=direct
     265Info[20260420-14:56:04]: MAX_ITER=500
     266Info[20260420-14:56:04]: MODULE_LIST=matlab/r2023a
     267Info[20260420-14:56:04]: RUNNER_MARGIN_SEC=60
     268Info[20260420-14:56:04]: RUNNER_TIME_LIMIT=00:03:00
     269Info[20260420-14:56:04]: SRUN_ARGS=-n 1
     270Info[20260420-14:56:04]: Loading modules.
     271MATLAB checkpointing application
     272 Text checkpoint: state_iter_mat.txt
     273 State checkpoint: state_iter_mat.mat
     274 MAX_ITER=500, CHECKPOINT_EVERY=20
     275Starting fresh (no checkpoint found)
     276[periodic] checkpoint saved at iter=20
     277[periodic] checkpoint saved at iter=40
     278[periodic] checkpoint saved at iter=60
     279[periodic] checkpoint saved at iter=80
     280[periodic] checkpoint saved at iter=100
     281Info[20260420-14:58:04]: Program exit code (from timeout wrapper): 124
     282Info[20260420-14:58:04]: Timeout + checkpoint advance detected (0->100).
     283}}}
     284
     285==== Listing of the final checkpoint runner log - completed application ====
     286
     287Here is the listing of the final log file, '''log_runner_3303171.out''', generated by [wiki://Workshops/JobCheckpointing/Examples#checkpoint_runner.sh checkpoint_runner.sh] ending with message "Timeout + checkpoint advance detected (400->500)."
     288
     289{{{
     290[tulaneID@cypress2 ~]$cat log_runner_3306583.out
     291Info[20260420-15:06:26]: Start on cypress01-057; JOBID=3306583
     292Info[20260420-15:06:26]: Runner settings:
     293Info[20260420-15:06:26]: APP_CMD=matlab -batch checkpoint_signal_iter
     294Info[20260420-15:06:26]: CHECKPOINT_EVERY=20
     295Info[20260420-15:06:26]: CKPT_PATH=state_iter_mat.txt
     296Info[20260420-15:06:26]: LAUNCH_MODE=direct
     297Info[20260420-15:06:26]: MAX_ITER=500
     298Info[20260420-15:06:26]: MODULE_LIST=matlab/r2023a
     299Info[20260420-15:06:26]: RUNNER_MARGIN_SEC=60
     300Info[20260420-15:06:26]: RUNNER_TIME_LIMIT=00:03:00
     301Info[20260420-15:06:26]: SRUN_ARGS=-n 1
     302Info[20260420-15:06:26]: Loading modules.
     303MATLAB checkpointing application
     304 Text checkpoint: state_iter_mat.txt
     305 State checkpoint: state_iter_mat.mat
     306 MAX_ITER=500, CHECKPOINT_EVERY=20
     307Resuming from checkpoint: iter=400
     308[periodic] checkpoint saved at iter=420
     309[periodic] checkpoint saved at iter=440
     310[periodic] checkpoint saved at iter=460
     311[periodic] checkpoint saved at iter=480
     312[periodic] checkpoint saved at iter=500
     313Completed all iterations
     314MATLAB exiting; saving checkpoint at iter=500
     315Exit code 99 (checkpoint written)
     316Info[20260420-15:08:26]: Program exit code (from timeout wrapper): 124
     317Info[20260420-15:08:26]: Timeout + checkpoint advance detected (400->500).
     318}}}
     319
     320=== Listings of post-workflow analysis output files ===
     321
     322The following is the complete, annotated listing of all the generated .csv and .txt files
     323
     324{{{
     325[tulaneID@cypress2 ~]$tail -n +1 *.csv *.txt
     326==> anomaly_details.csv <==
     327JobID,StartTime,ElapsedSeconds,MaxRSS_MiB,TimeZ,MemModZ,AnomalyType
     3283306571,2026-04-20 14:56:04,120.0,1141.998291015625,-0.8164965809277377,0.0,none
     3293306574,2026-04-20 14:58:44,121.0,1146.6522216796875,1.2247448713915774,0.0,none
     3303306577,2026-04-20 15:01:15,120.0,1188.140869140625,-0.8164965809277377,0.0,none
     3313306580,2026-04-20 15:03:55,121.0,1143.268585205078,1.2247448713915774,0.0,none
     3323306583,2026-04-20 15:06:26,120.0,1155.21240234375,-0.8164965809277377,0.6745,none
     333
     334==> worker_usage.csv <==
     335JobID,ElapsedSeconds,TotalCPU,MaxRSS,MaxRSS_bytes,MaxRSS_MiB,ReqMem,NodeList,State
     3363306571,120,00:39.177,1197472K,1197472000.0,1141.998291015625,4Gn,cypress01-057,FAILED
     3373306574,121,00:38.906,1202352K,1202352000.0,1146.6522216796875,4Gn,cypress01-057,FAILED
     3383306577,120,00:39.133,1245856K,1245856000.0,1188.140869140625,4Gn,cypress01-057,FAILED
     3393306580,121,00:38.991,1198804K,1198804000.0,1143.2685852050781,4Gn,cypress01-057,FAILED
     3403306583,120,00:39.425,1211328K,1211328000.0,1155.21240234375,4Gn,cypress01-057,FAILED
     341
     342==> state_iter_mat.txt <==
     343500
     344
     345==> submitter_state.txt <==
     346submitter_cycle=5
     347
     348==> worker_ids.txt <==
     3493306571
     3503306574
     3513306577
     3523306580
     3533306583
     354}}}
     355
     356=== Post-workflow analysis plots ===
     357
     358==== Background ====
     359
     360For background on the following plots, see [wiki://Workshops/JobCheckpointing/Examples#Plottingexecutiontimeandmemoryusage Plotting execution time and memory usage].
     361
     362==== Table of anomalies, compute time and memory usage ====
     363
     364Here is the plot, '''anomaly_stats_table.png''', showing the table of anomalies, if any, from among all of the [wiki://Workshops/JobCheckpointing/Examples#checkpoint_runner.sh checkpoint_runner.sh] jobs.
     365
     366[[Image(MATLAB_anomaly_stats_table.png, width=800px)]]
     367
     368==== Compute progress ====
     369
     370Here is the plot, '''progress_with_anomalies.png''', showing '''Checkpoint Progress''' and '''MaxRSS''' (SLURM max job memory usage) vs '''Wall Time'''.
     371
     372[[Image(MATLAB_progress_with_anomalies.png, width=800px)]]
     373
     374==== Execution time and memory usage trends ====
     375
     376Here is the plot, '''resource_trend.png''', showing '''Elapsed (time)''' and '''MaxRSS''' vs. '''Worker job index'''.
     377
     378[[Image(MATLAB_resource_trend.png, width=800px)]]