Logo AND Algorithmique Numérique Distribuée

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