Checkpointing

Checkpointing allows you to save a snapshot of a job’s state so you can restart it in case your job fails or is interrupted while it is running. It is important that jobs running on the scavenge partion be able to checkpoint, as they may be interrupted at any point due to the nature of this queue.

Distributed Multi-Threaded Checkpointing (DMTCP) is installed on HPC in /usr/bin/ and should be in your path by default.

$ which dmtcp_launch
/usr/bin/dmtcp_launch

DMTCP Example (C Program)

[This example was modified to run on HPC.]

To demonstrate DMTCP, we’ll use a simple program that prints out an integer (starting at 0), sleeps for 1 second, and then prints the next integer.

Create Program

Here is an example written in C. Copy these lines to a file named count.c.

#include <stdio.h>
#include <unistd.h>

int main(void) {
  unsigned long ii = 0;
  while (1) {
    printf("%lu ", ii);
    ii = ii + 1;
    sleep(1);
    fflush(stdout);
  }
}
Compile and Run

Once you have created count.c, compile the program. The resulting executable will be named “count”.

$ gcc count.c -o count

Try running the program without DMTCP by typing ./count. Press Ctrl-C to stop the program.

We can tell DMTCP to checkpoint in intervals of “<number>” seconds by specifying -i <number> as an argument. We’ll try a relatively short interval of 5 seconds for this example. Press Ctrl-C sometime after the first 5 seconds of running with DMTCP to kill the application.

$ dmtcp_launch -i 5 ./count
0 1 2 3 4 5 6 7 8 9 ^C

Note: You must explicitly specify an absolute path (i.e., use ./count, not count) or you will get an error.

DMTCP uses a default port (7779) when it runs. If other people use DMTCP on the same node, there may be conflicts with the port. You can specify a different port by using the argument -p <port number>, or try a random port with -p 0.

View Result

Take a look at the output of dmtcp_launch:

$ ls
ckpt_count_762a16ea7d13f8c4-40000-d8bcd6ac676ad.dmtcp
count.c   
dmtcp_restart_script_762a16ea7d13f8c4-40000-d8bcd2a9ba711.sh*
count*                                                  
dmtcp_restart_script.sh@

DMTCP produces a checkpointing file, a script for restarting the program, and a script wrapper. The program can be restarted manually or by using the convenient wrapper from the most recent checkpoint in the current directory.

# Restart manually
$ dmtcp_restart ckpt_count_762a16ea7d13f8c4-40000-d8bcd6ac676ad.dmtcp
9 10 11 12 13 ^C

# Restart using wrapper
$ ./dmtcp_restart_script.sh
9 10 11 12 13 ^C

DMTCP Example (Perl Script)

[This example was modified to run on HPC.]

Here is a similar example written in Perl. Copy these lines to a file named count.pl.

#/usr/bin/perl -w

$| = 1; # autoflush STDOUT

$ii = 0;
while (1) {
  print "$ii ";
  $ii = $ii + 1;
  sleep(1);
}

Scripts do not need to be pre-compiled but the interpreter for the script must be called as an argument to dmtcp_launch.

$ dmtcp_launch -i 5 perl -w count.pl
0 1 2 3 4 ^C

We now see a Perl checkpoint file. Note that the dmtcp_restart_script.sh wrapper has been updated to restart the Perl process and can no longer be used to restart the C program.

# View new perl output
$ ls -F
ckpt_count_762a16ea7d13f8c4-40000-d8bcd6ac676ad.dmtcp
ckpt_perl_762a16ea7d13f8c4-40000-d8bfaa2ef36ab.dmtcp
count*
count.c
count.pl
dmtcp_restart_script_762a16ea7d13f8c4-40000-d8bcd2a9ba711.sh*
dmtcp_restart_script_762a16ea7d13f8c4-40000-d8bfa65866428.sh*
dmtcp_restart_script.sh@
# Restart
$ ./dmtcp_restart_script.sh
5 6 7 ^C

