Logo AND Algorithmique Numérique Distribuée

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