Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 "[$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 "[$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 "[$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
248 if [ -z "${NUMPROCS}" ] ; then
249     # Use the amount of processes in the hostfile as default value for the -np parameter
250     NUMPROCS=$hostfile_procs
251 fi
252
253
254 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
255     echo "You requested to use ${NUMPROCS} processes, but there is only ${hostfile_procs} processes in your hostfile..." >&2
256 fi
257
258 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
259 if [ -z "${PLATFORM}" ]; then
260     PLATFORMTMP="$(mktemp tmpXXXXXX)"
261
262     cat > ${PLATFORMTMP} <<PLATFORMHEAD
263 <?xml version='1.0'?>
264 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
265 <platform version="3">
266 <AS id="AS0" routing="Full">
267 PLATFORMHEAD
268
269     i=${NUMPROCS}
270     while [ $i -gt 0 ]; do
271         echo "  <host id=\"host$i\" power=\"${POWER}\"/>" >> ${PLATFORMTMP}
272         echo "  <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
273         echo "  <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
274         i=$((i - 1))
275     done
276
277     i=${NUMPROCS}
278     while [ $i -gt 0 ]; do
279         j=${NUMPROCS}
280         while [ $j -gt 0 ]; do
281             if [ $i -eq $j ]; then
282                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
283             else
284                 echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
285             fi
286             j=$((j - 1))
287         done
288         i=$((i - 1))
289     done
290
291     cat >> ${PLATFORMTMP} <<PLATFORMFOOT
292 </AS>
293 </platform>
294 PLATFORMFOOT
295
296 else
297     PLATFORMTMP=${PLATFORM}
298 fi
299 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
300
301 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
302 APPLICATIONTMP="$(mktemp tmpXXXXXX)"
303 #APPLICATIONTMP="app.xml"
304
305
306 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
307 <?xml version='1.0'?>
308 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
309 <platform version="3">
310 APPLICATIONHEAD
311
312 ##---- cache hostnames of hostfile---------------
313 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
314     hostnames=$(cat ${HOSTFILE} | tr '\n\r' '  ')
315     NUMHOSTS=$(cat ${HOSTFILE} | wc -l)
316 fi
317
318 if [ "${EXTOPT}" = "smpi_replay" ]; then
319     APP_TRACES=$PROC_ARGS
320     if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
321         hosttraces=$(cat ${APP_TRACES} | tr '\n\r' '  ' )
322         NUMTRACES=$(cat ${APP_TRACES} | wc -l)
323     else
324         printf "File not found: %s\n", "${APP_TRACES:-\${APP_TRACES\}}" >&2
325         exit 1
326     fi
327 fi
328
329 ##----------------------------------------------------------
330 ##  generate application.xml with hostnames from hostfile:
331 ##  the name of host_i (1<=i<=p, where -np p) is the line i
332 ##  in hostfile (where -hostfile hostfile), or "host$i" if
333 ##  hostfile has less than i lines.
334 ##----------------------------------------------------------
335
336 HAVE_SEQ="`which seq 2>/dev/null`"
337
338 if [ -n "${HAVE_SEQ}" ]; then
339     SEQ=`${HAVE_SEQ} 0 $((${NUMPROCS}-1))`
340 else
341     cnt=0
342     while (( $cnt < ${NUMPROCS} )) ; do
343         SEQ="$SEQ $cnt"
344         cnt=$((cnt + 1));
345     done
346 fi
347
348 ##---- generate <process> tags------------------------------
349
350 for i in ${SEQ}
351 do
352     if [ -n "${HOSTFILE}" ]; then
353         j=$(( $i % ${NUMHOSTS} + 1 ))
354     fi
355     ##---- optional display of ranks to process mapping
356     if [ -n "${MAPOPT}" ]; then
357         echo "[rank $i] -> $(echo $hostnames|cut -d' ' -f$j)"
358     fi
359
360     if [ -z "$(echo $hostnames|cut -d' ' -f$j)" ]; then
361         host="host"$($j)
362     else
363         host="$(echo $hostnames|cut -d' ' -f$j)"
364     fi
365     echo "  <process host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->" >> ${APPLICATIONTMP}
366     echo "    <argument value=\"1\"/> <!-- instance -->" >> ${APPLICATIONTMP}
367     echo "    <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
368     if [ "${EXTOPT}" = "smpi_replay" ]; then
369         if  [ ${NUMTRACES} -gt 1 ]; then
370             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
371         else
372             echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
373         fi
374     else 
375         for ARG in $PROC_ARGS; do
376             echo "    <argument value=\"${ARG}\"/>" >> ${APPLICATIONTMP}
377         done
378     fi
379     echo "  </process>" >> ${APPLICATIONTMP}
380 done
381
382 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
383 </platform>
384 APPLICATIONFOOT
385 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
386
387 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
388 if [ -n "${TRACE_ACTIVE}" ]; then
389     #define trace filename
390     if [ -n "${TRACE_TI_ACTIVE}" ]; then
391         if [ -z "${TRACE_FILENAME}" ]; then
392             TRACE_FILENAME="smpi_simgrid.txt"
393         fi
394         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
395     else
396         if [ -z "${TRACE_FILENAME}" ]; then
397             TRACE_FILENAME="smpi_simgrid.trace"
398         fi
399         TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
400     fi
401
402     if [ -n "${TRACE_COMMENT}" ]; then
403         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
404     fi
405
406     if [ -n "${TRACE_COMMENT_FILE}" ]; then
407         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment_file:${TRACE_COMMENT_FILE}"
408     fi
409
410     if [ -n "${TRACE_GROUPED}" ]; then
411         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
412     fi
413
414     if [ -n "${TRACE_RESOURCE}" ]; then
415         TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
416     fi
417
418     if [ -n "${TRACE_VIVA}" ]; then
419         TRACEOPTIONS="${TRACEOPTIONS} --cfg=viva/categorized:smpi_cat.plist --cfg=viva/uncategorized:smpi_uncat.plist"
420     fi
421 fi
422 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
423
424 export SMPI_GLOBAL_SIZE=${NUMPROCS}
425 if [ -n "${KEEP}" ] ; then
426     echo ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
427     if [ ${HOSTFILETMP} = 1 ] ; then
428         echo "Generated hostfile ${HOSTFILE} keeped."
429     fi
430     if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
431         echo "Generated unrolled hostfile ${UNROLLEDHOSTFILE} keeped." 
432     fi
433 fi
434 ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
435 status=$?
436
437 if [ -z "${KEEP}" ] ; then
438     if [ -z "${PLATFORM}" ]; then
439         rm ${PLATFORMTMP}
440     fi
441     if [ ${HOSTFILETMP} = 1 ] ; then
442         rm ${HOSTFILE}
443     fi
444     if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
445         rm ${UNROLLEDHOSTFILE}
446     fi
447     rm ${APPLICATIONTMP}
448 fi
449
450 exit $status