Logo AND Algorithmique Numérique Distribuée

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