Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reindent
[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"
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 die () {
79     printf '[%s] ** error: %s. Aborting.\n' "$(basename $0)" "$*" >&2
80     exit 1
81 }
82
83 smpirun_cleanup()
84 {
85   if [ -z "${KEEP}" ] ; then
86       if [ -z "${PLATFORM}" -a -n "$PLATFORMTMP" ]; then
87         rm -f ${PLATFORMTMP}
88         PLATFORMTMP=""
89       fi
90       if [ ${HOSTFILETMP} = 1 -a -n "$HOSTFILE" ] ; then
91           rm -f ${HOSTFILE}
92           HOSTFILE=""
93       fi
94       if [ ${UNROLLEDHOSTFILETMP} = 1 -a -n "$UNROLLEDHOSTFILE" ] ; then
95           rm -f ${UNROLLEDHOSTFILE}
96           UNROLLEDHOSTFILE=""
97       fi
98       if [ -n ${APPLICATIONTMP} ]; then
99         rm -f ${APPLICATIONTMP}
100         APPLICATIONTMP=""
101       fi
102   fi
103 }
104
105 smpirun_trap() {
106   local sig
107   sig="$1"
108
109   # Cleanup and kill the child process:
110   smpirun_cleanup
111   if ! [ -z "$pid" ]; then
112     kill -TERM $pid
113   fi
114   unset pid
115
116   # Raise the same signal again (remove the traps first):
117   trap - $trapped_signals
118   kill -$sig $$
119
120   # This should never happen:
121   kill -ABRT $$
122   kill -TERM $$
123 }
124
125 for s in $trapped_signals; do
126   trap "smpirun_trap $s" $s
127 done
128
129 while true; do
130     case "$1" in
131         "-np" | "-n")
132             NUMPROCS="$2"
133             shift 2
134             ;;
135         "-bandwidth")
136             NETWORK_BANDWIDTH="$2"
137             shift 2
138             ;;
139         "-latency")
140             NETWORK_LATENCY="$2"
141             shift 2
142             ;;
143         "-platform")
144             PLATFORM="$2"
145             if [ ! -f "${PLATFORM}" ]; then
146                 die "the file '${PLATFORM}' does not exist"
147             fi
148             shift 2
149             ;;
150         "-hostfile")
151             HOSTFILE="$2"
152             if [ ! -f "${HOSTFILE}" ]; then
153                 die "the file '${HOSTFILE}' does not exist"
154             fi
155             shift 2
156             ;;
157         "-machinefile")
158             HOSTFILE="$2"
159             if [ ! -f "${HOSTFILE}" ]; then
160                 die "the file '${HOSTFILE}' does not exist"
161             fi
162             shift 2
163             ;;
164         "-ext")
165             EXTOPT="$2"
166             shift 2
167             ;;
168         "-no-privatize")
169             PRIVATIZE=""
170             shift 1
171             ;;
172         "-map")
173             MAPOPT=1
174             shift 1
175             ;;
176         "-trace")
177             TRACE_ACTIVE="true"
178             shift 1
179             ;;
180         "-trace-ti")
181             TRACE_ACTIVE="true"
182             TRACE_TI_ACTIVE="true"
183             shift 1
184             ;;
185         "-trace-comment")
186             TRACE_COMMENT="$2"
187             shift 2
188             ;;
189         "-trace-comment-file")
190             TRACE_COMMENT_FILE="$2"
191             shift 2
192             ;;
193         "-trace-file")
194             TRACE_FILENAME="$2"
195             shift 2
196             ;;
197         "-trace-grouped")
198             TRACE_GROUPED="true"
199             shift 1
200             ;;
201         "-trace-resource")
202             TRACE_RESOURCE="true"
203             shift 1
204             ;;
205         "-trace-viva")
206             TRACE_VIVA="true"
207             shift 1
208             ;;
209         "-keep-temps")
210             KEEP="true"
211             SIMOPTS="$SIMOPTS --cfg=smpi/keep-temps:yes"
212             shift 1
213             ;;
214         "-wrapper")
215             WRAPPER="$2"
216             shift 2
217             ;;
218         "-help" | "--help" | "-h")
219             usage
220             exit 0
221             ;;
222         "-version" | "--version" | "-v")
223             printf '%b\n' "$SIMGRID_VERSION"
224             exit 0
225             ;;
226         "-git-version" | "--git-version")
227             printf '%b\n' "$SIMGRID_GITHASH"
228             exit 0
229             ;;
230         "--cfg="*|"--log="*)
231             for OPT in ${1#*=}
232             do
233                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
234             done
235             shift 1
236             ;;
237         "-foreground")
238             # Nothing to do, compatibility.
239             shift 1
240             ;;
241         *)
242             break
243             ;;
244     esac
245 done
246
247 # check if we still have at least one parameter beyond options
248 if [ $# -eq 0 ]
249 then
250     echo "Error: no program to execute!"
251     usage
252     exit
253 fi
254
255 EXEC="$1"
256 shift
257
258 # steel --cfg and --logs options
259 while [ $# -gt 0 ]; do
260     case "$1" in
261         "--cfg="*|"--log="*)
262             for OPT in ${1#*=}
263             do
264                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
265             done
266             shift 1
267             ;;
268         *)
269             PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
270             shift      
271             ;;
272     esac
273 done
274
275 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
276     echo "No hostfile nor platform specified."
277     usage
278     exit 1
279 fi
280
281 if [ -z "${HOSTFILE}" ] ; then
282     HOSTFILETMP=1
283     HOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
284     perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
285     perl -ne 'if (/.*<cluster.*?prefix="(.*?)".*?radical="(.*?)".*?suffix="(.*?)".*/) { 
286                 my ($pre,$rad,$post)=($1,$2,$3); 
287                 for my $elm (split(",",$rad)) { 
288                   if ($elm=~/^([^-]*?)-([^-]*)$/) { 
289                      for (my $i=$1; $i<=$2;$i++) { 
290                         print "$pre$i$post\n"; 
291                      }
292                   } else {
293                      print "$pre$elm$post\n";
294                   }
295                 }
296               } elsif (/<cluster/) {
297             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)");
298               }' ${PLATFORM} >> ${HOSTFILE}
299 fi
300 UNROLLEDHOSTFILETMP=0
301
302 #parse if our lines are terminated by :num_process
303 multiple_processes=$(grep -c ":" $HOSTFILE)
304 if [ "${multiple_processes}" -gt 0 ] ; then
305     UNROLLEDHOSTFILETMP=1
306     UNROLLEDHOSTFILE="$(mktemp smpitmp-hostfXXXXXX)"
307     perl -ne ' do{ for ( 1 .. $2 ) { print "$1\n" } } if /(.*?):(\d+).*/'  ${HOSTFILE}  > ${UNROLLEDHOSTFILE}
308     if [ ${HOSTFILETMP} = 1 ] ; then
309         rm ${HOSTFILE}
310         HOSTFILETMP=0
311     fi
312     HOSTFILE=$UNROLLEDHOSTFILE
313 fi
314
315 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
316 hostfile_procs=$(grep -c "[a-zA-Z0-9]" $HOSTFILE)
317 if [ ${hostfile_procs} = 0 ] ; then
318     die "the hostfile '${HOSTFILE}' is empty"
319 fi
320
321 if [ -z "${NUMPROCS}" ] ; then
322     # Use the amount of processes in the hostfile as default value for the -np parameter
323     NUMPROCS=$hostfile_procs
324 fi
325
326 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
327     echo "You requested to use ${NUMPROCS} ranks, but there is only ${hostfile_procs} processes in your hostfile..." >&2
328 fi
329
330 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
331 if [ -z "${PLATFORM}" ]; then
332     PLATFORMTMP="$(mktemp smpitmp-platfXXXXXX)"
333
334     cat > ${PLATFORMTMP} <<PLATFORMHEAD
335 <?xml version='1.0'?>
336 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
337 <platform version="4.1">
338 <zone id="AS0" routing="Full">
339 PLATFORMHEAD
340
341     i=${NUMPROCS}
342     while [ $i -gt 0 ]; do
343         echo "  <host id=\"host$i\" speed=\"${SPEED}\"/>" >> ${PLATFORMTMP}
344         echo "  <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
345         echo "  <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
346         i=$((i - 1))
347     done
348
349     i=${NUMPROCS}
350     while [ $i -gt 0 ]; do
351         j=${NUMPROCS}
352         while [ $j -gt 0 ]; do
353             if [ $i -eq $j ]; then
354                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
355             else
356                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
357             fi
358             j=$((j - 1))
359         done
360         i=$((i - 1))
361     done
362
363     cat >> ${PLATFORMTMP} <<PLATFORMFOOT
364 </zone>
365 </platform>
366 PLATFORMFOOT
367
368 else
369     PLATFORMTMP=${PLATFORM}
370 fi
371 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
372 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
373 APPLICATIONTMP="$(mktemp smpitmp-appXXXXXX)"
374 #APPLICATIONTMP="app.xml"
375
376 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
377 <?xml version='1.0'?>
378 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
379 <platform version="4.1">
380 APPLICATIONHEAD
381
382 ##---- cache hostnames of hostfile---------------
383 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
384     hostnames=$(cat ${HOSTFILE} | tr '\n\r' '  ')
385 fi
386
387 if [ "${EXTOPT}" = "smpi_replay" ]; then
388     APP_TRACES=$PROC_ARGS
389     if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
390         hosttraces=$(cat ${APP_TRACES} | tr '\n\r' '  ' )
391         NUMTRACES=$(cat ${APP_TRACES} | wc -l)
392         REPLAY=1
393     else
394         printf "File not found: %s\n" "${APP_TRACES:-\${APP_TRACES\}}" >&2
395         exit 1
396     fi
397 fi
398
399 ##----------------------------------------------------------
400 ##  generate application.xml with hostnames from hostfile:
401 ##  the name of host_i (1<=i<=p, where -np p) is the line i in hostfile (where -hostfile hostfile), or "host$i" if 
402 ##  hostfile has less than i lines.
403 ##----------------------------------------------------------
404
405 HAVE_SEQ="$(which seq 2>/dev/null)"
406
407 if [ -n "${HAVE_SEQ}" ]; then
408     SEQ=$(${HAVE_SEQ} 0 $(( NUMPROCS - 1)))
409 else
410     cnt=0
411     while [ $cnt -lt ${NUMPROCS} ] ; do
412         SEQ="$SEQ $cnt"
413         cnt=$((cnt + 1));
414     done
415 fi
416
417 set -- $hostnames
418
419 ##---- generate <actor> tags------------------------------
420 #prepare arguments at once
421 for ARG in $PROC_ARGS; do
422   XML_ARGS="${XML_ARGS}""<argument value=\"${ARG}\"/>
423 "
424 done
425
426 for i in ${SEQ}
427 do
428     j=$(( i % hostfile_procs + 1 ))
429     host=$(eval "echo \${$j}")
430
431     ##---- optional display of ranks to actor mapping
432     if [ ${MAPOPT} = 1 ]; then
433       echo "[rank $i] -> $host"
434     fi
435
436     echo "  <actor host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->
437     <argument value=\"1\"/> <!-- instance -->
438     <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
439     if [ ${REPLAY} = 1 ]; then
440         if  [ ${NUMTRACES} -gt 1 ]; then
441             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
442         else
443             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
444         fi
445     else 
446     echo ${XML_ARGS} >> ${APPLICATIONTMP}
447     fi
448     echo "  </actor>" >> ${APPLICATIONTMP}
449 done
450
451 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
452 </platform>
453 APPLICATIONFOOT
454 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
455 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
456 if [ -n "${TRACE_ACTIVE}" ]; then
457     #define trace filename
458     if [ -n "${TRACE_TI_ACTIVE}" ]; then
459         if [ -z "${TRACE_FILENAME}" ]; then
460             TRACE_FILENAME="smpi_simgrid.txt"
461         fi
462         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
463     else
464         if [ -z "${TRACE_FILENAME}" ]; then
465             TRACE_FILENAME="smpi_simgrid.trace"
466         fi
467         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
468     fi
469
470     if [ -n "${TRACE_COMMENT}" ]; then
471         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
472     fi
473
474     if [ -n "${TRACE_COMMENT_FILE}" ]; then
475         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment-file:${TRACE_COMMENT_FILE}"
476     fi
477
478     if [ -n "${TRACE_GROUPED}" ]; then
479         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
480     fi
481
482     if [ -n "${TRACE_RESOURCE}" ]; then
483         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
484     fi
485
486     if [ -n "${TRACE_VIVA}" ]; then
487         TRACEOPTIONS="${TRACEOPTIONS} --cfg=viva/categorized:smpi_cat.plist --cfg=viva/uncategorized:smpi_uncat.plist"
488     fi
489 fi
490 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
491
492 export SMPI_GLOBAL_SIZE=${NUMPROCS}
493 if [ -n "${KEEP}" ] ; then
494     echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
495     if [ ${HOSTFILETMP} = 1 ] ; then
496         echo "Generated hostfile ${HOSTFILE} kept."
497     fi
498     if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
499         echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept." 
500     fi
501 fi
502
503 # Execute the process
504 #
505 # The shell still need to be alive for the duration in order to do some cleanup after the process.
506 #
507 # We are going through great lengths in order to both keep stdin and be able to handle signals:
508 #
509 # * The job is launched in the background in order to be able to handle signals.
510 #
511 # * The FD 3 is used to temporarily store FD 1. This is because the shell connects FD 1 to /dev/null when the command
512 #   is launched in the background: this can be overriden in bash but not in standard bourne shell.
513 exec 3<&0
514 ${WRAPPER} "@SMPIMAIN@" ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP} <&3 3>&- &
515 pid=$!
516 exec 3>&-
517 wait $pid
518 status=$?
519 pid=""
520
521 # Keep temporary files on failures to help debugging
522 #
523 if [ ${status} -ne 0 ] ; then
524     if [ -z "${KEEP}" ]; then
525         echo ${EXEC} ${PRIVATIZE} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
526         if [ ${HOSTFILETMP} = 1 ] ; then
527             echo "Generated hostfile ${HOSTFILE} kept."
528         fi
529         if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
530             echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} kept."
531         fi
532     fi
533     echo "Execution failed with code ${status}."
534     KEEP=true
535 fi
536
537 smpirun_cleanup
538
539 exit $status