Restarting a Job

[This example was modified to run on HPC.]

Once you have run a job using DMTCP to create checkpoints you will find several new files starting with dmtcp_ and ckpt_ in your working directory. The files contain the jobID of the original job. Removing these will make you unable to restart your job! So make sure you do not!

If you submit additional jobs using DMTCP, you will create more and more of these files. DMTCP will always restart the most recent job but having so many files can still be confusing. If you wish to scrap a previous run, you can remove these unnecessary files with the following two commands, before starting a new job.

rm -f dmtcp_*
rm -fr ckpt_*

Note: If you have dmtcp_ or ckpt_ files that you wish to keep you must move/copy them somewhere else before running these commands. (Alternatively, you can specify the exact files to remove instead of using the wildcard *.)

Specifying checkpointing intervals

Don’t forget to specify checkpoint intervals on restart! In production operations, you would also want to pass an -i <number> option to dmtcp_restart, otherwise the application is merely restarted without checkpointing.

If you use a dmtcp_restart_script.sh that was generated from a dmtcp_launch -i <number> command, then the interval should be enabled in the generated dmtcp_restart_script.sh.

Alternatively, if you really don’t know when you want to checkpoint, you can checkpoint without specifying an interval if you know the hostname:

$ dmtcp_command --host <HOSTNAME> --checkpoint
Using a coordinator and command wrapper

Instead of invoking DMTCP directly, you can use the DMTCP coordinator program to hide the details of communicating with DMTCP directly. It is also useful for guaranteeing that when you restart your job, the working environment is exactly the same as your original job environment.

DMTCP Wrapper Script (Scavenge Partition)

[This example was modified to run on HPC.]

Instead of invoking DMTCP directly, you can run the DMTCP coordinator program to hide the details of communicating with DMTCP. If you do this from within your job script, your workflow will simplify to the following:

# Submit job
$ sbatch myjob.slurm
  : (job running)
  : (job preempted)

# Resubmit job
$ sbatch myjob.slurm
  : (job resumes)

The following job script runs count.pl for one hour on HPC’s scavenge partition. The counting is interrupted with the time expires to simulate preemption on the scavenge partition. A 20 minute checkpointing interval is used for testing.

Copy the lines below to a file named dmtcp_test.sl. The example assumes that count.pl resides in the same directory as dmtcp_test.sl. Type sbatch dmtcp_test.sl to submit the job and let it run until the time expires. If you are testing interactively, wait until after the checkpointing interval before interrupting the program with Ctrl-C (or “kill -SIGINT “, if running as a daemon).

#!/bin/bash

#SBATCH --export=none
#SBATCH --partition=scavenge
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=2
#SBATCH --mem-per-cpu=1g
#SBATCH --time=1:00:00     #one hour
#SBATCH --job-name="dmtcp_test"

###############################################################################
# Routine to start DMTCP coordinator on the launching node.  An available
# TCP port is automatically allocated.  A dmtcp_command.$JOBID script is
# created, which is a wrapper around the dmtcp_command.  The script runs
# dmtcp_command for the exact dmtcp_coordinator (its hostname and port).
# Instead of typing:
#    "dmtcp_command -h  -p  "
# you just type "./dmtcp_command.$JOBID " and talk to the coordinator
# for JOBID job.
# See https://redmine.hpc.rug.nl/redmine/projects/peregrine/wiki/DMTCP_Scripts
###############################################################################

