Logo AND Algorithmique Numérique Distribuée

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