Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Using strings of different lengths with tr is unspecified.
[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    "-ext")
89       EXTOPT="$2"
90       shift 2
91    ;;
92
93    "-map")
94         MAPOPT="true"
95       shift 1
96    ;;
97
98    "-trace")
99       TRACE_ACTIVE="true"
100       shift 1
101    ;;
102
103    "-trace-ti")
104       TRACE_ACTIVE="true"
105       TRACE_TI_ACTIVE="true"
106       shift 1
107    ;;
108
109    "-trace-comment")
110       TRACE_COMMENT="$2"
111       shift 2
112    ;;
113
114    "-trace-comment-file")
115       TRACE_COMMENT_FILE="$2"
116       shift 2
117    ;;
118
119    "-trace-file")
120       TRACE_FILENAME="$2"
121       shift 2
122    ;;
123
124    "-trace-grouped")
125       TRACE_GROUPED="true"
126       shift 1
127    ;;
128
129    "-trace-resource")
130       TRACE_RESOURCE="true"
131       shift 1
132    ;;
133
134    "-trace-viva")
135         TRACE_VIVA="true"
136           shift 1
137    ;;
138
139    "-keep-temps")
140         KEEP="true"
141       shift 1
142    ;;
143
144    "-wrapper")
145       WRAPPER="$2"
146       shift 2
147       ;;
148
149    "-help" | "--help" | "-h")
150       usage
151       exit
152    ;;
153
154    "--cfg="*|"--log="*)
155      for OPT in ${1#*=}
156      do
157        SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
158      done
159      shift 1
160    ;;
161     *)
162       break
163     ;;
164   esac
165 done
166
167 if [ -n "$WRAPPER" ]; then
168     EXEC="$WRAPPER $1"
169 else
170     EXEC="$1"
171 fi
172 shift
173
174 # steel --cfg and --logs options
175 while [ $# -gt 0 ]; do
176   case "$1" in
177    "--cfg="*|"--log="*)
178      for OPT in ${1#*=}
179      do
180        SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
181      done
182      shift 1
183    ;;
184     *)
185       PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
186       shift      
187     ;;
188   esac
189 done
190
191
192 ##-----------------------------------
193
194 # Basic checks on the provided arguments
195 if [ -z "${EXEC}" ] ; then
196     echo "You must provide a program to execute."
197     usage
198     exit 1
199 fi
200
201 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
202     echo "No hostfile nor platform specified."
203     usage
204     exit 1
205 fi
206
207 HOSTFILETMP=0
208 if [ -z "${HOSTFILE}" ] ; then
209     HOSTFILETMP=1
210     HOSTFILE="$(mktemp tmphostXXXXXX)"
211     perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
212 fi
213
214 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
215 hostfile_procs=`grep -c "[a-zA-Z0-9]" $HOSTFILE`
216
217 if [ -z "${NUMPROCS}" ] ; then
218     # Use the amount of processes in the hostfile as default value for the -np parameter
219     NUMPROCS=$hostfile_procs
220 fi
221
222
223 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
224     echo "You requested to use ${NUMPROCS} processes, but there is only ${hostfile_procs} processes in your hostfile..." >&2
225 fi
226
227 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
228 if [ -z "${PLATFORM}" ]; then
229         PLATFORMTMP="$(mktemp tmpXXXXXX)"
230
231         cat > ${PLATFORMTMP} <<PLATFORMHEAD
232 <?xml version='1.0'?>
233 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
234 <platform version="3">
235 <AS id="AS0" routing="Full">
236 PLATFORMHEAD
237
238 for i in $(echo "for (i=${NUMPROCS};i>0;i--) i"|bc); do
239   echo "  <host id=\"host$i\" power=\"${POWER}\"/>" >> ${PLATFORMTMP}
240   echo "  <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
241   echo "  <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
242 done
243
244 for i in $(echo "for (i=${NUMPROCS};i>0;i--) i"|bc); do
245   for j in $(echo "for (i=${NUMPROCS};i>0;i--) i"|bc); do
246     if [ $i -eq $j ]; then
247       echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
248     else
249       echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
250     fi
251   done
252 done
253
254 cat >> ${PLATFORMTMP} <<PLATFORMFOOT
255 </AS>
256 </platform>
257 PLATFORMFOOT
258
259 else
260         PLATFORMTMP=${PLATFORM}
261 fi
262 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
263
264 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
265 APPLICATIONTMP="$(mktemp tmpXXXXXX)"
266 #APPLICATIONTMP="app.xml"
267
268
269 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
270 <?xml version='1.0'?>
271 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
272 <platform version="3">
273 APPLICATIONHEAD
274
275 ##---- cache hostnames of hostfile---------------
276 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
277   hostnames=$(cat ${HOSTFILE} | tr '\n\r' '  ')
278   NUMHOSTS=$(cat ${HOSTFILE} | wc -l)
279 fi
280
281 if [ "${EXTOPT}" = "smpi_replay" ]; then
282   APP_TRACES=$PROC_ARGS
283   if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
284      hosttraces=$(cat ${APP_TRACES} | tr '\n\r' '  ' )
285      NUMTRACES=$(cat ${APP_TRACES} | wc -l)
286   else
287      printf "File not found: %s\n", "${APP_TRACES:-\${APP_TRACES\}}" >&2
288      exit 1
289   fi
290 fi
291
292 ##----------------------------------------------------------
293 ##  generate application.xml with hostnames from hostfile:
294 ##  the name of host_i (1<=i<=p, where -np p) is the line i
295 ##  in hostfile (where -hostfile hostfile), or "host$i" if
296 ##  hostfile has less than i lines.
297 ##----------------------------------------------------------
298
299 HAVE_SEQ="`which seq 2>/dev/null`"
300
301 if [ -n "${HAVE_SEQ}" ]; then
302         SEQ=`${HAVE_SEQ} 0 $((${NUMPROCS}-1))`
303 else
304       cnt=0
305         while (( $cnt < ${NUMPROCS} )) ; do
306             SEQ="$SEQ $cnt"
307             cnt=$((cnt + 1));
308         done
309 fi
310
311 ##---- generate <process> tags------------------------------
312
313 for i in ${SEQ}
314 do
315   if [ -n "${HOSTFILE}" ]; then
316         j=$(( $i % ${NUMHOSTS} + 1 ))
317   fi
318   ##---- optional display of ranks to process mapping
319   if [ -n "${MAPOPT}" ]; then
320         echo "[rank $i] -> $(echo $hostnames|cut -d' ' -f$j)"
321   fi
322
323   if [ -z "$(echo $hostnames|cut -d' ' -f$j)" ]; then
324         host="host"$($j)
325   else
326         host="$(echo $hostnames|cut -d' ' -f$j)"
327   fi
328   echo "  <process host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->" >> ${APPLICATIONTMP}
329   echo "    <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
330   if [ "${EXTOPT}" = "smpi_replay" ]; then
331     if  [ ${NUMTRACES} -gt 1 ]; then
332       echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f$j)\"/>" >> ${APPLICATIONTMP}
333     else
334       echo "    <argument value=\"$(echo $hosttraces|cut -d' ' -f1)\"/>" >> ${APPLICATIONTMP}
335     fi
336   else 
337     for ARG in $PROC_ARGS; do
338        echo "    <argument value=\"${ARG}\"/>" >> ${APPLICATIONTMP}
339     done
340   fi
341   echo "  </process>" >> ${APPLICATIONTMP}
342 done
343
344 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
345 </platform>
346 APPLICATIONFOOT
347 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
348
349 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
350 if [ -n "${TRACE_ACTIVE}" ]; then
351   #define trace filename
352   if [ -n "${TRACE_TI_ACTIVE}" ]; then
353     if [ -z "${TRACE_FILENAME}" ]; then
354       TRACE_FILENAME="smpi_simgrid.txt"
355     fi
356     TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes --cfg=tracing/smpi/format:TI --cfg=tracing/smpi/computing:yes"
357   else
358     if [ -z "${TRACE_FILENAME}" ]; then
359       TRACE_FILENAME="smpi_simgrid.trace"
360     fi
361     TRACEOPTIONS="--cfg=tracing:yes --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:yes"
362   fi
363
364   if [ -n "${TRACE_COMMENT}" ]; then
365     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
366   fi
367
368   if [ -n "${TRACE_COMMENT_FILE}" ]; then
369     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment_file:${TRACE_COMMENT_FILE}"
370   fi
371
372   if [ -n "${TRACE_GROUPED}" ]; then
373     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:yes"
374   fi
375
376   if [ -n "${TRACE_RESOURCE}" ]; then
377     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:yes --cfg=tracing/uncategorized:yes"
378   fi
379
380   if [ -n "${TRACE_VIVA}" ]; then
381     TRACEOPTIONS="${TRACEOPTIONS} --cfg=viva/categorized:smpi_cat.plist --cfg=viva/uncategorized:smpi_uncat.plist"
382   fi
383 fi
384 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
385
386 export SMPI_GLOBAL_SIZE=${NUMPROCS}
387 if [ -n "${KEEP}" ] ; then
388   echo ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
389   if [ ${HOSTFILETMP} = 1 ] ; then
390     echo "Generated hostfile ${HOSTFILE} keeped."
391   fi
392 fi
393 ${EXEC} ${TRACEOPTIONS} ${SIMOPTS} ${PLATFORMTMP} ${APPLICATIONTMP}
394 status=$?
395
396 if [ -z "${KEEP}" ] ; then
397    if [ -z "${PLATFORM}" ]; then
398         rm ${PLATFORMTMP}
399    fi
400    if [ ${HOSTFILETMP} = 1 ] ; then
401         rm ${HOSTFILE}
402    fi
403    rm ${APPLICATIONTMP}
404 fi
405
406 exit $status