3 # Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.
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.
10 SIMGRID_VERSION="@SIMGRID_VERSION_STRING@"
11 SIMGRID_GITHASH="@SIMGRID_GITHASH@"
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_SPEED="100flops"
19 LOOPBACK_BANDWIDTH="${DEFAULT_LOOPBACK_BANDWIDTH}"
20 LOOPBACK_LATENCY="${DEFAULT_LOOPBACK_LATENCY}"
21 NETWORK_BANDWIDTH="${DEFAULT_NETWORK_BANDWIDTH}"
22 NETWORK_LATENCY="${DEFAULT_NETWORK_LATENCY}"
23 SPEED="${DEFAULT_SPEED}"
25 PRIVATIZE="--cfg=smpi/privatization:${SMPI_PRIVATIZATION:-@HAVE_PRIVATIZATION@}"
27 SIMOPTS="--cfg=surf/precision:1e-9 --cfg=network/model:SMPI"
29 SMPITMPDIR="$(dirname $(mktemp -u))"
31 #usage to print the way this script should be called
34 Usage: $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> program [program-options]
35 $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> -replay <tracefile> [program] [program-options]
37 -keep-temps # don't remove the generated files after execution
38 -wrapper <command> # use command to run the program (e.g. "valgrind" or "gdb --args")
39 -gdb # run within GDB (-wrapper "gdb --args" -keep-temps)
40 -lldb # run within LLDB (-wrapper "lldb --" -keep-temps)
41 -vgdb # run within Valgrind+GDB (-wrapper "valgrind --vgdb=yes --vgdb-error=0" -keep-temps)
42 -map # display the machine on which each process rank is mapped
43 -np <numprocs> # use that amount of processes from the hostfile.
44 # By default, all processes of the hostfile are used.
45 -no-privatize # Disable the globals privatization, that is activated by default
46 -tmpdir # Directory used to store temporary files. Defaults to system's.
47 -trace-ti # activate time independent tracing (for replay, default in smpi_simgrid.txt)
48 -trace # activate tracing (Paje, default in smpi_simgrid.trace)
49 -trace-comment <comment> # put a comment on the top of the trace file
50 -trace-comment-file <file> # put file contents on the top of the trace file as comment
51 -trace-grouped # group MPI processes by location
52 -trace-resource # trace resource utilization
53 -trace-file <tracefile> # name of the tracefile (simgrid_smpi.trace)
54 -replay <tracefile> # replays a trace instead of actually executing an application
56 -version # Displays the SimGrid version (human readable)
57 -git-version # Displays the git hash of SimGrid
59 or (deprecated usage):
60 $0 [-keep-temps] [-np <numprocs>] [-bandwidth <bytes/sec>] [-latency <secs>] program [program-options]
65 #check if we have at least one parameter
81 trapped_signals="HUP INT QUIT ILL ABRT SEGV FPE ALRM TERM USR1 USR2 BUS"
84 printf '[%s] ** error: %s. Aborting.\n' "$(basename "$0")" "$*" >&2
90 if [ -z "${KEEP}" ] ; then
91 if [ -z "${PLATFORM}" ] && [ -n "$PLATFORMTMP" ]; then
92 rm -f "${PLATFORMTMP}"
95 if [ ${HOSTFILETMP} = 1 ] && [ -n "$HOSTFILE" ] ; then
99 if [ "${UNROLLEDHOSTFILETMP}" = 1 ] && [ -n "$UNROLLEDHOSTFILE" ] ; then
100 rm -f "${UNROLLEDHOSTFILE}"
103 if [ -n "${APPLICATIONTMP}" ]; then
104 rm -f "${APPLICATIONTMP}"
114 # Cleanup and kill the child process:
116 if [ -n "$pid" ]; then
121 # Raise the same signal again (remove the traps first):
122 trap - $trapped_signals
125 # This should never happen:
130 for s in $trapped_signals; do
131 trap "smpirun_trap $s" "$s"
141 NETWORK_BANDWIDTH="$2"
150 if [ ! -f "${PLATFORM}" ]; then
151 die "the file '${PLATFORM}' does not exist"
155 "-hostfile" | "-machinefile")
157 if [ ! -f "${HOSTFILE}" ]; then
158 die "the file '${HOSTFILE}' does not exist"
167 PRIVATIZE="--cfg=smpi/privatization:no"
184 TRACE_TI_ACTIVE="true"
191 "-trace-comment-file")
192 TRACE_COMMENT_FILE="$2"
204 TRACE_RESOURCE="true"
209 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
223 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
227 WRAPPER="valgrind --vgdb=yes --vgdb-error=0"
229 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
235 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
238 "-help" | "--help" | "-h")
242 "-version" | "--version" | "-v")
243 printf '%b\n' "$SIMGRID_VERSION"
246 "-git-version" | "--git-version")
247 printf '%b\n' "$SIMGRID_GITHASH"
253 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
258 # Nothing to do, compatibility.
268 SIMOPTS="$SIMOPTS --cfg=smpi/tmpdir:$SMPITMPDIR"
269 export LD_LIBRARY_PATH="$SMPITMPDIR:$LD_LIBRARY_PATH"
271 if [ -n "${APP_TRACES}" ] ; then
272 if [ $# -eq 0 ] ; then
273 EXEC="@SMPIREPLAYMAIN@"
279 # check if we still have at least one parameter beyond options
282 echo "Error: no program to execute!"
291 # steal --cfg and --logs options
292 while [ $# -gt 0 ]; do
297 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
302 PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
308 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
309 echo "No hostfile nor platform specified."
314 if [ -z "${HOSTFILE}" ] ; then
316 HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
317 @PYTHON_EXECUTABLE@ -c '
318 import xml.etree.ElementTree as ET
322 tree = ET.parse(sys.stdin)
324 for elem in tree.findall(".//host"):
325 print(elem.attrib["id"])
327 for elem in tree.findall(".//cluster"):
328 prefix = elem.attrib["prefix"]
329 radical = elem.attrib["radical"]
330 suffix = elem.attrib["suffix"]
331 for r in radical.split(","):
332 m = re.match("^([^-]*?)-([^-]*)$", r)
334 for i in range(int(m.group(1)), int(m.group(2))):
335 print(prefix + str(i) + suffix)
337 print(prefix + r + suffix)
338 ' < "${PLATFORM}" > "${HOSTFILE}"
340 UNROLLEDHOSTFILETMP=0
342 # parse if our lines are terminated by :num_process
343 if grep -q ':' "$HOSTFILE" ; then
344 UNROLLEDHOSTFILETMP=1
345 UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
346 @PYTHON_EXECUTABLE@ -c '
350 for line in sys.stdin:
351 m = re.match("(.*):(.*)", line)
353 for i in range(0, int(m.group(2))):
357 ' < "${HOSTFILE}" > "${UNROLLEDHOSTFILE}"
358 if [ ${HOSTFILETMP} = 1 ] ; then
362 HOSTFILE=$UNROLLEDHOSTFILE
365 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
366 hostfile_procs=$(grep -c "[a-zA-Z0-9]" "$HOSTFILE")
367 if [ "${hostfile_procs}" = 0 ] ; then
368 die "the hostfile '${HOSTFILE}' is empty"
371 if [ -z "${NUMPROCS}" ] ; then
372 # Use the amount of processes in the hostfile as default value for the -np parameter
373 NUMPROCS=$hostfile_procs
376 if [ "${NUMPROCS}" -gt "${hostfile_procs}" ] ; then
377 echo "You requested to use ${NUMPROCS} ranks, but there is only ${hostfile_procs} processes in your hostfile..." >&2
380 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
381 if [ -z "${PLATFORM}" ]; then
382 PLATFORMTMP="$(mktemp smpitmp-platfXXXXXX)"
384 cat > "${PLATFORMTMP}" <<PLATFORMHEAD
385 <?xml version='1.0'?>
386 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
387 <platform version="4.1">
388 <zone id="AS0" routing="Full">
392 while [ "$i" -gt 0 ]; do
394 echo " <host id=\"host$i\" speed=\"${SPEED}\"/>"
395 echo " <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>"
396 echo " <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>"
397 } >> "${PLATFORMTMP}"
402 while [ "$i" -gt 0 ]; do
404 while [ "$j" -gt 0 ]; do
405 if [ "$i" -eq "$j" ]; then
406 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> "${PLATFORMTMP}"
408 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> "${PLATFORMTMP}"
415 cat >> "${PLATFORMTMP}" <<PLATFORMFOOT
421 PLATFORMTMP=${PLATFORM}
423 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
424 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
425 APPLICATIONTMP="$(mktemp smpitmp-appXXXXXX)"
426 #APPLICATIONTMP="app.xml"
428 cat > "${APPLICATIONTMP}" <<APPLICATIONHEAD
429 <?xml version='1.0'?>
430 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
431 <platform version="4.1">
434 ##---- cache hostnames of hostfile---------------
435 if [ -n "${HOSTFILE}" ] && [ -f "${HOSTFILE}" ]; then
436 hostnames=$(< "${HOSTFILE}" tr '\n\r' ' ')
439 if [ -n "${APP_TRACES}" ]; then
440 if [ -f "${APP_TRACES}" ]; then
441 hosttraces=$(< "${APP_TRACES}" tr '\n\r' ' ' )
442 NUMTRACES=$(< "${APP_TRACES}" wc -l)
445 printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
450 ##----------------------------------------------------------
451 ## generate application.xml with hostnames from hostfile:
452 ## the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if
453 ## hostfile has less than i lines.
454 ##----------------------------------------------------------
456 HAVE_SEQ="$(which seq 2>/dev/null)"
458 if [ -n "${HAVE_SEQ}" ]; then
459 SEQ=$(${HAVE_SEQ} 0 $(( NUMPROCS - 1)))
462 while [ $cnt -lt "${NUMPROCS}" ] ; do
470 ##---- generate <actor> tags------------------------------
471 #prepare arguments at once
472 for ARG in $PROC_ARGS; do
473 XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
479 j=$(( i % hostfile_procs + 1 ))
480 host=$(eval "echo \${$j}")
482 ##---- optional display of ranks to actor mapping
483 if [ ${MAPOPT} = 1 ]; then
484 echo "[rank $i] -> $host"
487 echo " <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
488 <prop id=\"instance_id\" value=\"smpirun\"/>
489 <prop id=\"rank\" value=\"$i\"/>"
490 if [ ${REPLAY} = 1 ]; then
491 echo " <prop id=\"smpi_replay\" value=\"true\"/>"
492 if [ "${NUMTRACES}" -gt 1 ]; then
493 echo " <argument value=\"$(echo "$hosttraces"|cut -d' ' -f$j)\"/>"
495 echo " <argument value=\"$(echo "$hosttraces"|cut -d' ' -f1)\"/>"
501 } >> "${APPLICATIONTMP}"
504 cat >> "${APPLICATIONTMP}" <<APPLICATIONFOOT
507 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
508 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
509 if [ -n "${TRACE_ACTIVE}" ]; then
510 #define trace filename
511 if [ -n "${TRACE_TI_ACTIVE}" ]; then
512 if [ -z "${TRACE_FILENAME}" ]; then
513 TRACE_FILENAME="smpi_simgrid.txt"
515 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
517 if [ -z "${TRACE_FILENAME}" ]; then
518 TRACE_FILENAME="smpi_simgrid.trace"
520 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
523 if [ -n "${TRACE_COMMENT}" ]; then
524 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
527 if [ -n "${TRACE_COMMENT_FILE}" ]; then
528 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment-file:${TRACE_COMMENT_FILE}"
531 if [ -n "${TRACE_GROUPED}" ]; then
532 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
535 if [ -n "${TRACE_RESOURCE}" ]; then
536 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
539 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
541 # Do not remove, this variable may be used by user code (e.g. StarPU)
542 export SMPI_GLOBAL_SIZE=${NUMPROCS}
543 if [ -n "${KEEP}" ] && [ -z "${QUIET}" ] ; then
544 echo "${EXEC}" ${PRIVATIZE} "${TRACEOPTIONS}" "${SIMOPTS}" "${PLATFORMTMP}" "${APPLICATIONTMP}"
545 if [ ${HOSTFILETMP} = 1 ] ; then
546 echo "Generated hostfile ${HOSTFILE} kept."
548 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
549 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
553 # Execute the process
555 # The shell still need to be alive for the duration in order to do some cleanup after the process.
557 # We are going through great lengths in order to both keep stdin and be able to handle signals:
559 # * The job is launched in the background in order to be able to handle signals.
561 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
562 # is launched in the background: this can be overridden in bash but not in standard bourne shell.
564 ${WRAPPER} "@SMPIMAIN@" "${EXEC}" ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} "${PLATFORMTMP}" "${APPLICATIONTMP}" <&3 3>&- &
569 # With dash on Windows WSL/Ubuntu, "wait" sometimes returns early with an exit
570 # status of 128. Try again.
571 while test $status -eq 128 && kill -0 $pid 2>/dev/null; do
577 # Keep temporary files on failures to help debugging
579 if [ ${status} -ne 0 ] ; then
580 if [ -z "${KEEP}" ] && [ -z "${QUIET}" ]; then
581 echo "${EXEC}" ${PRIVATIZE} "${TRACEOPTIONS}" "${SIMOPTS}" "${PLATFORMTMP}" "${APPLICATIONTMP}"
582 if [ ${HOSTFILETMP} = 1 ] ; then
583 echo "Generated hostfile ${HOSTFILE} kept."
585 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
586 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
590 echo "Execution failed with code ${status}."