Logo AND Algorithmique Numérique Distribuée

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