Logo AND Algorithmique Numérique Distribuée

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