forked from C2SM-RCM/buildenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoduleTools.sh
256 lines (202 loc) · 8.07 KB
/
moduleTools.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
# module tools
##################################################
# functions
##################################################
exitError()
{
\rm -f /tmp/tmp.${user}.$$ 1>/dev/null 2>/dev/null
echo "ERROR $1: $3" 1>&2
echo "ERROR LOCATION=$0" 1>&2
echo "ERROR LINE=$2" 1>&2
exit $1
}
showWarning()
{
echo "WARNING $1: $3" 1>&2
echo "WARNING LOCATION=$0" 1>&2
echo "WARNING LINE=$2" 1>&2
}
containsElement()
{
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
isOnOff()
{
local switch=$1
local onoff=(ON OFF)
containsElement "${switch}" "${onoff[@]}" || exitError 101 ${LINENO} "Invalid value for ON/OFF switch (${switch}) chosen"
}
checkModuleAvailable()
{
local module=$1
if [ -n "${module}" ] ; then
module avail -t 2>&1 | grep "${module}" &> /dev/null
if [ $? -ne 0 ] ; then
exitError 201 ${LINENO} "module ${module} is unavailable"
fi
fi
}
compareFiles()
{
one=$1
two=$2
msg=$3
if [ ! -f "${one}" ] ; then exitError 3001 ${LINENO} "Must supply two valid files to compareFiles (${one})" ; fi
if [ ! -f "${two}" ] ; then exitError 3002 ${LINENO} "Must supply two valid files to compareFiles (${two})" ; fi
# sort and compare the two files
diff <(sort "${one}") <(sort "${two}")
if [ $? -ne 0 ] ; then
echo "ERROR: Difference detected between ${one} and ${two} in compareFiles"
echo " ${msg}"
exit 1
fi
}
compilerVersion()
{
compiler=$1
# check for zero strings
if [ -z "${compiler}" ] ; then exitError 3101 ${LINENO} "Must supply a compiler command to compilerVersion" ; fi
# find absolute path of compiler
which ${compiler} &> /dev/null
if [ $? -eq 1 ] ; then exitError 3102 ${LINENO} "Cannot find compiler command (${compiler})" ; fi
compiler=`which ${compiler}`
# check for GNU
res=`${compiler} -v 2>&1 | grep '^gcc'`
if [ -n "${res}" ] ; then
version=`echo "${res}" | awk '{print $3}'`
echo ${version}
return
fi
# check for Cray
res=`${compiler} -V 2>&1 | grep '^Cray'`
if [ -n "${res}" ] ; then
version=`echo "${res}" | awk '{print $5}'`
echo ${version}
return
fi
# check for PGI
res=`${compiler} -V 2>&1 | grep '^pg'`
if [ -n "${res}" ] ; then
version=`echo "${res}" | awk '{print $2}'`
echo ${version}
return
fi
# could not determine compiler version
exitError 3112 ${LINENO} "Could not determine compiler version (${compiler})"
}
writeModuleList()
{
local logfile=$1
local mode=$2
local msg=$3
local modfile=$4
# check arguments
test -n "${logfile}" || exitError 601 ${LINENO} "Option <logfile> is not set"
test -n "${mode}" || exitError 602 ${LINENO} "Option <mode> is not set"
test -n "${msg}" || exitError 603 ${LINENO} "Option <msg> is not set"
# check correct mode
local modes=(all loaded)
containsElement "${mode}" "${modes[@]}" || exitError 610 ${LINENO} "Invalid mode (${mode}) chosen"
# clean log file for "all" mode
if [ "${mode}" == "all" ] ; then
/bin/rm -f ${logfile} 2>/dev/null
touch ${logfile}
fi
# log modules to logfile
echo "=============================================================================" >> ${logfile}
echo "${msg}:" >> ${logfile}
echo "=============================================================================" >> ${logfile}
if [ "${mode}" == "all" ] ; then
module avail -t >> ${logfile} 2>&1
elif [ "${mode}" == "loaded" ] ; then
module list -t 2>&1 | grep -v alps >> ${logfile}
else
exitError 620 ${LINENO} "Invalid mode (${mode}) chosen"
fi
# save list of loaded modules to environment file (if required)
if [ -n "${modfile}" ] ; then
/bin/rm -f ${modfile}
touch ${modfile}
module list -t 2>&1 | grep -v alps | grep -v '^- Package' | grep -v '^Currently Loaded' | sed 's/^/module load /g' > ${modfile}
# Workaround for machines that store the modules in a predefined list
# such as kesch
if [[ -n "$ENVIRONMENT_TEMPFILE" ]] ; then
cp $ENVIRONMENT_TEMPFILE ${modfile}
else
if [[ -z ${host} ]]; then
exitError 654 ${LINENO} "host is not defined"
fi
# workaround for Todi, Daint, and Lema
if [ "${host}" == "lema" -o "${host}" == "todi" -o "${host}" == "daint" ] ; then
cat ${modfile} | egrep -v "module load cce\/|module load gcc\/|module load pgi\/" > /tmp/tmp.${host}.${user}.$$
compilo=`cat ${modfile} | egrep "module load cce\/|module load gcc\/|module load pgi\/" | sed 's/module load/module swap/g'`
echo "${compilo}" >> /tmp/tmp.${host}.${user}.$$
/bin/mv -f /tmp/tmp.${host}.${user}.$$ ${modfile}
compilo=`cat ${modfile} | egrep "module load cray-mpich" | sed 's/module load/module swap/g'`
sed 's/module load cray-mpich/module swap cray-mpich/g' ${modfile} > /tmp/tmp.${host}.${user}.$$
/bin/mv -f /tmp/tmp.${host}.${user}.$$ ${modfile}
fi
fi
fi
}
testEnvironment()
{
local tmp=/tmp/tmp.${user}.$$
echo ">>>>>>>>>>>>>>> test environment setup"
# initialize the log
writeModuleList ${tmp}.log all "AVAILABLE MODULES"
# checkpoint environment before
writeModuleList ${tmp}.log loaded "BEFORE C++ MODULES" ${tmp}.mod.before
# change environments a couple of times
for i in `seq 2` ; do
echo ">>>>>>>>>>>>>>> test C++ environment setup"
# check C++ env
setCppEnvironment
writeModuleList ${tmp}.log loaded "C++ MODULES" ${tmp}.mod.dycore
if [ -z ${old_prgenv+x} ] ; then exitError 8001 ${LINENO} "variable old_prgenv is not set" ; fi
if [ -z ${dycore_gpp+x} ] ; then exitError 8002 ${LINENO} "variable dycore_gpp is not set" ; fi
if [ -z ${dycore_gcc+x} ] ; then exitError 8003 ${LINENO} "variable dycore_gcc is not set" ; fi
if [ -z ${cuda_gpp+x} ] ; then exitError 8004 ${LINENO} "variable cuda_gpp is not set" ; fi
if [ -z ${boost_path+x} ] ; then exitError 8005 ${LINENO} "variable boost_path is not set" ; fi
# check cleanup of C++ env
unsetCppEnvironment
writeModuleList ${tmp}.log loaded "BETWEEN MODULES" ${tmp}.mod.between
if [ ! -z ${old_prgenv+x} ] ; then exitError 8101 ${LINENO} "variable old_prgenv is still set" ; fi
if [ ! -z ${dycore_gpp+x} ] ; then exitError 8102 ${LINENO} "variable dycore_gpp is still set" ; fi
if [ ! -z ${dycore_gcc+x} ] ; then exitError 8103 ${LINENO} "variable dycore_gcc is still set" ; fi
if [ ! -z ${cuda_gpp+x} ] ; then exitError 8104 ${LINENO} "variable cuda_gpp is still set" ; fi
if [ ! -z ${boost_path+x} ] ; then exitError 8105 ${LINENO} "variable boost_path is still set" ; fi
compareFiles ${tmp}.mod.before ${tmp}.mod.between
echo ">>>>>>>>>>>>>>> test Fortran environment setup"
# check Fortran env
setFortranEnvironment
writeModuleList ${tmp}.log loaded "FORTRAN MODULES" ${tmp}.mod.fortran
if [ -z ${old_prgenv+x} ] ; then exitError 8201 ${LINENO} "variable old_prgenv is not set" ; fi
# check cleanup of Fortran env
unsetFortranEnvironment
writeModuleList ${tmp}.log loaded "AFTER FORTRAN MODULES" ${tmp}.mod.after
if [ ! -z ${old_prgenv+x} ] ; then exitError 8301 ${LINENO} "variable old_prgenv is still set" ; fi
compareFiles ${tmp}.mod.before ${tmp}.mod.after
done
# everything ok
echo ">>>>>>>>>>>>>>> success"
# remove temporary files
/bin/rm -f ${tmp}*
}
writeCppEnvironment()
{
setCppEnvironment
writeModuleList /dev/null loaded "C++ MODULES" ${base_path}/modules_dycore.env
unsetCppEnvironment
}
writeFortranEnvironment()
{
setFortranEnvironment
writeModuleList /dev/null loaded "FORTRAN MODULES" ${base_path}/modules_fortran.env
unsetFortranEnvironment
}
export -f writeModuleList
export -f containsElement