Logo AND Algorithmique Numérique Distribuée

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