start_coordinator()
{
    local hname port fname=dmtcp_command.$SLURM_JOBID

    # Run dmtcp_coodinator daemon on random port
    if ! dmtcp_coordinator --daemon --exit-on-last -p0 --port-file $fname "$@"
        then
        echo $0': dmtcp_coordinator error; aborting...'
        exit 1
    fi >&2

    # Get hostname
    if ! read hname < /proc/sys/kernel/hostname; then
        echo $0': cannot read hostname; aborting...'
        exit 1
    fi >&2

    # Get coordinator port
    read port < $fname || :
    if [ -z "${port:-}" ]; then
        echo $0': cannot read coordinator port; aborting...'
        exit 1
    fi >&2

    # Create dmtcp_command wrapper for easy communication with coordinator
    if chmod 0750 $fname; then
        echo '#!/bin/sh'
        echo
        echo 'PATH="'$PATH'"'
        echo 'export DMTCP_COORD_HOST='$hname
        echo 'export DMTCP_COORD_PORT='$port
        echo 'dmtcp_command "$@"'
    fi > $fname

    # Set up local environment for DMTCP
    export DMTCP_COORD_HOST=$hname
    export DMTCP_COORD_PORT=$port
}

# Set checkpoint interval
start_coordinator -i 1200  #checkpoint every 20 minutes

# The restart script is created by dmtcp_launch after initialization
if [ -x dmtcp_restart_script.sh ]; then
    bash dmtcp_restart_script.sh
else
    # On first pass, run program under DMTCP
    dmtcp_launch --rm perl count.pl	# must run interpreter for scripts
fi

The routine start_coordinator() is called before launching your program: start_coordinator starts a dmtcp_coordinator daemon and then creates a dmtcp_command wrapper containing host and port information. This frees you from having to invoke DMTCP functions directly.

Note: The “––ntasks=1” and “––cpus-per-task=2” options are used together to force the job onto one node and allocate 2 CPUs for processing.

DMTCP Wrapper Script Example (Scavenge Partition)

The following example was run on HPC’s scavenge partition using the job script above. Here, we go step-by-step through the process of submitting, checkpointing, and re-starting a job.

Job requirements

The example requires dmtcp_test.sl and count.pl.

$ ls
count.pl
dmtcp_test.sl

Two modifications were made to the job script above to shorten the testing time:

    1. The job time was shortened to five minutes

    #SBATCH --time=0:05:00     #five minutes
    
    2. The checkpointing interval was shortened to one minute

    start_coordinator -i 60  #checkpoint every 60 seconds
    
Submit job

First, submit the job as you normally would and check that the job has started.

$ sbatch dmtcp_test.sl
Submitted batch job 2489023

$ myqueue
JOBID    USER      ACCOUNT  PARTITION  NAME        TASKS  CPUS_PER_TASK  MIN_MEMORY  START_TIME           TIME  TIME_LIMIT  STATE    NODELIST(REASON)
2489023  ttrojan   lc_tt1   scavenge   dmtcp_test  1      2              1G          2019-01-04T10:52:15  0:29  5:00        RUNNING  hpc0681

Check that the dmtcp_command wrapper was created.

$ ls -F
count.pl
dmtcp_command.2489023*
dmtcp_test.sl
slurm-2489023.out

Check that the program is running (it takes a few minutes to populate program results).

$ cat slurm-2489023.out
---------- Begin SLURM Prolog ----------
Job ID:        2489023
Username:      ttrojan
Accountname:   lc_tt1
Name:          dmtcp_test
Partition:     scavenge
Nodelist:      hpc0681
TasksPerNode:  1
CPUsPerTask:   2
TMPDIR:        /tmp/2489023.scavenge
SCRATCHDIR:    /staging/scratch/2489023
Cluster:       uschpc
HSDA Account:  false
---------- 2019-01-04 10:52:20 ---------
dmtcp_coordinator starting...
    Host: hpc0681.hpcc.usc.edu (0.0.0.0)
    Port: 22056
    Checkpoint Interval: 60
    Exit on last client: 1
Backgrounding...
0 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

Check that checkpointing files were created after the checkpointing interval.

$ ls -F
ckpt_perl_1e78063feacc41a-40000-18770ce1679370.dmtcp
count.pl
dmtcp_command.2489023*
dmtcp_restart_script_1e78063feacc41a-40000-18770cdf79966d.sh*
dmtcp_restart_script.sh@
dmtcp_test.sl
slurm-2489023.out

