Logo AND Algorithmique Numérique Distribuée

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