Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Shell scripts: replace backquotes with $().
[simgrid.git] / src / smpi / smpirun.in
1 #! /bin/sh
2
3 # Copyright (c) 2007-2016, 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:@HAVE_PRIVATIZATION@"
27
28 SIMOPTS="--cfg=surf/precision:1e-9 --cfg=network/model:SMPI --cfg=network/TCP-gamma:4194304"
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 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-viva                # generate configuration for Viva's GraphView
48   -trace-file <tracefile>    # name of the tracefile (simgrid_smpi.trace)
49   -ext <value>               # additional parameter (reserved)
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 EXTOPT=""
68 WRAPPER=""
69 HOSTFILE=""
70 HOSTFILETMP=0
71 MAPOPT=0
72 REPLAY=0
73
74 unset pid
75
76 trapped_signals="HUP INT QUIT ILL ABRT SEGV FPE ALRM TERM USR1 USR2 BUS"
77
78 smpirun_cleanup()
79 {
80   if [ -z "${KEEP}" ] ; then
81       if [ -z "${PLATFORM}" -a -n "$PLATFORMTMP" ]; then
82         rm -f ${PLATFORMTMP}
83         PLATFORMTMP=""
84       fi
85       if [ ${HOSTFILETMP} = 1 -a -n "$HOSTFILE" ] ; then
86           rm -f ${HOSTFILE}
87           HOSTFILE=""
88       fi
89       if [ ${UNROLLEDHOSTFILETMP} = 1 -a -n "$UNROLLEDHOSTFILE" ] ; then
90           rm -f ${UNROLLEDHOSTFILE}
91           UNROLLEDHOSTFILE=""
92       fi
93       if [ -n ${APPLICATIONTMP} ]; then
94         rm -f ${APPLICATIONTMP}
95         APPLICATIONTMP=""
96       fi
97   fi
98 }
99
100 smpirun_trap() {
101   local sig
102   sig="$1"
103
104   # Cleanup and kill the child process:
105   smpirun_cleanup
106   if ! [ -z "$pid" ]; then
107     kill -TERM $pid
108   fi
109   unset pid
110
111   # Raise the same signal again (remove the traps first):
112   trap - $trapped_signals
113   kill -$sig $$
114
115   # This should never happen:
116   kill -ABRT $$
117   kill -TERM $$
118 }
119
120 for s in $trapped_signals; do
121   trap "smpirun_trap $s" $s
122 done
123
124 while true; do
125     case "$1" in
126         "-np" | "-n")
127             NUMPROCS="$2"
128             shift 2
129             ;;
130         "-bandwidth")
131             NETWORK_BANDWIDTH="$2"
132             shift 2
133             ;;
134         "-latency")
135             NETWORK_LATENCY="$2"
136             shift 2
137             ;;
138         "-platform")
139             PLATFORM="$2"
140             if [ ! -f "${PLATFORM}" ]; then
141                 echo "[`basename $0`] ** error: the file '${PLATFORM}' does not exist. Aborting."
142                 exit 1
143             fi
144             shift 2
145             ;;
146         "-hostfile")
147             HOSTFILE="$2"
148             if [ ! -f "${HOSTFILE}" ]; then
149                 echo "[`basename $0`] ** error: the file '${HOSTFILE}' does not exist. Aborting."
150                 exit 1
151             fi
152             shift 2
153             ;;
154         "-machinefile")
155             HOSTFILE="$2"
156             if [ ! -f "${HOSTFILE}" ]; then
157                 echo "[`basename $0`] ** error: the file '${HOSTFILE}' does not exist. Aborting."
158                 exit 1
159             fi
160             shift 2
161             ;;
162         "-ext")
163             EXTOPT="$2"
164             shift 2
165             ;;
166         "-no-privatize")
167             PRIVATIZE=""
168             shift 1
169             ;;
170         "-map")
171             MAPOPT=1
172             shift 1
173             ;;
174         "-trace")
175             TRACE_ACTIVE="true"
176             shift 1
177             ;;
178         "-trace-ti")
179             TRACE_ACTIVE="true"
180             TRACE_TI_ACTIVE="true"
181             shift 1
182             ;;
183         "-trace-comment")
184             TRACE_COMMENT="$2"
185             shift 2
186             ;;
187         "-trace-comment-file")
188             TRACE_COMMENT_FILE="$2"
189             shift 2
190             ;;
191         "-trace-file")
192             TRACE_FILENAME="$2"
193             shift 2
194             ;;
195         "-trace-grouped")
196             TRACE_GROUPED="true"
197             shift 1
198             ;;
199         "-trace-resource")
200             TRACE_RESOURCE="true"
201             shift 1
202             ;;
203         "-trace-viva")
204             TRACE_VIVA="true"
205             shift 1
206             ;;
207         "-keep-temps")
208             KEEP="true"
209             SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
210             shift 1
211             ;;
212         "-wrapper")
213             WRAPPER="$2"
214             shift 2
215             ;;
216         "-help" | "--help" | "-h")
217             usage
218             exit 0
219             ;;
220         "-version" | "--version" | "-v")
221             printf '%b\n' "$SIMGRID_VERSION"
222             exit 0
223             ;;
224         "-git-version" | "--git-version")
225             printf '%b\n' "$SIMGRID_GITHASH"
226             exit 0
227             ;;
228         "--cfg="*|"--log="*)
229             for OPT in ${1#*=}
230             do
231                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
232             done
233             shift 1
234             ;;
235         "-foreground")
236             # Nothing to do, compatibility.
237             shift 1
238             ;;
239         *)
240             break
241             ;;
242     esac
243 done
244
245 # check if we still have at least one parameter beyond options
246 if [ $# -eq 0 ]
247 then
248     echo "Error: no program to execute!"
249     usage
250     exit
251 fi
252
253 EXEC="$1"
254 shift
255
256 # steel --cfg and --logs options
257 while [ $# -gt 0 ]; do
258     case "$1" in
259         "--cfg="*|"--log="*)
260             for OPT in ${1#*=}
261             do
262                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
263             done
264             shift 1
265             ;;
266         *)
267             PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
268             shift      
269             ;;
270     esac
271 done
272
273 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
274     echo "No hostfile nor platform specified."
275     usage
276     exit 1
277 fi
278
279 if [ -z "${HOSTFILE}" ] ; then
280     HOSTFILETMP=1
281     HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
282     perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
283     perl -ne 'if (/.*<cluster.*?prefix="(.*?)".*?radical="(.*?)".*?suffix="(.*?)".*/) { 
284                 my ($pre,$rad,$post)=($1,$2,$3); 
285                 for my $elm (split(",",$rad)) { 
286                   if ($elm=~/^([^-]*?)-([^-]*)$/) { 
287                      for (my $i=$1; $i<=$2;$i++) { 
288                         print "$pre$i$post\n"; 
289                      }
290                   } else {
291                      print "$pre$elm$post\n";
292                   }
293                 }
294               } elsif (/<cluster/) {
295             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 and ON THE SAME LINE as the opening tag (<cluster)");
296               }' ${PLATFORM} >> ${HOSTFILE}
297 fi
298 UNROLLEDHOSTFILETMP=0
299
300 #parse if our lines are terminated by :num_process
301 multiple_processes=$(grep -c ":" $HOSTFILE)
302 if [ "${multiple_processes}" -gt 0 ] ; then
303     UNROLLEDHOSTFILETMP=1
304     UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
305     perl -ne ' do{ for ( 1 .. $2 ) { print "$1\n" } } if /(.*?):(\d+).*/'  ${HOSTFILE}  > ${UNROLLEDHOSTFILE}
306     if [ ${HOSTFILETMP} = 1 ] ; then
307         rm ${HOSTFILE}
308         HOSTFILETMP=0
309     fi
310     HOSTFILE=$UNROLLEDHOSTFILE
311 fi
312
313 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
314 hostfile_procs=$(grep -c "[a-zA-Z0-9]" $HOSTFILE)
315 if [ ${hostfile_procs} = 0 ] ; then
316    echo "[`basename $0`] ** error: the hostfile '${HOSTFILE}' is empty. Aborting." >&2
317    exit 1
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 "http://simgrid.gforge.inria.fr/simgrid/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 "http://simgrid.gforge.inria.fr/simgrid/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 [ "${EXTOPT}" = "smpi_replay" ]; then
387     APP_TRACES=$PROC_ARGS
388     if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
389         hosttraces=$(cat ${APP_TRACES} | tr '\n\r' '  ' )
390         NUMTRACES=$(cat ${APP_TRACES} | wc -l)
391         REPLAY=1
392     else
393         printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
394         exit 1
395     fi
396 fi
397
398 ##----------------------------------------------------------
399 ##  generate application.xml with hostnames from hostfile:
400 ##  the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if 
401 ##  hostfile has less than i lines.
402 ##----------------------------------------------------------
403
404 HAVE_SEQ="$(which seq 2>/dev/null)"
405
406 if [ -n "${HAVE_SEQ}" ]; then
407     SEQ=$(${HAVE_SEQ} 0 $((${NUMPROCS}-1)))
408 else
409     cnt=0
410     while [ $cnt -lt ${NUMPROCS} ] ; do
411         SEQ="$SEQ $cnt"
412         cnt=$((cnt + 1));
413     done
414 fi
415
416 set -- $hostnames
417
418 ##---- generate <actor> tags------------------------------
419 #prepare arguments at once
420 for ARG in $PROC_ARGS; do
421   XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
422 "
423 done
424
425 for i in ${SEQ}
426 do
427     j=$(( $i % ${hostfile_procs} + 1 ))
428     host=$(eval "echo \${$j}")
429
430     ##---- optional display of ranks to actor mapping
431     if [ ${MAPOPT} = 1 ]; then
432       echo "[rank $i] -> $host"
433     fi
434
435     echo "  <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
436     <argument value=\"1\"/> <!-- instance -->
437     <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
438     if [ ${REPLAY} = 1 ]; then
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
485     if [ -n "${TRACE_VIVA}" ]; then
486         TRACEOPTIONS="${TRACEOPTIONS} --cfg=viva/categorized:smpi_cat.plist --cfg=viva/uncategorized:smpi_uncat.plist"
487     fi
488 fi
489 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
490
491 export SMPI_GLOBAL_SIZE=${NUMPROCS}
492 if [ -n "${KEEP}" ] ; then
493     echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
494     if [ ${HOSTFILETMP} = 1 ] ; then
495         echo "Generated hostfile ${HOSTFILE} kept."
496     fi
497     if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
498         echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept." 
499     fi
500 fi
501
502 # Execute the process
503 #
504 # The shell still need to be alive for the duration in order to do some cleanup after the process.
505 #
506 # We are going through great lengths in order to both keep stdin and be able to handle signals:
507 #
508 # * The job is launched in the background in order to be able to handle signals.
509 #
510 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
511 #   is launched in the background: this can be overriden in bash but not in standard bourne shell.
512 exec 3<&0
513 ${WRAPPER} "@SMPIMAIN@" ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP} <&3 3>&- &
514 pid=$!
515 exec 3>&-
516 wait $pid
517 status=$?
518 pid=""
519
520 # Keep temporary files on failures to help debugging
521 #
522 if [ ${status} -ne 0 ] ; then
523     if [ -z ${KEEP} ]; then
524         echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
525         if [ ${HOSTFILETMP} = 1 ] ; then
526             echo "Generated hostfile ${HOSTFILE} kept."
527         fi
528         if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
529             echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
530         fi
531     fi
532     echo "Execution failed with code ${status}."
533     KEEP=true
534 fi
535
536 smpirun_cleanup
537
538 exit $status