-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·342 lines (276 loc) · 9.13 KB
/
build.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env bash
# ./build.sh [configuration_file_path] [output_file_path]
#set -ex
set -e
###################
## global settings
###################
cpp_file="model.cpp"
fortran_file="wrapper.f90"
output_folder=${2:-$(cd .. && pwd -P)/install}
###################
## utils function
###################
function print_log() {
echo "[ LOG ] " "$@"
}
# log error
function print_error() {
format=$1
shift
# shellcheck disable=SC2059
printf "[ ERROR ] ${format}" "$@"
exit
}
# Standarlize the string to make it conform to the variable name standards
function standardlize_string() {
local string=$1
string="${string##*([[:space:]])}" # remove leading spaces
string="${string%%*([[:space:]])}" # remove trailling spaces
string=$(echo -e "${string}" | tr -s '[:punct:] [:blank:]' '_') # transform punctations & spaces into `_`
string=$(echo -e "${string}" | sed 's/[^a-zA-Z0-9_]//g') # remove non-alphanumberics
# string=$(echo -e "${string}" | tr '[:upper:]' '[:lower:]') # transform to lower case
echo "${string}"
}
# Standarlize the key OR value to make it easy to parse
function standardlize_value() {
local value=$1
value="${value%%\;*}" # Remove in line right comments
value="${value%%\#*}" # Remove in line right comments
value="${value##*( )}" # Remove leading spaces
value="${value%%*( )}" # Remove trailing spaces
value="${value#\"*}" # Remove leading string quotes
value="${value%\"*}" # Remove trailing string quotes
value=$(echo -e "${value}" | sed -e 's/[[:space:]]*//g') # Remove spaces in value
echo "${value}"
}
# Usage: string_to_arr ',' string
# Output: string_arr (global)
function string_to_arr() {
local str=$2
local seperator=$1
IFS="$seperator" read -r -a string_arr <<<"$str"
# # print arr contents
# for index in "${!arr[@]}"; do
# echo "$index ${arr[index]}"
# done
}
# Usage: arr_to_string ',' arr
function arr_to_string() {
local separator="$1"
local args=("${@:2}")
local result
printf -v result '%s' "${args[@]/#/$separator}"
printf '%s' "${result:${#separator}}"
}
# Usage: reverse_arr input_arr reverse_arr
function reverse_arr() {
declare -n arr="$1" rev="$2"
for i in "${arr[@]}"; do
rev=("$i" "${rev[@]}")
done
}
# Usage: range start end step
# [start, end]
function range() {
local start=$1
local end=$2
local step=$3
echo $(seq -s ", " $start $step $end)
}
# Usage: read_templ temp_file
# ! Notice: temp_file should have escaped quotes
function read_templ() {
eval "echo \"$(cat $1)\""
}
###################
## process function
###################
# generating based on the settings
function generate_wrapper_file() {
local model_name=$1
local c_input_dim_str
c_input_dim_str=$(printf '%s\n' "${input_dim[@]}" | tac | tr '\n' ', '; echo)
#reverse_arr input_dim c_input_dim
print_log "model_name:" ${model_name}
print_log "input_dim: " $(arr_to_string ', ' "${input_dim[@]}")
print_log "output_dim: " $(arr_to_string ', ' "${output_dim[@]}")
print_log "input_type: " ${input_type}
print_log "output_type: " ${output_type}
# assure that all settings in this section are valid
if [[ -z ${input_type} || -z ${input_dim} || -z ${output_type} || -z ${output_dim} ]]; then
print_error "section %s format error:\n" "${model_name}"
fi
# generate C++ model class
read_templ ./template/cpp_body >>${cpp_file}
# generate Fortran file
## generate extern interfaces (from C++ source)
read_templ ./template/fortran_extern_interface | sed -i "/INTERFACE/ r /dev/stdin" ${fortran_file}
## generate interfaces (used by user)
read_templ ./template/fortran_interface | sed -i "/CONTAINS/ r /dev/stdin" ${fortran_file}
echo "generate ${section} successfully!"
}
# Parse the conf file
function parse_conf() {
local line_num=0
local section=''
local input_file="$1"
if [[ -z $input_file ]]; then
input_file="./configure.conf"
fi
shopt -s extglob # enable extended globbing
while read -r line; do
line_num=$((line_num + 1))
# Ignore comments & empty lines
if [[ $line =~ ^# || -z $line ]]; then
continue
fi
# if match section title
if [[ $line =~ ^"["(.+)"]".*$ ]]; then
# if section is not null
if [[ "${section}" ]]; then
generate_wrapper_file "${section}"
fi
# start with a new section, and reset all settings
section=$(standardlize_string "${BASH_REMATCH[1]}")
input_type=''
output_type=''
input_dim=''
output_dim=''
# if match key-value pair
elif [[ $line =~ ^(.*)"="(.*) ]]; then
key=$(standardlize_value "${BASH_REMATCH[1]}")
value=$(standardlize_value "${BASH_REMATCH[2]}")
# check value format
if [[ -z ${key} ]]; then
print_error 'line %d: No key name\n' "${line_num}"
elif [[ -z ${value} ]]; then
print_error 'line %d: No key name\n' "${line_num}"
fi
# retrieve value into variable
if [[ $key == "input_dim" ]]; then
string_to_arr ',' $value
input_dim=(${string_arr[@]})
elif [[ $key == "input_format" ]]; then
input_type=${value}
elif [[ $key == "output_dim" ]]; then
string_to_arr ',' $value
output_dim=(${string_arr[@]})
elif [[ $key == "output_format" ]]; then
output_type=${value}
elif [[ $key == "libtorch_path" ]]; then
libtorch_path=${value}
elif [[ $key == "compiler" ]]; then
compiler_option=${value}
fi
fi
done \
<"$input_file"
if [[ "${section}" ]]; then
generate_wrapper_file "${section}"
fi
}
function precompile_init() {
echo
echo "------------------------"
echo "PRECOMPILE Stage Start!"
echo "------------------------"
# generate header file
# cpp file
cat <./template/cpp_header >${cpp_file}
# Fortran file
cat <./template/fortran_header >${fortran_file}
}
function precompile_finish() {
echo "------------------------"
echo "PRECOMPILE Stage Finish!"
echo "------------------------"
}
function compile_start() {
echo
echo "------------------------"
echo "COMPILE Stage Start!"
echo "------------------------"
if [ -d "build" ]; then
print_log "build directory already exist, delete it"
rm -rf ./build
fi
mkdir build && cd build
}
function do_compile() {
local cxx_compiler
local fortran_compiler
# set libtorch path
while [[ -z ${libtorch_path} ]]; do
echo "Please set the libtorch library path in your computer"
read libtorch_path
done
# set compiler option
while [[ $compiler_option != 1 && $compiler_option != 2 ]]; do
echo "Please choose your compiler(1 or 2): 1. gcc 2. icc"
read compiler_option
done
if [[ $compiler_option == 1 ]]; then
cxx_compiler="c++"
fortran_compiler="gfortran"
else
cxx_compiler="icc"
fortran_compiler="ifort"
fi
# cmake generate
print_log "cmake -DCMAKE_PREFIX_PATH=${libtorch_path} -DCMAKE_MODULE_PATH=${libtorch_path}/share/cmake/Torch -DCMAKE_CXX_COMPILER=${cxx_compiler} -DCMAKE_Fortran_COMPILER=${fortran_compiler} .."
cmake -DCMAKE_PREFIX_PATH=$libtorch_path -DCMAKE_MODULE_PATH=$libtorch_path/share/cmake/Torch -DCMAKE_CXX_COMPILER=$cxx_compiler -DCMAKE_Fortran_COMPILER=$fortran_compiler ..
if [ $? -ne 0 ]; then
print_error "cmake generate fail; build abort :<\n"
fi
# cmake build
print_log "cmake --build . --config Release"
cmake --build . --config Release
if [ $? -ne 0 ]; then
print_error "cmake build fail; build abort :<\n"
fi
}
function compile_end() {
# move target files into output_folder
cd .. # return to src directory
if [ -d $output_folder ]; then
print_log "build directory already exist, delete it"
rm -rf $output_folder
fi
mkdir -p "${output_folder}/include"
cp -r ./build/bin $output_folder
cp -r ./build/lib $output_folder
cp ./build/torch_wrapper.mod "${output_folder}/include/"
echo "------------------------"
echo "COMPILE Stage Finish!"
echo "------------------------"
}
function test() {
echo "------------------------"
echo "TEST Stage Start!"
echo "------------------------"
cd "$output_folder/bin"
if ./test_cpu; then
print_log "FTA CPU test pass!"
else
print_error "FTA CPU test fail!\n"
fi
if ./test_gpu; then
print_log "FTA GPU test pass!"
else
print_error "FTA GPU test fail!\n"
fi
echo "------------------------"
echo "TEST Stage Finish!"
echo "------------------------"
}
###################
## main process
###################
precompile_init
parse_conf "$1"
precompile_finish
compile_start
do_compile
compile_end
test