Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / smpi / replay_multiple / generate_multiple_deployment.sh
1 #! /bin/sh
2
3 # Copyright (c) 2007-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 #usage to print the way this script should be called
10 usage () {
11     cat <<EOF
12 Usage: $0 [OPTIONS] -platform <xmldesc> -hostfile <hostfile> replay-file output-file
13 EOF
14 }
15
16 #check if we have at least one parameter
17 if [ $# -eq 0 ]
18 then
19     usage
20     exit
21 fi
22
23 EXTOPT=""
24 WRAPPER=""
25 HOSTFILE=""
26
27 while true; do
28     case "$1" in
29         "-platform")
30             PLATFORM="$2"
31             if [ ! -f "${PLATFORM}" ]; then
32                 echo "[`basename $0`] ** error: the file '${PLATFORM}' does not exist. Aborting."
33                 exit 1
34             fi
35             shift 2
36             ;;
37         "-hostfile")
38             HOSTFILE="$2"
39             if [ ! -f "${HOSTFILE}" ]; then
40                 echo "[`basename $0`] ** error: the file '${HOSTFILE}' does not exist. Aborting."
41                 exit 1
42             fi
43             shift 2
44             ;;
45         "-machinefile")
46             HOSTFILE="$2"
47             if [ ! -f "${HOSTFILE}" ]; then
48                 echo "[`basename $0`] ** error: the file '${HOSTFILE}' does not exist. Aborting."
49                 exit 1
50             fi
51             shift 2
52             ;;
53         *)
54             break
55             ;;
56     esac
57 done
58
59 # steel --cfg and --logs options
60 while [ $# -gt 0 ]; do
61     case "$1" in
62         "--cfg="*|"--log="*)
63             for OPT in ${1#*=}
64             do
65                 SIMOPTS="$SIMOPTS ${1%%=*}=$OPT"
66             done
67             shift 1
68             ;;
69         *)
70             PROC_ARGS="${PROC_ARGS:+$PROC_ARGS }$1"
71             shift      
72             ;;
73     esac
74 done
75
76 if [ -z "${HOSTFILE}" ] && [ -z "${PLATFORM}" ] ; then
77     echo "No hostfile nor platform specified."
78     usage
79     exit 1
80 fi
81
82 HOSTFILETMP=0
83 if [ -z "${HOSTFILE}" ] ; then
84     HOSTFILETMP=1
85     HOSTFILE="$(mktemp tmphostXXXXXX)"
86     perl -ne 'print "$1\n" if /.*<host.*?id="(.*?)".*?\/>.*/' ${PLATFORM} > ${HOSTFILE}
87 fi
88 UNROLLEDHOSTFILETMP=0
89
90 #parse if our lines are terminated by :num_process
91 multiple_processes=`grep -c ":" $HOSTFILE`
92 if [ "${multiple_processes}" -gt 0 ] ; then
93     UNROLLEDHOSTFILETMP=1
94     UNROLLEDHOSTFILE="$(mktemp tmphostXXXXXX)"
95     perl -ne ' do{ for ( 1 .. $2 ) { print "$1\n" } } if /(.*?):(\d+).*/'  ${HOSTFILE}  > ${UNROLLEDHOSTFILE}
96     if [ ${HOSTFILETMP} = 1 ] ; then
97         rm ${HOSTFILE}
98         HOSTFILETMP=0
99     fi
100     HOSTFILE=$UNROLLEDHOSTFILE
101 fi
102
103 # Don't use wc -l to compute it to avoid issues with trailing \n at EOF
104 hostfile_procs=`grep -c "[a-zA-Z0-9]" $HOSTFILE`
105 if [ ${hostfile_procs} = 0 ] ; then
106    echo "[`basename $0`] ** error: the hostfile '${HOSTFILE}' is empty. Aborting." >&2
107    exit 1
108 fi
109
110 ##-------------------------------- DEFAULT APPLICATION --------------------------------------
111
112 APPLICATIONTMP=$(echo ${PROC_ARGS}|cut -d' ' -f2 -s)
113
114 cat > ${APPLICATIONTMP} <<APPLICATIONHEAD
115 <?xml version='1.0'?>
116 <!DOCTYPE platform SYSTEM "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd">
117 <platform version="4">
118 APPLICATIONHEAD
119
120 ##---- cache hostnames of hostfile---------------
121 if [ -n "${HOSTFILE}" ] && [ -f ${HOSTFILE} ]; then
122     hostnames=$(cat ${HOSTFILE} | tr '\n\r' '  ')
123     NUMHOSTS=$(cat ${HOSTFILE} | wc -l)
124 fi
125
126 DESCRIPTIONFILE=$(echo $PROC_ARGS|cut -d' ' -f1)
127
128 if [ -n "${DESCRIPTIONFILE}" ] && [ -f "${DESCRIPTIONFILE}" ]; then
129     NUMINSTANCES=$(cat ${DESCRIPTIONFILE} | wc -l)
130     replayinstances=$(cat ${DESCRIPTIONFILE})
131     IFS_OLD=$IFS
132     IFS=$'\n'
133     set -f
134     NUMPROCS=0
135     while IFS= read -r line; do
136         # return IFS back if you need to split new line by spaces:
137         IFS=$IFS_OLD
138         IFS_OLD=
139         # generate three lists, one with instance id, ont with instance size, one with files
140         instance=$(echo "$line"|cut -d' ' -f1)
141         hosttrace=$(cat $(echo "$line"|cut -d' ' -f2)| tr '\n\r' '  ' )
142         NUMPROCSMINE=$(cat $(echo "$line"|cut -d' ' -f2) | wc -l)
143         
144         if [ $NUMPROCSMINE != $(echo "$line"|cut -d' ' -f3) ];
145         then
146           echo "declared num of processes for instance $instance : ${array[2]} is not the same as the one in the replay files : $NUMPROCSMINE. Please check consistency of these information"
147           exit 1
148         fi
149         
150         sleeptime=$(echo "$line"|cut -d' ' -f4)
151         HAVE_SEQ="`which seq 2>/dev/null`"
152
153         if [ -n "${HAVE_SEQ}" ]; then
154             SEQ1=`${HAVE_SEQ} 0 $((${NUMPROCSMINE}-1))`
155         else
156             cnt=0
157             while (( $cnt < ${NUMPROCSMINE} )) ; do
158             SEQ1="$SEQ1 $cnt"
159             cnt=$((cnt + 1));
160             done
161         fi
162         #NUMPROCS=$((${NUMPROCS}+${NUMPROCSMINE}));
163         for i in $SEQ1
164 ##----------------------------------------------------------
165 ##  generate application.xml with hostnames from hostfile:
166 ##  the name of host_i (1<=i<=p, where -np p) is the line i
167 ##  in hostfile (where -hostfile hostfile), or "host$i" if
168 ##  hostfile has less than i lines.
169 ##----------------------------------------------------------
170
171 ##---- generate <process> tags------------------------------
172         do
173             if [ -n "${HOSTFILE}" ]; then
174                 j=$(( ${NUMPROCS} % ${NUMHOSTS} +1))
175             fi
176             hostname=$(echo $hostnames|cut -d' ' -f$j)
177             if [ -z "${hostname}" ]; then
178                 host="host"$($j)
179             else
180                 host="${hostname}"
181             fi
182         
183             echo "  <process host=\"${host}\" function=\"${instance}\"> <!-- function name used only for logging -->" >> ${APPLICATIONTMP}
184             echo "    <argument value=\"${instance}\"/> <!-- instance -->" >> ${APPLICATIONTMP}
185             echo "    <argument value=\"${i}\"/> <!-- rank -->" >> ${APPLICATIONTMP}
186             echo "    <argument value=\"$(echo $hosttrace|cut -d' ' -f$(($i+1)))\"/>" >> ${APPLICATIONTMP}
187             
188             echo "    <argument value=\"${sleeptime}\"/> <!-- delay -->" >> ${APPLICATIONTMP}
189             echo "  </process>" >> ${APPLICATIONTMP}
190             NUMPROCS=$(( ${NUMPROCS} +1))
191         done
192         # return IFS back to newline for "for" loop
193         IFS_OLD=$IFS
194         IFS=$'\n'
195     done < ${DESCRIPTIONFILE}
196
197     # return delimiter to previous value
198     IFS=$IFS_OLD
199     IFS_OLD=
200 else
201     printf "File not found: %s\n", "${APP_TRACES[0]:-\${APP_TRACES[0]\}}" >&2
202     exit 1
203 fi
204
205 cat >> ${APPLICATIONTMP} <<APPLICATIONFOOT
206 </platform>
207 APPLICATIONFOOT
208 ##-------------------------------- end DEFAULT APPLICATION --------------------------------------
209
210 if [ ${HOSTFILETMP} = 1 ] ; then
211     rm ${HOSTFILE}
212 fi
213 if [ ${UNROLLEDHOSTFILETMP} = 1 ] ; then
214     rm ${UNROLLEDHOSTFILE}
215 fi
216
217 exit 0