Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Print a message and exit if file was not found.
[simgrid.git] / src / smpi / smpirun.in
1 #!/bin/bash
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 function 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   -map                       # display the machine on which each process rank is mapped
27   -np <numprocs>             # use that amount of processes from the hostfile.
28                              # By default, all processes of the hostfile are used.
29   -trace                     # activate tracing
30   -trace-comment <comment>   # put a comment on the top of the trace file
31   -trace-comment-file <file> # put file contents on the top of the trace file as comment
32   -trace-grouped             # group MPI processes by location
33   -trace-resource            # trace resource utilization
34   -trace-viva               # generate configuration for Viva's GraphView
35   -trace-file <tracefile>    # name of the tracefile (simgrid_smpi.trace)
36   -ext <value>               # additional parameter (reserved)
37
38 or (deprecated usage):
39   $0 [-keep-temps] [-np <numprocs>] [-bandwidth <bytes/sec>] [-latency <secs>] program [program-options]
40
41 EOF
42 }
43
44 #check if we have at least one parameter
45 if [ $# -eq 0 ]
46 then
47     usage
48     exit
49 fi
50
51 EXTOPT=""
52
53 while true; do
54   case "$1" in
55    "-np" | "-n")
56       NUMPROCS="$2"
57       shift 2
58     ;;
59    "-bandwidth")
60       NETWORK_BANDWIDTH="$2"
61       shift 2
62     ;;
63    "-latency")
64       NETWORK_LATENCY="$2"
65       shift 2
66     ;;
67    "-platform")
68         PLATFORM="$2"
69       if [ ! -f "${PLATFORM}" ]; then
70                 echo "[$0] ** error: the file '${PLATFORM}' does not exist. Aborting."
71                 exit 1
72       fi
73         shift 2
74     ;;
75    "-hostfile")
76         HOSTFILE="$2"
77       if [ ! -f "${HOSTFILE}" ]; then
78                 echo "[$0] ** error: the file '${HOSTFILE}' does not exist. Aborting."
79                 exit 1
80       fi
81         shift 2
82     ;;
83
84    "-ext")
85       EXTOPT="$2"
86       shift 2
87    ;;
88
89    "-map")
90         MAPOPT="true"
91       shift 1
92    ;;
93
94    "-trace")
95       TRACE_ACTIVE="true"
96       shift 1
97    ;;
98
99    "-trace-comment")
100       TRACE_COMMENT="$2"
101       shift 2
102    ;;
103
104    "-trace-comment-file")
105       TRACE_COMMENT_FILE="$2"
106       shift 2
107    ;;
108
109    "-trace-file")
110       TRACE_FILENAME="$2"
111       shift 2
112    ;;
113
114    "-trace-grouped")
115       TRACE_GROUPED="true"
116       shift 1
117    ;;
118
119    "-trace-resource")
120       TRACE_RESOURCE="true"
121       shift 1
122    ;;
123
124    "-trace-viva")
125         TRACE_VIVA="true"
126           shift 1
127    ;;
128
129    "-keep-temps")
130         KEEP="true"
131       shift 1
132    ;;
133
134    "-help" | "--help" | "-h")
135       usage
136       exit
137    ;;
138
139    "--cfg="*|"--log="*)
140      for OPT in ${1#*=}
141      do
142        SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
143      done
144      shift 1
145    ;;
146     *)
147       break
148     ;;
149   esac
150 done
151
152 EXEC="$1"
153 shift
154
155 ##-----------------------------------
156
157 # Basic checks on the provided arguments
158 if [ -z "${EXEC}" ] ; then
159     echo "You must provide a program to execute."
160     usage
161     exit 1
162 fi
163
164 if [ -z "${HOSTFILE}" ] ; then
165     echo "No hostfile specified."
166     usage
167     exit 1
168 fi
169
170 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
171 hostfile_procs=`grep -c "[a-zA-Z0-9]" $HOSTFILE`
172
173 if [ -z "${NUMPROCS}" ] ; then
174     # Use the amount of processes in the hostfile as default value for the -np parameter
175     NUMPROCS=$hostfile_procs
176 fi
177
178
179 if [ ${NUMPROCS} -gt ${hostfile_procs} ] ; then
180     echo "You requested to use ${NUMPROCS} processes, but there is only ${hostfile_procs} processes in your hostfile..." >&2
181 fi
182
183 ##-------------------------------- DEFAULT or SPECIFIED PLATFORM --------------------------------------
184 if [ -z "${PLATFORM}" ]; then
185         PLATFORMTMP="$(mktemp tmpXXXXXX)"
186
187         cat > ${PLATFORMTMP} <<PLATFORMHEAD
188 <?xml version='1.0'?>
189 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
190 <platform version="3">
191 <AS id="AS0" routing="Full">
192 PLATFORMHEAD
193
194 for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
195   echo "  <host id=\"host$i\" power=\"${POWER}\"/>" >> ${PLATFORMTMP}
196   echo "  <link id=\"loop$i\" bandwidth=\"${LOOPBACK_BANDWIDTH}\" latency=\"${LOOPBACK_LATENCY}\"/>" >> ${PLATFORMTMP}
197   echo "  <link id=\"link$i\" bandwidth=\"${NETWORK_BANDWIDTH}\" latency=\"${NETWORK_LATENCY}\"/>" >> ${PLATFORMTMP}
198 done
199
200 for (( i=${NUMPROCS}; $i ; i=$i-1 )) do
201   for (( j=${NUMPROCS}; $j ; j=$j-1 )) do
202     if [ $i -eq $j ]; then
203       echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"loop$i\"/></route>" >> ${PLATFORMTMP}
204     else
205       echo "  <route src=\"host$i\" dst=\"host$j\"><link_ctn id=\"link$i\"/><link_ctn id=\"link$j\"/></route>" >> ${PLATFORMTMP}
206     fi
207   done
208 done
209
210 cat >> ${PLATFORMTMP} <<PLATFORMFOOT
211 </AS>
212 </platform>
213 PLATFORMFOOT
214
215 else
216         PLATFORMTMP=${PLATFORM}
217 fi
218 ##-------------------------------- end DEFAULT or SPECIFIED PLATFORM --------------------------------------
219
220 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
221 APPLICATIONTMP="$(mktemp tmpXXXXXX)"
222 #APPLICATIONTMP="app.xml"
223
224
225 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
226 <?xml version='1.0'?>
227 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid.dtd">
228 <platform version="3">
229 APPLICATIONHEAD
230
231 ##---- cache hostnames of hostfile---------------
232 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
233         hostnames=(`cat ${HOSTFILE} | tr \\\n " "`)
234       NUMHOSTS=`cat ${HOSTFILE} | wc -l`
235 fi
236
237 if [ "${EXTOPT}" = "smpi_replay" ]; then
238   APP_TRACES=$1;
239   if [ -n "${APP_TRACES}" ] && [ -f "${APP_TRACES}" ]; then
240      hosttraces=(`cat ${APP_TRACES} | tr \\\n " "`)
241      NUMTRACES=`cat ${APP_TRACES} | wc -l`
242   else
243      printf "File not found: %s\n", "${APP_TRACES:-\${APP_TRACES}}" >&2
244      exit 1
245   fi
246 fi
247
248 ##----------------------------------------------------------
249 ##  generate application.xml with hostnames from hostfile:
250 ##  the name of host_i (1<=i<=p, where -np p) is the line i
251 ##  in hostfile (where -hostfile hostfile), or "host$i" if
252 ##  hostfile has less than i lines.
253 ##----------------------------------------------------------
254
255 HAVE_SEQ="`which seq 2>/dev/null`"
256
257 if [ -n "${HAVE_SEQ}" ]; then
258         SEQ=`${HAVE_SEQ} 0 $((${NUMPROCS}-1))`
259 else
260       cnt=0
261         while (( $cnt < ${NUMPROCS} )) ; do
262             SEQ="$SEQ $cnt"
263             cnt=$((cnt + 1));
264         done
265 fi
266
267 ##---- generate <process> tags------------------------------
268
269 for i in ${SEQ}
270 do
271   if [ -n "${HOSTFILE}" ]; then
272         j=$(( $i % ${NUMHOSTS} ))
273   fi
274   ##---- optional display of ranks to process mapping
275   if [ -n "${MAPOPT}" ]; then
276         echo "[rank $i] -> ${hostnames[$j]}"
277   fi
278
279   if [ -z "${hostnames[$j]}" ]; then
280         host="host"$(($j+1))
281   else
282         host="${hostnames[$j]}"
283   fi
284   echo "  <process host=\"${host}\" function=\"$i\"> <!-- function name used only for logging -->" >> ${APPLICATIONTMP}
285   echo "    <argument value=\"$i\"/> <!-- rank -->" >> ${APPLICATIONTMP}
286   if [ "${EXTOPT}" = "smpi_replay" ]; then
287     if  [ ${NUMTRACES} -gt 1 ]; then
288       echo "    <argument value=\"${hosttraces[$j]}\"/>" >> ${APPLICATIONTMP}
289     else
290       echo "    <argument value=\"${hosttraces[0]}\"/>" >> ${APPLICATIONTMP}
291     fi
292   else 
293     for ARG in $*; do
294        echo "    <argument value=\"${ARG}\"/>" >> ${APPLICATIONTMP}
295     done
296   fi
297   echo "  </process>" >> ${APPLICATIONTMP}
298 done
299
300 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
301 </platform>
302 APPLICATIONFOOT
303 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
304
305 ##---------------------- SMPI TRACING OPTIONS ---------------------------------
306 if [ -n "${TRACE_ACTIVE}" ]; then
307   #define trace filename
308   if [ -z "${TRACE_FILENAME}" ]; then
309     TRACE_FILENAME="smpi_simgrid.trace"
310   fi
311   TRACEOPTIONS="--cfg=tracing:1 --cfg=tracing/filename:${TRACE_FILENAME} --cfg=tracing/smpi:1"
312
313   if [ -n "${TRACE_COMMENT}" ]; then
314     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment:${TRACE_COMMENT}"
315   fi
316
317   if [ -n "${TRACE_COMMENT_FILE}" ]; then
318     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/comment_file:${TRACE_COMMENT_FILE}"
319   fi
320
321   if [ -n "${TRACE_GROUPED}" ]; then
322     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/smpi/group:1"
323   fi
324
325   if [ -n "${TRACE_RESOURCE}" ]; then
326     TRACEOPTIONS="${TRACEOPTIONS} --cfg=tracing/categorized:1 --cfg=tracing/uncategorized:1"
327   fi
328
329   if [ -n "${TRACE_VIVA}" ]; then
330     TRACEOPTIONS="${TRACEOPTIONS} --cfg=viva/categorized:smpi_cat.plist --cfg=viva/uncategorized:smpi_uncat.plist"
331   fi
332 fi
333 ##---------------------- end SMPI TRACING OPTIONS ---------------------------------
334
335 export SMPI_GLOBAL_SIZE=${NUMPROCS}
336 if [ -n "${KEEP}" ] ; then
337   echo ${EXEC} ${SIMOPTS} ${TRACEOPTIONS} ${PLATFORMTMP} ${APPLICATIONTMP}
338 fi
339 ${EXEC} ${SIMOPTS} ${TRACEOPTIONS} ${PLATFORMTMP} ${APPLICATIONTMP}
340 status=$?
341
342 if [ -z "${KEEP}" ] ; then
343    if [ -z "${PLATFORM}" ]; then
344         rm ${PLATFORMTMP}
345    fi
346    rm ${APPLICATIONTMP}
347 fi
348
349 exit $status