Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi] replay from `smpirun -replay <tracefile>`
[simgrid.git] / src / smpi / smpirun.in
1 #!/usr/bin/env sh
2
3 # Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.
4
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the license (GNU LGPL) which comes with this package.
7
8 @CMAKE_SMPI_COMMAND@
9
10 SIMGRID_VERSION="@SIMGRID_VERSION_STRING@"
11 SIMGRID_GITHASH="@SIMGRID_GITHASH@"
12
13 DEFAULT_LOOPBACK_BANDWIDTH="498000000Bps"
14 DEFAULT_LOOPBACK_LATENCY="0.000004s"
15 DEFAULT_NETWORK_BANDWIDTH="$((26 * 1024 * 1024))Bps"
16 DEFAULT_NETWORK_LATENCY="0.000005s"
17 DEFAULT_NUMPROCS="4"
18 DEFAULT_SPEED="100flops"
19
20 LOOPBACK_BANDWIDTH="${DEFAULT_LOOPBACK_BANDWIDTH}"
21 LOOPBACK_LATENCY="${DEFAULT_LOOPBACK_LATENCY}"
22 NETWORK_BANDWIDTH="${DEFAULT_NETWORK_BANDWIDTH}"
23 NETWORK_LATENCY="${DEFAULT_NETWORK_LATENCY}"
24 SPEED="${DEFAULT_SPEED}"
25
26 PRIVATIZE="--cfg=smpi/privatization:${SMPI_PRIVATIZATION:-@HAVE_PRIVATIZATION@}"
27
28 SIMOPTS="--cfg=surf/precision:1e-9 --cfg=network/model:SMPI"
29
30 #usage to print the way this script should be called
31 usage () {
32     cat <<EOF
33 Usage: $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> program [program-options]
34        $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> -replay <tracefile> [program] [program-options]
35 Options:
36   -keep-temps                # don't remove the generated files after execution
37   -wrapper <command>         # use command to run the program (e.g. "valgrind" or "gdb --args")
38   -map                       # display the machine on which each process rank is mapped
39   -np <numprocs>             # use that amount of processes from the hostfile.
40                              # By default, all processes of the hostfile are used.
41   -no-privatize              # Disable the globals privatization, that is activated by default
42   -trace-ti                  # activate time independant tracing (for replay, default in smpi_simgrid.txt)
43   -trace                     # activate tracing (Paje, default in smpi_simgrid.trace)
44   -trace-comment <comment>   # put a comment on the top of the trace file
45   -trace-comment-file <file> # put file contents on the top of the trace file as comment
46   -trace-grouped             # group MPI processes by location
47   -trace-resource            # trace resource utilization
48   -trace-file <tracefile>    # name of the tracefile (simgrid_smpi.trace)
49   -replay <tracefile>        # replays a trace instead of actually executing an application
50
51   -version                   # Displays the SimGrid version (human readable)
52   -git-version               # Displays the git hash of SimGrid
53
54 or (deprecated usage):
55   $0 [-keep-temps] [-np <numprocs>] [-bandwidth <bytes/sec>] [-latency <secs>] program [program-options]
56
57 EOF
58 }
59
60 #check if we have at least one parameter
61 if [ $# -eq 0 ]
62 then
63     usage
64     exit
65 fi
66
67 WRAPPER=""
68 HOSTFILE=""
69 HOSTFILETMP=0
70 MAPOPT=0
71 REPLAY=0
72
73 unset pid
74
75 trapped_signals="HUP INT QUIT ILL ABRT SEGV FPE ALRM TERM USR1 USR2 BUS"
76
77 die () {
78     printf '[%s] ** error: %s. Aborting.\n' "$(basename $0)" "$*" >&2
79     exit 1
80 }
81
82 smpirun_cleanup()
83 {
84   if [ -z "${KEEP}" ] ; then
85       if [ -z "${PLATFORM}" -a -n "$PLATFORMTMP" ]; then
86         rm -f ${PLATFORMTMP}
87         PLATFORMTMP=""
88       fi
89       if [ ${HOSTFILETMP} = 1 -a -n "$HOSTFILE" ] ; then
90           rm -f ${HOSTFILE}
91           HOSTFILE=""
92       fi
93       if [ ${UNROLLEDHOSTFILETMP} = 1 -a -n "$UNROLLEDHOSTFILE" ] ; then
94           rm -f ${UNROLLEDHOSTFILE}
95           UNROLLEDHOSTFILE=""
96       fi
97       if [ -n ${APPLICATIONTMP} ]; then
98         rm -f ${APPLICATIONTMP}
99         APPLICATIONTMP=""
100       fi
101   fi
102 }
103
104 smpirun_trap() {
105   local sig
106   sig="$1"
107
108   # Cleanup and kill the child process:
109   smpirun_cleanup
110   if ! [ -z "$pid" ]; then
111     kill -TERM $pid
112   fi
113   unset pid
114
115   # Raise the same signal again (remove the traps first):
116   trap - $trapped_signals
117   kill -$sig $$
118
119   # This should never happen:
120   kill -ABRT $$
121   kill -TERM $$
122 }
123
124 for s in $trapped_signals; do
125   trap "smpirun_trap $s" $s
126 done
127
128 while true; do
129     case "$1" in
130         "-np" | "-n")
131             NUMPROCS="$2"
132             shift 2
133             ;;
134         "-bandwidth")
135             NETWORK_BANDWIDTH="$2"
136             shift 2
137             ;;
138         "-latency")
139             NETWORK_LATENCY="$2"
140             shift 2
141             ;;
142         "-platform")
143             PLATFORM="$2"
144             if [ ! -f "${PLATFORM}" ]; then
145                 die "the file '${PLATFORM}' does not exist"
146             fi
147             shift 2
148             ;;
149         "-hostfile" | "-machinefile")
150             HOSTFILE="$2"
151             if [ ! -f "${HOSTFILE}" ]; then
152                 die "the file '${HOSTFILE}' does not exist"
153             fi
154             shift 2
155             ;;
156         "-replay")
157             APP_TRACES="$2"
158             shift 2
159             ;;
160         "-no-privatize")
161             PRIVATIZE="--cfg=smpi/privatization:no"
162             shift 1
163             ;;
164         "-map")
165             MAPOPT=1
166             shift 1
167             ;;
168         "-trace")
169             TRACE_ACTIVE="true"
170             shift 1
171             ;;
172         "-trace-ti")
173             TRACE_ACTIVE="true"
174             TRACE_TI_ACTIVE="true"
175             shift 1
176             ;;
177         "-trace-comment")
178             TRACE_COMMENT="$2"
179             shift 2
180             ;;
181         "-trace-comment-file")
182             TRACE_COMMENT_FILE="$2"
183             shift 2
184             ;;
185         "-trace-file")
186             TRACE_FILENAME="$2"
187             shift 2
188             ;;
189         "-trace-grouped")
190             TRACE_GROUPED="true"
191             shift 1
192             ;;
193         "-trace-resource")
194             TRACE_RESOURCE="true"
195             shift 1
196             ;;
197         "-keep-temps")
198             KEEP="true"
199             SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
200             shift 1
201             ;;
202         "-wrapper")
203             WRAPPER="$2"
204             shift 2
205             ;;
206         "-help" | "--help" | "-h")
207             usage
208             exit 0
209             ;;
210         "-version" | "--version" | "-v")
211             printf '%b\n' "$SIMGRID_VERSION"
212             exit 0
213             ;;
214         "-git-version" | "--git-version")
215             printf '%b\n' "$SIMGRID_GITHASH"
216             exit 0
217             ;;
218         "--cfg="*|"--log="*)
219             for OPT in ${1#*=}
220             do
221                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
222             done
223             shift 1
224             ;;
225         "-foreground")
226             # Nothing to do, compatibility.
227             shift 1
228             ;;
229         *)
230             break
231             ;;
232     esac
233 done
234
235 if [ -n "${APP_TRACES}" ] ; then
236     if [ $# -eq 0 ] ; then
237         EXEC="@SMPIREPLAYMAIN@"
238     else
239         EXEC="$1"
240         shift
241     fi
242 else
243     # check if we still have at least one parameter beyond options
244     if [ $# -eq 0 ]
245     then
246         echo "Error: no program to execute!"
247         usage
248         exit
249     fi
250
251     EXEC="$1"
252     shift
253 fi
254
255 # steel --cfg and --logs options
256 while [ $# -gt 0 ]; do
257     case "$1" in
258         "--cfg="*|"--log="*)
259             for OPT in ${1#*=}
260             do
261                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
262             done
263             shift 1
264             ;;
265         *)
266             PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
267             shift
268             ;;
269     esac
270 done
271
272 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
273     echo "No hostfile nor platform specified."
274     usage
275     exit 1
276 fi
277
278 if [ -z "${HOSTFILE}" ] ; then
279     HOSTFILETMP=1
280     HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
281     perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
282     # put all <cluster tag on its own line.
283     cat ${PLATFORM} | tr '\n' ' ' | sed 's/<cluster/\n<cluster/' | \
284       perl -ne 'if (m/.*<cluster.*?prefix="(.*?)".*?radical="(.*?)".*?suffix="(.*?)".*/s) {
285                 my ($pre,$rad,$post)=($1,$2,$3);
286                 for my $elm (split(",",$rad)) {
287                   if ($elm=~/^([^-]*?)-([^-]*)$/) {
288                      for (my $i=$1; $i<=$2;$i++) {
289                         print "$pre$i$post\n";
290                      }
291                   } else {
292                      print "$pre$elm$post\n";
293                   }
294                 }
295               } elsif (/<cluster/) {
296             die ("Unparsable cluster tag. smpirun uses a primitive regular expression to parse cluster tags. Either provide a hostfile yourself or give the attributes prefix, radical and suffix IN THAT ORDER.");
297               }' ${PLATFORM} >> ${HOSTFILE}
298 fi
299 UNROLLEDHOSTFILETMP=0
300
301 #parse if our lines are terminated by :num_process
302 multiple_processes=$(grep -c ":" $HOSTFILE)
303 if [ "${multiple_processes}" -gt 0 ] ; then
304     UNROLLEDHOSTFILETMP=1
305     UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
306     perl -ne ' do{ for ( 1 .. $2 ) { print "$1\n" } } if /(.*?):(\d+).*/'  ${HOSTFILE}  > ${UNROLLEDHOSTFILE}
307     if [ ${HOSTFILETMP} = 1 ] ; then
308         rm ${HOSTFILE}
309         HOSTFILETMP=0
310     fi
311     HOSTFILE=$UNROLLEDHOSTFILE
312 fi
313
314 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
315 hostfile_procs=$(grep -c "[a-zA-Z0-9]" $HOSTFILE)
316 if [ ${hostfile_procs} = 0 ] ; then
317     die "the hostfile '${HOSTFILE}' is empty"
318 fi
319
320 if [ -z "${NUMPROCS}" ] ; then
321     # Use the amount of processes in the hostfile as default value for the -np parameter
322     NUMPROCS=$hostfile_procs
323 fi
324
325 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
326     echo "You requested to use ${NUMPROCS} ranks, but there is only ${hostfile_procs} processes in your hostfile..." >&2
327 fi
328
329 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
330 if [ -z "${PLATFORM}" ]; then
331     PLATFORMTMP="$(mktemp smpitmp-platfXXXXXX)"
332
333     cat > ${PLATFORMTMP} <<PLATFORMHEAD
334 <?xml version='1.0'?>
335 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
336 <platform version="4.1">
337 <zone id="AS0" routing="Full">
338 PLATFORMHEAD
339
340     i=${NUMPROCS}
341     while [ $i -gt 0 ]; do
342         echo "  <host id=\"host$i\" speed=\"${SPEED}\"/>" >> ${PLATFORMTMP}
343         echo "  <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
344         echo "  <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
345         i=$((i - 1))
346     done
347
348     i=${NUMPROCS}
349     while [ $i -gt 0 ]; do
350         j=${NUMPROCS}
351         while [ $j -gt 0 ]; do
352             if [ $i -eq $j ]; then
353                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
354             else
355                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
356             fi
357             j=$((j - 1))
358         done
359         i=$((i - 1))
360     done
361
362     cat >> ${PLATFORMTMP} <<PLATFORMFOOT
363 </zone>
364 </platform>
365 PLATFORMFOOT
366
367 else
368     PLATFORMTMP=${PLATFORM}
369 fi
370 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
371 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
372 APPLICATIONTMP="$(mktemp smpitmp-appXXXXXX)"
373 #APPLICATIONTMP="app.xml"
374
375 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
376 <?xml version='1.0'?>
377 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
378 <platform version="4.1">
379 APPLICATIONHEAD
380
381 ##---- cache hostnames of hostfile---------------
382 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
383     hostnames=$(cat ${HOSTFILE} | tr '\n\r' '  ')
384 fi
385
386 if [ -n "${APP_TRACES}" ]; then
387     if [ -f "${APP_TRACES}" ]; then
388         hosttraces=$(cat ${APP_TRACES} | tr '\n\r' '  ' )
389         NUMTRACES=$(cat ${APP_TRACES} | wc -l)
390         REPLAY=1
391     else
392         printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
393         exit 1
394     fi
395 fi
396
397 ##----------------------------------------------------------
398 ##  generate application.xml with hostnames from hostfile:
399 ##  the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if
400 ##  hostfile has less than i lines.
401 ##----------------------------------------------------------
402
403 HAVE_SEQ="$(which seq 2>/dev/null)"
404
405 if [ -n "${HAVE_SEQ}" ]; then
406     SEQ=$(${HAVE_SEQ} 0 $(( NUMPROCS - 1)))
407 else
408     cnt=0
409     while [ $cnt -lt ${NUMPROCS} ] ; do
410         SEQ="$SEQ $cnt"
411         cnt=$((cnt + 1));
412     done
413 fi
414
415 set -- $hostnames
416
417 ##---- generate <actor> tags------------------------------
418 #prepare arguments at once
419 for ARG in $PROC_ARGS; do
420   XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
421 "
422 done
423
424 for i in ${SEQ}
425 do
426     j=$(( i % hostfile_procs + 1 ))
427     host=$(eval "echo \${$j}")
428
429     ##---- optional display of ranks to actor mapping
430     if [ ${MAPOPT} = 1 ]; then
431       echo "[rank $i] -> $host"
432     fi
433
434     echo "  <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
435     <prop id=\"instance_id\" value=\"smpirun\"/>
436     <prop id=\"rank\" value=\"$i\"/>" >> ${APPLICATIONTMP}
437     if [ ${REPLAY} = 1 ]; then
438         echo "    <prop id=\"smpi_replay\" value=\"true\"/>" >> ${APPLICATIONTMP}
439         if  [ ${NUMTRACES} -gt 1 ]; then
440             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
441         else
442             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
443         fi
444     else
445     echo ${XML_ARGS} >> ${APPLICATIONTMP}
446     fi
447     echo "  </actor>" >> ${APPLICATIONTMP}
448 done
449
450 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
451 </platform>
452 APPLICATIONFOOT
453 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
454 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
455 if [ -n "${TRACE_ACTIVE}" ]; then
456     #define trace filename
457     if [ -n "${TRACE_TI_ACTIVE}" ]; then
458         if [ -z "${TRACE_FILENAME}" ]; then
459             TRACE_FILENAME="smpi_simgrid.txt"
460         fi
461         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
462     else
463         if [ -z "${TRACE_FILENAME}" ]; then
464             TRACE_FILENAME="smpi_simgrid.trace"
465         fi
466         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
467     fi
468
469     if [ -n "${TRACE_COMMENT}" ]; then
470         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
471     fi
472
473     if [ -n "${TRACE_COMMENT_FILE}" ]; then
474         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment-file:${TRACE_COMMENT_FILE}"
475     fi
476
477     if [ -n "${TRACE_GROUPED}" ]; then
478         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
479     fi
480
481     if [ -n "${TRACE_RESOURCE}" ]; then
482         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
483     fi
484 fi
485 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
486
487 export SMPI_GLOBAL_SIZE=${NUMPROCS}
488 if [ -n "${KEEP}" ] ; then
489     echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
490     if [ ${HOSTFILETMP} = 1 ] ; then
491         echo "Generated hostfile ${HOSTFILE} kept."
492     fi
493     if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
494         echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
495     fi
496 fi
497
498 # Execute the process
499 #
500 # The shell still need to be alive for the duration in order to do some cleanup after the process.
501 #
502 # We are going through great lengths in order to both keep stdin and be able to handle signals:
503 #
504 # * The job is launched in the background in order to be able to handle signals.
505 #
506 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
507 #   is launched in the background: this can be overriden in bash but not in standard bourne shell.
508 exec 3<&0
509 ${WRAPPER} "@SMPIMAIN@" ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP} <&3 3>&- &
510 pid=$!
511 exec 3>&-
512 wait $pid
513 status=$?
514 # With dash on Windows WSL/Ubuntu, "wait" sometimes returns early with an exit
515 # status of 128. Try again.
516 while test $status -eq 128 && kill -0 $pid 2>/dev/null; do
517     wait $pid
518     status=$?
519 done
520 pid=""
521
522 # Keep temporary files on failures to help debugging
523 #
524 if [ ${status} -ne 0 ] ; then
525     if [ -z "${KEEP}" ]; then
526         echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
527         if [ ${HOSTFILETMP} = 1 ] ; then
528             echo "Generated hostfile ${HOSTFILE} kept."
529         fi
530         if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
531             echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
532         fi
533     fi
534     echo "Execution failed with code ${status}."
535     KEEP=true
536 fi
537
538 smpirun_cleanup
539
540 exit $status