Logo AND Algorithmique Numérique Distribuée

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