3 # Copyright (c) 2007-2018. 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]
35 -keep-temps # don't remove the generated files after execution
36 -wrapper <command> # use command to run the program (e.g. "valgrind" or "gdb --args")
37 -map # display the machine on which each process rank is mapped
38 -np <numprocs> # use that amount of processes from the hostfile.
39 # By default, all processes of the hostfile are used.
40 -no-privatize # Disable the globals privatization, that is activated by default
41 -trace-ti # activate time independant tracing (for replay, default in smpi_simgrid.txt)
42 -trace # activate tracing (Paje, default in smpi_simgrid.trace)
43 -trace-comment <comment> # put a comment on the top of the trace file
44 -trace-comment-file <file> # put file contents on the top of the trace file as comment
45 -trace-grouped # group MPI processes by location
46 -trace-resource # trace resource utilization
47 -trace-file <tracefile> # name of the tracefile (simgrid_smpi.trace)
48 -ext <value> # additional parameter (reserved)
50 -version # Displays the SimGrid version (human readable)
51 -git-version # Displays the git hash of SimGrid
53 or (deprecated usage):
54 $0 [-keep-temps] [-np <numprocs>] [-bandwidth <bytes/sec>] [-latency <secs>] program [program-options]
59 #check if we have at least one parameter
75 trapped_signals="HUP INT QUIT ILL ABRT SEGV FPE ALRM TERM USR1 USR2 BUS"
78 printf '[%s] ** error: %s. Aborting.\n' "$(basename $0)" "$*" >&2
84 if [ -z "${KEEP}" ] ; then
85 if [ -z "${PLATFORM}" -a -n "$PLATFORMTMP" ]; then
89 if [ ${HOSTFILETMP} = 1 -a -n "$HOSTFILE" ] ; then
93 if [ ${UNROLLEDHOSTFILETMP} = 1 -a -n "$UNROLLEDHOSTFILE" ] ; then
94 rm -f ${UNROLLEDHOSTFILE}
97 if [ -n ${APPLICATIONTMP} ]; then
98 rm -f ${APPLICATIONTMP}
108 # Cleanup and kill the child process:
110 if ! [ -z "$pid" ]; then
115 # Raise the same signal again (remove the traps first):
116 trap - $trapped_signals
119 # This should never happen:
124 for s in $trapped_signals; do
125 trap "smpirun_trap $s" $s
135 NETWORK_BANDWIDTH="$2"
144 if [ ! -f "${PLATFORM}" ]; then
145 die "the file '${PLATFORM}' does not exist"
149 "-hostfile" | "-machinefile")
151 if [ ! -f "${HOSTFILE}" ]; then
152 die "the file '${HOSTFILE}' does not exist"
161 PRIVATIZE="--cfg=smpi/privatization:no"
174 TRACE_TI_ACTIVE="true"
181 "-trace-comment-file")
182 TRACE_COMMENT_FILE="$2"
194 TRACE_RESOURCE="true"
199 SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
206 "-help" | "--help" | "-h")
210 "-version" | "--version" | "-v")
211 printf '%b\n' "$SIMGRID_VERSION"
214 "-git-version" | "--git-version")
215 printf '%b\n' "$SIMGRID_GITHASH"
221 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
226 # Nothing to do, compatibility.
235 # check if we still have at least one parameter beyond options
238 echo "Error: no program to execute!"
246 # steel --cfg and --logs options
247 while [ $# -gt 0 ]; do
252 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
257 PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
263 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
264 echo "No hostfile nor platform specified."
269 if [ -z "${HOSTFILE}" ] ; then
271 HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
272 perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
273 # put all <cluster tag on its own line.
274 cat ${PLATFORM} | tr '\n' ' ' | sed 's/<cluster/\n<cluster/' | \
275 perl -ne 'if (m/.*<cluster.*?prefix="(.*?)".*?radical="(.*?)".*?suffix="(.*?)".*/s) {
276 my ($pre,$rad,$post)=($1,$2,$3);
277 for my $elm (split(",",$rad)) {
278 if ($elm=~/^([^-]*?)-([^-]*)$/) {
279 for (my $i=$1; $i<=$2;$i++) {
280 print "$pre$i$post\n";
283 print "$pre$elm$post\n";
286 } elsif (/<cluster/) {
287 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.");
288 }' ${PLATFORM} >> ${HOSTFILE}
290 UNROLLEDHOSTFILETMP=0
292 #parse if our lines are terminated by :num_process
293 multiple_processes=$(grep -c ":" $HOSTFILE)
294 if [ "${multiple_processes}" -gt 0 ] ; then
295 UNROLLEDHOSTFILETMP=1
296 UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
297 perl -ne ' do{ for ( 1 .. $2 ) { print "$1\n" } } if /(.*?):(\d+).*/' ${HOSTFILE} > ${UNROLLEDHOSTFILE}
298 if [ ${HOSTFILETMP} = 1 ] ; then
302 HOSTFILE=$UNROLLEDHOSTFILE
305 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
306 hostfile_procs=$(grep -c "[a-zA-Z0-9]" $HOSTFILE)
307 if [ ${hostfile_procs} = 0 ] ; then
308 die "the hostfile '${HOSTFILE}' is empty"
311 if [ -z "${NUMPROCS}" ] ; then
312 # Use the amount of processes in the hostfile as default value for the -np parameter
313 NUMPROCS=$hostfile_procs
316 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
317 echo "You requested to use ${NUMPROCS} ranks, but there is only ${hostfile_procs} processes in your hostfile..." >&2
320 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
321 if [ -z "${PLATFORM}" ]; then
322 PLATFORMTMP="$(mktemp smpitmp-platfXXXXXX)"
324 cat > ${PLATFORMTMP} <<PLATFORMHEAD
325 <?xml version='1.0'?>
326 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
327 <platform version="4.1">
328 <zone id="AS0" routing="Full">
332 while [ $i -gt 0 ]; do
333 echo " <host id=\"host$i\" speed=\"${SPEED}\"/>" >> ${PLATFORMTMP}
334 echo " <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
335 echo " <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
340 while [ $i -gt 0 ]; do
342 while [ $j -gt 0 ]; do
343 if [ $i -eq $j ]; then
344 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
346 echo " <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
353 cat >> ${PLATFORMTMP} <<PLATFORMFOOT
359 PLATFORMTMP=${PLATFORM}
361 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
362 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
363 APPLICATIONTMP="$(mktemp smpitmp-appXXXXXX)"
364 #APPLICATIONTMP="app.xml"
366 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
367 <?xml version='1.0'?>
368 <!DOCTYPE platform SYSTEM "https://simgrid.org/simgrid.dtd">
369 <platform version="4.1">
372 ##---- cache hostnames of hostfile---------------
373 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
374 hostnames=$(cat ${HOSTFILE} | tr '\n\r' ' ')
377 if [ "${EXTOPT}" = "smpi_replay" ]; then
378 APP_TRACES=$PROC_ARGS
379 if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
380 hosttraces=$(cat ${APP_TRACES} | tr '\n\r' ' ' )
381 NUMTRACES=$(cat ${APP_TRACES} | wc -l)
384 printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
389 ##----------------------------------------------------------
390 ## generate application.xml with hostnames from hostfile:
391 ## the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if
392 ## hostfile has less than i lines.
393 ##----------------------------------------------------------
395 HAVE_SEQ="$(which seq 2>/dev/null)"
397 if [ -n "${HAVE_SEQ}" ]; then
398 SEQ=$(${HAVE_SEQ} 0 $(( NUMPROCS - 1)))
401 while [ $cnt -lt ${NUMPROCS} ] ; do
409 ##---- generate <actor> tags------------------------------
410 #prepare arguments at once
411 for ARG in $PROC_ARGS; do
412 XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
418 j=$(( i % hostfile_procs + 1 ))
419 host=$(eval "echo \${$j}")
421 ##---- optional display of ranks to actor mapping
422 if [ ${MAPOPT} = 1 ]; then
423 echo "[rank $i] -> $host"
426 echo " <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
427 <argument value=\"smpirun\"/> <!-- instance -->
428 <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
429 if [ ${REPLAY} = 1 ]; then
430 if [ ${NUMTRACES} -gt 1 ]; then
431 echo " <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
433 echo " <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
436 echo ${XML_ARGS} >> ${APPLICATIONTMP}
438 echo " </actor>" >> ${APPLICATIONTMP}
441 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
444 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
445 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
446 if [ -n "${TRACE_ACTIVE}" ]; then
447 #define trace filename
448 if [ -n "${TRACE_TI_ACTIVE}" ]; then
449 if [ -z "${TRACE_FILENAME}" ]; then
450 TRACE_FILENAME="smpi_simgrid.txt"
452 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
454 if [ -z "${TRACE_FILENAME}" ]; then
455 TRACE_FILENAME="smpi_simgrid.trace"
457 TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
460 if [ -n "${TRACE_COMMENT}" ]; then
461 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
464 if [ -n "${TRACE_COMMENT_FILE}" ]; then
465 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment-file:${TRACE_COMMENT_FILE}"
468 if [ -n "${TRACE_GROUPED}" ]; then
469 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
472 if [ -n "${TRACE_RESOURCE}" ]; then
473 TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
476 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
478 export SMPI_GLOBAL_SIZE=${NUMPROCS}
479 if [ -n "${KEEP}" ] ; then
480 echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
481 if [ ${HOSTFILETMP} = 1 ] ; then
482 echo "Generated hostfile ${HOSTFILE} kept."
484 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
485 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
489 # Execute the process
491 # The shell still need to be alive for the duration in order to do some cleanup after the process.
493 # We are going through great lengths in order to both keep stdin and be able to handle signals:
495 # * The job is launched in the background in order to be able to handle signals.
497 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
498 # is launched in the background: this can be overriden in bash but not in standard bourne shell.
500 ${WRAPPER} "@SMPIMAIN@" ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP} <&3 3>&- &
505 # With dash on Windows WSL/Ubuntu, "wait" sometimes returns early with an exit
506 # status of 128. Try again.
507 while test $status -eq 128 && kill -0 $pid 2>/dev/null; do
513 # Keep temporary files on failures to help debugging
515 if [ ${status} -ne 0 ] ; then
516 if [ -z "${KEEP}" ]; then
517 echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
518 if [ ${HOSTFILETMP} = 1 ] ; then
519 echo "Generated hostfile ${HOSTFILE} kept."
521 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
522 echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
525 echo "Execution failed with code ${status}."