-
Notifications
You must be signed in to change notification settings - Fork 5
/
system_capabilities.sh
executable file
·300 lines (254 loc) · 7.98 KB
/
system_capabilities.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
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
#############################################################################################
#
#############################################################################################
function get_optimisation_level()
{
# Detect the platform (similar to $OSTYPE)
local OS="`uname`"
case $OS in
'Linux')
declare -a all_opt_capabilities=(avx avx2 fma sse sse2 ssse3 sse4_1 sse4_2)
# Grab the flags
flags=$(grep flags /proc/cpuinfo |head -1|cut -f2- -d:)
declare -A flag_map
# Create an associatve array and add to it
for i in $flags
do
i=$(tr [A-Z] [a-z] <<< $i)
flag_map[$i]=$i
done
echo "Gathered flags in map as follows: " ${!flag_map[@]}
declare -i num_optimisations=0
for cap in "${all_opt_capabilities[@]}"
do
if [[ ! -z "${flag_map[${cap}]}" ]]
then
num_optimisations=$(( num_optimisations + 1 ))
fi
done
local optim_level=$1
# If we found all capabilities, we flag it as "all"
if [[ $num_optimisations -eq "${#all_opt_capabilities[*]}" ]]
then
optim_level="all"
elif [[ $num_optimisations -eq 0 ]]
then
optim_level="none"
else
optim_level="safe"
fi
eval "$1=$optim_level"
;;
*)
eval "$1=all"
;;
esac
}
#############################################################################################
#
#############################################################################################
function get_compute_engine_type()
{
local engine_type=$1
engine_type="cpu"
# Run lspci and see if we have any NVIDIA drivers loaded
declare -i nvidia_found=$(lspci | grep -i nvidia | wc -l)
# We should have at least one line from lspci if we ahave NVIDIA installed
if [[ $nvidia_found -gt 0 ]]
then
if [[ ! -z ${CUDA_HOME+x} && -f $CUDA_HOME/lib64/libcudart.so ]]
then
cuda_version=$(cat ${CUDA_HOME}/version.txt | cut -d' ' -f3)
echo "Determined CUDA version to be $cuda_version"
cuda_major_ver=$(echo $cuda_version | awk -F '.' '{print $1}')
engine_type="cuda_${cuda_major_ver}"
fi
fi
eval "$1=$engine_type"
}
#############################################################################################
#
#############################################################################################
function get_compiler()
{
# Detect the platform (similar to $OSTYPE)
local OS="`uname`"
case $OS in
'Linux')
local gcc_version="gcc_$(gcc -dumpversion | cut -f1 -d.)"
eval "$1=$gcc_version"
;;
'FreeBSD')
local gcc_version="gcc_$(gcc -dumpversion | cut -f1 -d.)"
eval "$1=$gcc_version"
;;
'WindowsNT')
eval "$1=msvcpp"
;;
'Darwin')
eval "$1=apple_clang"
;;
'SunOS')
local gcc_version="gcc_$(gcc -dumpversion | cut -f1 -d.)"
eval "$1=$gcc_version"
;;
'AIX')
val "$1=xlc_r"
;;
*)
eval "$1=UNKNOWN"
;;
esac
}
#############################################################################################
#
#############################################################################################
function get_env_spec()
{
local COMPILER=$1
local COMPUTE_ENGINE_TYPE=$2
local OPTIM_LEVEL=$3
local env_script_spec="$COMPILER-$COMPUTE_ENGINE_TYPE-$OPTIM_LEVEL"
echo $env_script_spec
}
#############################################################################################
#
#############################################################################################
function compute_env_script_spec()
{
local OPTIM_LEVEL=""
get_optimisation_level 'OPTIM_LEVEL'
echo "Determined optimisation level to be $OPTIM_LEVEL"
local COMPUTE_ENGINE_TYPE=""
get_compute_engine_type 'COMPUTE_ENGINE_TYPE'
echo "Determined compute engine type to be $COMPUTE_ENGINE_TYPE"
local COMPILER=""
get_compiler 'COMPILER'
echo "Determined compiler to be $COMPILER"
local env_script_spec=$(get_env_spec $COMPILER $COMPUTE_ENGINE_TYPE $OPTIM_LEVEL)
echo "Determined env spec to be $env_script_spec"
eval "$1=$env_script_spec"
}
#############################################################################################
#
#############################################################################################
function get_no_opt_cpu_env_script_spec()
{
local OPTIM_LEVEL="none"
local COMPUTE_ENGINE_TYPE="cpu"
local COMPILER=""
get_compiler 'COMPILER'
local env_script_spec=$1
env_script_spec=$(get_env_spec $COMPILER $COMPUTE_ENGINE_TYPE $OPTIM_LEVEL)
eval "$1=$env_script_spec"
}
#############################################################################################
#
#############################################################################################
function get_safe_cpu_env_script_spec()
{
local OPTIM_LEVEL="safe"
local COMPUTE_ENGINE_TYPE="cpu"
local COMPILER=""
get_compiler 'COMPILER'
local env_script_spec=$1
env_script_spec=$(get_env_spec $COMPILER $COMPUTE_ENGINE_TYPE $OPTIM_LEVEL)
eval "$1=$env_script_spec"
}
#############################################################################################
#
#############################################################################################
function get_all_opt_cpu_env_script_spec()
{
local OPTIM_LEVEL="all"
local COMPUTE_ENGINE_TYPE="cpu"
local COMPILER=""
get_compiler 'COMPILER'
local env_script_spec=$1
env_script_spec=$(get_env_spec $COMPILER $COMPUTE_ENGINE_TYPE $OPTIM_LEVEL)
eval "$1=$env_script_spec"
}
#############################################################################################
#
#############################################################################################
function run_env_script_spec()
{
local env_script_spec=$1
local conan_script_home="${CONAN_USER_HOME}/CMakeModules/${env_script_spec}"
envSetupScript="${conan_script_home}/activate_run.sh"
if [ -f "${envSetupScript}" ]
then
export CONAN_SCRIPT_HOME=$conan_script_home
. "${envSetupScript}"
return 0
fi
return 1
}
#############################################################################################
#
#############################################################################################
function set_env_script()
{
local run_env_script_spec
compute_env_script_spec 'run_env_script_spec'
echo "Determined env script spec to be $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
local engine_type_1
get_compute_engine_type 'engine_type_1'
echo "Determined engine type to be $engine_type_1"
local compiler
get_compiler 'compiler'
if [ "$engine_type_1" != "cpu" ]
then
run_env_script_spec=$(get_env_spec $compiler $engine_type_1 "all")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
run_env_script_spec=$(get_env_spec $compiler $engine_type_1 "safe")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
run_env_script_spec=$(get_env_spec $compiler $engine_type_1 "none")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
fi
run_env_script_spec=$(get_env_spec $compiler "cpu" "all")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
run_env_script_spec=$(get_env_spec $compiler "cpu" "safe")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
run_env_script_spec=$(get_env_spec $compiler "cpu" "none")
echo "Testing for env script spec $run_env_script_spec"
run_env_script_spec $run_env_script_spec
if [[ $? -eq 0 ]]
then
return 0
fi
echo "Unable to determine which environemnt to use!!!"
echo -e "CONAN_USER_HOME = ${CONAN_USER_HOME}:\n" && ls -la ${CONAN_USER_HOME}
echo -e "CONAN_SCRIPT_HOME = ${CONAN_SCRIPT_HOME}:\n" && ls -la ${CONAN_SCRIPT_HOME}
return 1
}