3 # Copyright (c) 2007-2019. 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"
18 DEFAULT_SPEED="100flops"
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}"
26 PRIVATIZE="--cfg=smpi/privatization:${SMPI_PRIVATIZATION:-@HAVE_PRIVATIZATION@}"
28 SIMOPTS="--cfg=surf/precision:1e-9 --cfg=network/model:SMPI"
30 #usage to print the way this script should be called
33 Usage: $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> program [program-options]
34 $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> -replay <tracefile> [program] [program-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 -gdb # run within GDB (-wrapper "gdb --args" -keep-temps)
39 -lldb # run within LLDB (-wrapper "lldb --" -keep-temps)
40 -vgdb # run within Valgrind+GDB (-wrapper "valgrind --vgdb=yes --vgdb-error=0" -keep-temps)
41 -map # display the machine on which each process rank is mapped
42 -np <numprocs> # use that amount of processes from the hostfile.
43 # By default, all processes of the hostfile are used.
44 -no-privatize # Disable the globals privatization, that is activated by default
45 -trace-ti # activate time independant tracing (for replay, default in smpi_simgrid.txt)
46 -trace # activate tracing (Paje, default in smpi_simgrid.trace)
47 -trace-comment <comment> # put a comment on the top of the trace file
48 -trace-comment-file <file> # put file contents on the top of the trace file as comment
49 -trace-grouped # group MPI processes by location
50 -trace-resource # trace resource utilization
51 -trace-file <tracefile> # name of the tracefile (simgrid_smpi.trace)
52 -replay <tracefile> # replays a trace instead of actually executing an application
54 -version # Displays the SimGrid version (human readable)
55 -git-version # Displays the git hash of SimGrid
57 or (deprecated usage):
58 $0 [-keep-temps] [-np <numprocs>] [-bandwidth <bytes/sec>] [-latency <secs>] program [program-options]
63 #check if we have at least one parameter
78 trapped_signals="HUP INT QUIT ILL ABRT SEGV FPE ALRM TERM USR1 USR2 BUS"
81 printf '[%s] ** error: %s. Aborting.\n' "$(basename $0)" "$*" >&2
87 if [ -z "${KEEP}" ] ; then
88 if [ -z "${PLATFORM}" -a -n "$PLATFORMTMP" ]; then
92 if [ ${HOSTFILETMP} = 1 -a -n "$HOSTFILE" ] ; then
96 if [ ${UNROLLEDHOSTFILETMP} = 1 -a -n "$UNROLLEDHOSTFILE" ] ; then
97 rm -f ${UNROLLEDHOSTFILE}
100 if [ -n ${APPLICATIONTMP} ]; then
101 rm -f ${APPLICATIONTMP}
111 # Cleanup and kill the child process:
113 if ! [ -z "$pid" ]; then
118 # Raise the same signal again (remove the traps first):
119 trap - $trapped_signals
122 # This should never happen:
127 for s in $trapped_signals; do
128 trap "smpirun_trap $s" $s
138 NETWORK_BANDWIDTH="$2"
147 if [ ! -f "${PLATFORM}" ]; then
148 die "the file '${PLATFORM}' does not exist"
152 "-hostfile" | "-machinefile")
154 if [ ! -f "${HOSTFILE}" ]; then
155 die "the file '${HOSTFILE}' does not exist"
164 PRIVATIZE="--cfg=smpi/privatization:no"
177 TRACE_TI_ACTIVE="true"
184 "-trace-comment-file")
185 TRACE_COMMENT_FILE="$2"
197 TRACE_RESOURCE="true"
202 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
212 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
216 WRAPPER="valgrind --vgdb=yes --vgdb-error=0"
218 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
224 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
227 "-help" | "--help" | "-h")
231 "-version" | "--version" | "-v")
232 printf '%b\n' "$SIMGRID_VERSION"
235 "-git-version" | "--git-version")
236 printf '%b\n' "$SIMGRID_GITHASH"
242 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
247 # Nothing to do, compatibility.
256 if [ -n "${APP_TRACES}" ] ; then
257 if [ $# -eq 0 ] ; then
258 EXEC="@SMPIREPLAYMAIN@"
264 # check if we still have at least one parameter beyond options
267 echo "Error: no program to execute!"
276 # steel --cfg and --logs options
277 while [ $# -gt 0 ]; do
282 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
287 PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
293 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
294 echo "No hostfile nor platform specified."
299 if [ -z "${HOSTFILE}" ] ; then
301 HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
302 @PYTHON_EXECUTABLE@ -c '
303 import xml.etree.ElementTree as ET
307 tree = ET.parse(sys.stdin)
309 for elem in tree.findall(".//host"):
310 print(elem.attrib["id"])
312 for elem in tree.findall(".//cluster"):
313 prefix = elem.attrib["prefix"]
314 radical = elem.attrib["radical"]
315 suffix = elem.attrib["suffix"]
316 for r in radical.split(","):
317 m = re.match("^([^-]*?)-([^-]*)$", r)
319 for i in range(int(m.group(1)), int(m.group(2))):
320 print(prefix + str(i) + suffix)
322 print(prefix + r + suffix)
323 ' < ${PLATFORM} > ${HOSTFILE}
325 UNROLLEDHOSTFILETMP=0
327 # parse if our lines are terminated by :num_process
328 if grep -q ':' $HOSTFILE ; then
329 UNROLLEDHOSTFILETMP=1
330 UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
331 @PYTHON_EXECUTABLE@ -c '
335 for line in sys.stdin:
336 m = re.match("(.*):(.*)", line)
338 for i in range(0, int(m.group(2))):
342 ' < ${HOSTFILE} > ${UNROLLEDHOSTFILE}
343 if [ ${HOSTFILETMP} = 1 ] ; then
347 HOSTFILE=$UNROLLEDHOSTFILE
350 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
351 hostfile_procs=$(grep -c "[a-zA-Z0-9]" $HOSTFILE)
352 if [ ${hostfile_procs} = 0 ] ; then
353 die "the hostfile '${HOSTFILE}' is empty"
356 if [ -z "${NUMPROCS}" ] ; then
357 # Use the amount of processes in the hostfile as default value for the -np parameter
358 NUMPROCS=$hostfile_procs
361 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
362 echo "You requested to use ${NUMPROCS} ranks, but there is only ${hostfile_procs} processes in your hostfile..." >&2
365 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
366 if [ -z "${PLATFORM}" ]; then
367 PLATFORMTMP="$(mktemp smpitmp-platfXXXXXX)"
369 cat > ${PLATFORMTMP} <<PLATFORMHEAD
370 <?xml version='1.0'?>
371 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
372 <platform version="4.1">
373 <zone id="AS0" routing="Full">
377 while [ $i -gt 0 ]; do
378 echo " <host id=\"host$i\" speed=\"${SPEED}\"/>" >> ${PLATFORMTMP}
379 echo " <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
380 echo " <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
385 while [ $i -gt 0 ]; do
387 while [ $j -gt 0 ]; do
388 if [ $i -eq $j ]; then
389 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
391 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
398 cat >> ${PLATFORMTMP} <<PLATFORMFOOT
404 PLATFORMTMP=${PLATFORM}
406 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
407 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
408 APPLICATIONTMP="$(mktemp smpitmp-appXXXXXX)"
409 #APPLICATIONTMP="app.xml"
411 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
412 <?xml version='1.0'?>
413 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
414 <platform version="4.1">
417 ##---- cache hostnames of hostfile---------------
418 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
419 hostnames=$(cat ${HOSTFILE} | tr '\n\r' ' ')
422 if [ -n "${APP_TRACES}" ]; then
423 if [ -f "${APP_TRACES}" ]; then
424 hosttraces=$(cat ${APP_TRACES} | tr '\n\r' ' ' )
425 NUMTRACES=$(cat ${APP_TRACES} | wc -l)
428 printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
433 ##----------------------------------------------------------
434 ## generate application.xml with hostnames from hostfile:
435 ## the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if
436 ## hostfile has less than i lines.
437 ##----------------------------------------------------------
439 HAVE_SEQ="$(which seq 2>/dev/null)"
441 if [ -n "${HAVE_SEQ}" ]; then
442 SEQ=$(${HAVE_SEQ} 0 $(( NUMPROCS - 1)))
445 while [ $cnt -lt ${NUMPROCS} ] ; do
453 ##---- generate <actor> tags------------------------------
454 #prepare arguments at once
455 for ARG in $PROC_ARGS; do
456 XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
462 j=$(( i % hostfile_procs + 1 ))
463 host=$(eval "echo \${$j}")
465 ##---- optional display of ranks to actor mapping
466 if [ ${MAPOPT} = 1 ]; then
467 echo "[rank $i] -> $host"
470 echo " <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
471 <prop id=\"instance_id\" value=\"smpirun\"/>
472 <prop id=\"rank\" value=\"$i\"/>" >> ${APPLICATIONTMP}
473 if [ ${REPLAY} = 1 ]; then
474 echo " <prop id=\"smpi_replay\" value=\"true\"/>" >> ${APPLICATIONTMP}
475 if [ ${NUMTRACES} -gt 1 ]; then
476 echo " <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
478 echo " <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
481 echo ${XML_ARGS} >> ${APPLICATIONTMP}
483 echo " </actor>" >> ${APPLICATIONTMP}
486 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
489 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
490 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
491 if [ -n "${TRACE_ACTIVE}" ]; then
492 #define trace filename
493 if [ -n "${TRACE_TI_ACTIVE}" ]; then
494 if [ -z "${TRACE_FILENAME}" ]; then
495 TRACE_FILENAME="smpi_simgrid.txt"
497 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
499 if [ -z "${TRACE_FILENAME}" ]; then
500 TRACE_FILENAME="smpi_simgrid.trace"
502 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
505 if [ -n "${TRACE_COMMENT}" ]; then
506 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
509 if [ -n "${TRACE_COMMENT_FILE}" ]; then
510 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment-file:${TRACE_COMMENT_FILE}"
513 if [ -n "${TRACE_GROUPED}" ]; then
514 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
517 if [ -n "${TRACE_RESOURCE}" ]; then
518 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
521 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
523 # Do not remove, this variable may be used by user code (e.g. StarPU)
524 export SMPI_GLOBAL_SIZE=${NUMPROCS}
525 if [ -n "${KEEP}" ] ; then
526 echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
527 if [ ${HOSTFILETMP} = 1 ] ; then
528 echo "Generated hostfile ${HOSTFILE} kept."
530 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
531 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
535 # Execute the process
537 # The shell still need to be alive for the duration in order to do some cleanup after the process.
539 # We are going through great lengths in order to both keep stdin and be able to handle signals:
541 # * The job is launched in the background in order to be able to handle signals.
543 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
544 # is launched in the background: this can be overriden in bash but not in standard bourne shell.
546 ${WRAPPER} "@SMPIMAIN@" ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP} <&3 3>&- &
551 # With dash on Windows WSL/Ubuntu, "wait" sometimes returns early with an exit
552 # status of 128. Try again.
553 while test $status -eq 128 && kill -0 $pid 2>/dev/null; do
559 # Keep temporary files on failures to help debugging
561 if [ ${status} -ne 0 ] ; then
562 if [ -z "${KEEP}" ]; then
563 echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
564 if [ ${HOSTFILETMP} = 1 ] ; then
565 echo "Generated hostfile ${HOSTFILE} kept."
567 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
568 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
571 echo "Execution failed with code ${status}."