When your job ends, view the output and check last count (295).

$ cat slurm-2489023.out
---------- Begin SLURM Prolog ----------
Job ID:        2489023
Username:      ttrojan
Accountname:   lc_tt1
Name:          dmtcp_test
Partition:     scavenge
Nodelist:      hpc0681
TasksPerNode:  1
CPUsPerTask:   2
TMPDIR:        /tmp/2489023.scavenge
SCRATCHDIR:    /staging/scratch/2489023
Cluster:       uschpc
HSDA Account:  false
---------- 2019-01-04 10:52:20 ---------
dmtcp_coordinator starting...
    Host: hpc0681.hpcc.usc.edu (0.0.0.0)
    Port: 22056
    Checkpoint Interval: 60
    Exit on last client: 1
Backgrounding...
0 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 95 96 97 98 99 100 101 102 103 104 
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 slurmstepd: 
error: *** JOB 2489023 ON hpc0681 CANCELLED AT 2019-01-04T10:57:37 DUE TO TIME LIMIT ***
[40000] ERROR at dmtcpmessagetypes.cpp:56 in assertValid; REASON='JASSERT(strcmp ( DMTCP_MAGIC_STRING,_magicBits ) == 0) failed'
     _magicBits = 
Message: read invalid message, _magicBits mismatch.  Did DMTCP coordinator die uncleanly?
perl (40000): Terminating...
Re-submit job

You can resubmit your job at any time. As usual, when the job is running, you can ssh to the compute node it is running on to monitor resources. In this example, a new slurm output file when the job starts and you can check results there.

$ sbatch dmtcp.sl
Submitted batch job 2489082

Check that the job has started.

$ myqueue
JOBID    USER      ACCOUNT  PARTITION  NAME        TASKS  CPUS_PER_TASK  MIN_MEMORY  START_TIME           TIME  TIME_LIMIT  STATE    NODELIST(REASON)
2489082  ttrojan   lc_tt1   scavenge   dmtcp_test  1      2              1G          2019-01-04T11:15:21  0:45  5:00        RUNNING  hpc0681

Check that new checkpointing files were created after the checkpointing interval.

$ ls -F
ckpt_perl_1e78063feacc41a-40000-18770ce1679370.dmtcp
count.pl
dmtcp_command.2489023*					     
dmtcp_command.2489082*
dmtcp_restart_script_1e78063feacc41a-40000-18770cdf79966d.sh*
dmtcp_restart_script.sh@  
dmtcp_test.sl
slurm-2489023.out
slurm-2489082.out

Check that your program is running and has started from most recent checkpoint (240).

$ cat slurm-2489082.out
---------- Begin SLURM Prolog ----------
Job ID:        2489082
Username:      ttrojan
Accountname:   lc_tt
Name:          dmtcp_test
Partition:     scavenge
Nodelist:      hpc0681
TasksPerNode:  1
CPUsPerTask:   2
TMPDIR:        /tmp/2489082.scavenge
SCRATCHDIR:    /staging/scratch/2489082
Cluster:       uschpc
HSDA Account:  false
---------- 2019-01-04 11:15:24 ---------
dmtcp_coordinator starting...
    Host: hpc0681.hpcc.usc.edu (0.0.0.0)
    Port: 28409
    Checkpoint Interval: 60
    Exit on last client: 1
Backgrounding...
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 srun: Job step aborted: Waiting up to 32 seconds for job step to 
finish.
slurmstepd: error: *** JOB 2489082 ON hpc0681 CANCELLED AT 2019-01-04T11:20:37 DUE TO TIME LIMIT ***
slurmstepd: error: *** STEP 2489082.0 ON hpc0681 CANCELLED AT 2019-01-04T11:20:37 DUE TO TIME LIMIT ***

Getting Help

For help with checkpointing, please contact hpc@usc.edu. For more information, see the DMTCP FAQ and presentation slides (2015).