-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
311 lines (283 loc) · 9.91 KB
/
Copy pathscripts.py
File metadata and controls
311 lines (283 loc) · 9.91 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
################################################################
####
#### scripts.py : partial unix scripts
####
################################################################
import re
from MrPackMod.basics import module_version_from_env,\
trace_string,echo_string,echo_warning,trace_var,\
abort_on_zero_keyword,nonzero_keyword,zero_keyword
from MrPackMod.error import isnull,nonnull
from MrPackMod.names import compilers_names,family_names,srcdir_name,\
mode_has_mpi,mode_has_seq,mode_is_core
from typing import Any
def compilers_flags( **kwargs: Any ) -> dict[str, str]:
flags = { 'CFLAGS':"", 'CXXFLAGS':"", 'FFLAGS':"", }
if cflags := nonzero_keyword( "cflags",**kwargs ):
flags["CFLAGS"] = cflags
if cxxflags := nonzero_keyword( "cxxflags",**kwargs ):
flags["CXXFLAGS"] = cxxflags
if fflags := nonzero_keyword( "fflags",**kwargs ):
flags["FFLAGS"] = fflags
return flags
def export_flags( **kwargs: Any ) -> str:
flags = compilers_flags( **kwargs )
cmdline = ""; cont = ""
for lang in [ "CFLAGS", "CXXFLAGS", "FFLAGS", ]:
if nonnull( flag := flags[lang] ):
cmdline += f"{cont}export {lang}=\"{flag}\""
cont = " && "
return cmdline
def export_compilers_script( dummy : list[str],**kwargs: Any ) -> tuple[str,str]:
if mode_is_core( **kwargs ): return "","No compilers in core mode"
trace_string( "Exporting compilers",**kwargs )
compilers = compilers_names( **kwargs )
script : str = "\necho \"Now exporting compilers for cmake and such\""
for key,val in compilers.items():
trace_string( f" .. Setting compiler: {key}={val}",**kwargs )
script += f"""
export {key}={val}
echo Export {key}={val}
whichcomp=$( which {val} )
if [ $? -gt 0 ] ; then
echo FAILURE: could not determine {val} && exit 1
fi
echo where {val}=${{whichcomp}}
"""
return script,"Export compiler settings"
def load_compiler_and_mpi_and_modules_script( modules_to_load : str,**kwargs: Any ) -> str:
title : str = f"Load compiler and mpi and modules: {modules_to_load}"
errmsg : str = f"Failed to load compiler and mpi and modules: {modules_to_load}"
_,compiler,compilerversion,_,mpi,mpiversion = family_names( **kwargs )
modulepath = nonzero_keyword( "modulepath",**kwargs )
if not ( redirect := nonzero_keyword( "redirect",**kwargs ) ):
redirect = ""
loadscript : str = ""
#
# Start by defining some functions
#
loadscript += modulereportfunction( )
loadscript += modulelistfunction()
loadscript += modulecommandfunction( )
loadscript += moduleproperfunction()
#
# Now the actual script
#
loadscript += modulepurgefunction()
if not mode_is_core( **kwargs ):
loadscript += compilerloadfunction( modulepath,compiler,compilerversion )
if nonzero_keyword( "BLASLAPACK",**kwargs ):
if comp := abort_on_zero_keyword( "COMPILER",**kwargs ):
blas : str = ""
if comp=="gcc": blas : str = "mkl"
if comp=="nvidia" : blas = "nvpl"
if nonnull(blas):
loadscript += f"""
echo "Load blas/lapack library: {blas}"
modulecommand "load blas" "load {blas}"
"""
if mode_has_mpi( **kwargs ):
loadscript += mpiloadfunction( mpi,mpiversion )
if nonnull( modules_to_load ) and zero_keyword( "skipmodules",**kwargs ):
loadscript += modulesloadscript( modules_to_load,**kwargs )
else:
echo_warning( "not loading any modules",**kwargs )
loadscript += f"""
echo Module listing:
modulelist
"""
return loadscript,title
def module_and_version_to_load( modver : str,**kwargs ) -> tuple[str,str,str]:
if modslshver := re.match( r'^([^/]+)(/)([^/]+)$',modver ):
# if there was a slash, keep original mod+ver
return modslshver.groups()
else:
# no slash, let's see if we can find a version
module : str = modver
# no version required, let's see if the environment has one
version = module_version_from_env( modver,**kwargs )
if nonnull( version ):
return module,"/",version
modver = f"{module}/{version}"
else:
# no slash and no environment version, so load default
return module,"",""
#
# This gets called only from do_config_tests
#
def modules_proper_script( moduleslist : list[str],**kwargs : Any ) -> tuple[str,str]:
modulestring : str = ','.join(moduleslist)
title : str = f"Module proper testing for {modulestring}"
script : str = f""
if nonzero_keyword("installing",**kwargs ):
srcdir = srcdir_name( **kwargs )
script += f"""
if [ ! -d "{srcdir}" ] ; then
echo "FAILURE: Source directory {srcdir} does not exist"
exit 1
fi
"""
for modver in moduleslist:
# strip any version number
onemodulescript,_ = one_module_proper_script( [modver],**kwargs )
script += f"""
modulecommand "load {modver}" "load {modver}"
{onemodulescript}
"""
script += f"\necho End of module proper testing\n"
return script,title
#
# Assuming a module has been loaded,
# these lines test that the module is proper
#
def one_module_proper_script( modverlist : list[str],**kwargs : Any ) -> tuple[str,str]:
modver : str = modverlist[0]
title : str = f"test proper of module {modver}"
module,_ = f"{modver}/".split("/",maxsplit=1)
script : str = f"""
echo \">>>> Test proper of module {modver}\"
testmoduleproper {modver}
"""
if nonzero_keyword( "pkgconfig",**kwargs ) or nonzero_keyword( "pkgconfiglib",**kwargs ):
script += f"""
echo " .. Finding pc files:"
find ${{pkgdir}} -name \\*.pc
echo "where PKG_CONFIG_PATH="
echo ${{PKG_CONFIG_PATH}} | tr ':' '\\n'
"""
if nonzero_keyword( "cmakeconfig",**kwargs ):
script += f"echo \" .. Finding cmake files:\"\nfind ${{pkgdir}} -name \\*.cmake\n"
script += f"""
echo \"<<<< end of test proper of module {modver}\"
"""
return script,title
##
## Some long literals,
## to make function above more readable
##
def modulereportfunction( redirect="" ) -> str:
return f"""
# Non-redirected return code reporting
# $1 : error code
# $2 : title
# $3 : actual command
function modulereport () {{
if [ $1 -gt 0 ] ; then
echo FAILURE module command failed: $2
echo Output: && module -t $3
exit
else
echo SUCCESS module command succeeded: $2
local cmd="$3"
# echo "cmd was <<$cmd>>"
echo "Now loaded:"
if [[ $cmd =~ load* ]] ; then
module -t show ${{cmd##load}}
fi
modulelist
fi {redirect}
}}
"""
def modulelistfunction() -> str:
return """
# Single line module listing
function modulelist ()
{ module -t list 2>&1 | sort | tr '\n' ' ' && echo
}
"""
def modulecommandfunction( redirect="" ) -> str:
return f"""
# Execute a module command and report result:
# $1 : title, $2 actual command, $3 nonzero to display, otherwise redirected
function modulecommand () {{
echo
echo .... $1 : module $2 {redirect}
if [ -z "$3" ] ; then
module -t $2 2>/dev/null {redirect}
else
module -t $2 {redirect}
fi
modulereport $? "$1" "$2"
}}
"""
def moduleproperfunction() -> str:
return """
# Test whether modver is properly loaded
function testmoduleproper () {
local modver=$1
local module=${modver%%/*}
local MODULE=$( echo $module | tr a-z A-Z )
local nam=TACC_${MODULE}_DIR
eval pkgdir=\\${$nam}
if [ ! -d "${pkgdir}" ] ; then
echo "FAILURE: package dir $nam=$pkgdir does not exist"
else
echo "SUCCESS: package ${modver} is at $pkgdir"
fi
for e in BIN LIB INC ; do
nam=TACC_${MODULE}_${e}
eval cmpdir=\\${$nam}
if [ ! -z "${cmpdir}" -a ! -d "${cmpdir}" ] ; then
echo "FAILURE: variable $nam set but dir $cmpdir does not exist"
fi
done
}
"""
def modulepurgefunction( redirect="" ) -> str:
return f"""
echo
echo Module setup starts here {redirect}
modulecommand "module purge" "purge"
export LMOD_SYSTEM_DEFAULT_MODULES=TACC
modulecommand "reset" "reset"
"""
def compilerloadfunction( modulepath : str,compiler : str,compilerversion : str,
redirect="" ) -> str:
return f"""
if [ ! -z "${{TACC_FAMILY_MPI}}" ] ; then
modulecommand "unload mpi" "-f unload ${{TACC_FAMILY_MPI}}"
fi
modulecommand "unload compiler" "unload ${{TACC_FAMILY_COMPILER}}"
echo .... After reset: {redirect}
modulelist {redirect}
echo .... Set modulepath {redirect}
export MODULEPATH={modulepath}
echo MODULEPATH=${{MODULEPATH}} {redirect}
modulecommand "Can we load compiler?" "avail {compiler}/{compilerversion}" display
modulecommand "Load compiler" "load {compiler}/{compilerversion}"
"""
def mpiloadfunction( mpi : str,mpiversion : str ) -> str:
return f"""
modulecommand "Load mpi" "load {mpi}/{mpiversion}"
"""
def modulesloadscript( modules_to_load : str,**kwargs ) -> str:
redirect : str = kwargs.get( "redirect","" )
loadscript : str = f"""
echo ".... Load packages <<{modules_to_load}>>" {redirect}
"""
for modver in modules_to_load.split(" "):
if isnull(modver): continue
module,slash,version = module_and_version_to_load(modver,**kwargs )
modulepropertest,_ = one_module_proper_script( [modver],**kwargs )
loadscript += f"""
modulecommand "load module: {module}{slash}{version}" "load {module}{slash}{version}"
{modulepropertest}
"""
return loadscript
modulelonglist : str = """
function modulelist ()
{
local compiler=$( module -t list "${TACC_FAMILY_COMPILER}" 2>&1 );
local mpi=$( module -t list ${TACC_FAMILY_MPI} 2>&1 );
local modules=$( module -t list 2>&1 | grep -v $compiler | grep -v $mpi | sort );
for m in $compiler $mpi cont $modules;
do
if [ $m = "cont" ]; then
echo "----------------";
else
loc=$(module -t show $m 2>&1 | sed -e 's?'${WORK}'?${WORK}?' );
echo "$m : $loc";
fi;
done
}
"""