Logo AND Algorithmique Numérique Distribuée

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