forked from rapidsai/cucim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·1283 lines (1070 loc) · 42.5 KB
/
run
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Copyright (c) 2020-2021, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
init_globals() {
if [ "$0" != "/bin/bash" ]; then
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
export RUN_SCRIPT_FILE="$(readlink -f "$0")"
else
export RUN_SCRIPT_FILE="$(readlink -f "${BASH_SOURCE[0]}")"
fi
export TOP=$(git rev-parse --show-toplevel || $(dirname "${RUN_SCRIPT_FILE}"))
}
################################################################################
# Utility functions
################################################################################
#######################################
# Get list of available commands from a given input file.
#
# Available commands and command summary are extracted by checking a pattern
# "_desc() { echo '".
# Section title is extracted by checking a pattern "# Section: ".
# This command is used for listing available commands in CLI.
#
# e.g.)
# "# Section: String/IO functions"
# => "# String/IO functions"
# "to_lower_desc() { echo 'Convert to lower case"
# => "to_lower ----------------- Convert to lower case"
#
# Arguments:
# $1 - input file that defines commands
# Returns:
# Print list of available commands from $1
#######################################
get_list_of_available_commands() {
local file_name="$1"
if [ ! -e "$1" ]; then
echo "$1 doesn't exist!"
fi
local line_str='--------------------------------'
local IFS= cmd_lines="$(IFS= cat "$1" | grep -E -e "^(([[:alpha:]_[:digit:]]+)_desc\(\)|# Section: )" | sed "s/_desc() *{ *echo '/ : /")"
local line
while IFS= read -r line; do
local cmd=$(echo "$line" | cut -d":" -f1)
local desc=$(echo "$line" | cut -d":" -f2-)
if [ "$cmd" = "# Section" ]; then
c_echo B "${desc}"
else
# there is no substring operation in 'sh' so use 'cut'
local dash_line="$(echo "${line_str}" | cut -c ${#cmd}-)" # = "${line_str:${#cmd}}"
c_echo Y " ${cmd}" w " ${dash_line} ${desc}"
fi
# use <<EOF, not '<<<"$cmd_lines"' to be executable in sh
done <<EOF
$cmd_lines
EOF
}
my_cat_prefix() {
local IFS
local prefix="$1"
local line
while IFS= read -r line; do
echo "${prefix}${line}" # -e option doesn't work in 'sh' so disallow escaped characters
done <&0
}
c_str() {
local old_color=39
local old_attr=0
local color=39
local attr=0
local text=""
#local no_change=0
for i in "$@"; do
case "$i" in
r|R)
color=31
;;
g|G)
color=32
;;
y|Y)
color=33
;;
b|B)
color=34
;;
p|P)
color=35
;;
c|C)
color=36
;;
w|W)
color=37
;;
z|Z)
color=0
;;
esac
case "$i" in
l|L|R|G|Y|B|P|C|W)
attr=1
;;
n|N|r|g|y|b|p|c|w)
attr=0
;;
z|Z)
attr=0
;;
*)
text="${text}$i"
esac
if [ ${old_color} -ne ${color} ] || [ ${old_attr} -ne ${attr} ]; then
text="${text}\033[${attr};${color}m"
old_color=$color
old_attr=$attr
fi
done
/bin/echo -en "$text"
}
c_echo() {
local old_opt="$(shopt -op xtrace)" # save old xtrace option
set +x # unset xtrace
local text="$(c_str "$@")"
/bin/echo -e "$text\033[0m"
eval "${old_opt}" # restore old xtrace option
}
echo_err() {
>&2 echo "$@"
}
c_echo_err() {
>&2 c_echo "$@"
}
printf_err() {
>&2 printf "$@"
}
get_item_ranges() {
local indexes="$1"
local list="$2"
echo -n "$(echo "${list}" | xargs | cut -d " " -f "${indexes}")"
return $?
}
get_unused_ports() {
local num_of_ports=${1:-1}
local start=${2:-49152}
local end=${3:-61000}
comm -23 \
<(seq ${start} ${end} | sort) \
<(ss -tan | awk '{print $4}' | while read line; do echo ${line##*\:}; done | grep '[0-9]\{1,5\}' | sort -u) \
| shuf | tail -n ${num_of_ports} # use tail instead head to avoid broken pipe in VSCode terminal
}
newline() {
echo
}
info() {
c_echo W "$(date -u '+%Y-%m-%d %H:%M:%S') [INFO] " Z "$@"
}
error() {
echo R "$(date -u '+%Y-%m-%d %H:%M:%S') [ERROR] " Z "$@"
}
fatal() {
echo R "$(date -u '+%Y-%m-%d %H:%M:%S') [FATAL] " Z "$@"
echo
if [ -n "${SCRIPT_DIR}" ]; then
exit 1
fi
}
run_command() {
local status=0
local cmd="$*"
c_echo B "$(date -u '+%Y-%m-%d %H:%M:%S') " W "\$ " G "${cmd}"
[ "$(echo -n "$@")" = "" ] && return 1 # return 1 if there is no command available
"$@"
status=$?
unset IFS
return $status
}
retry() {
local retries=$1
shift
local count=0
until run_command "$@"; do
exit=$?
wait=$((2 ** count))
count=$((count + 1))
if [ $count -lt $retries ]; then
info "Retry $count/$retries. Exit code=$exit, Retrying in $wait seconds..."
sleep $wait
else
fatal "Retry $count/$retries. Exit code=$exit, no more retries left."
return 1
fi
done
return 0
}
#==================================================================================
# Section: Build
#==================================================================================
build_manylinux2014_desc() { echo 'Build manylinux2014 image
Arguments:
$1 - cuda version (e.g., 110, 111)
'
}
build_manylinux2014() {
local cuda_version="${1:-110}"
run_command docker build -f ${TOP}/Dockerfile-cuda${cuda_version} -t gigony/manylinux2014-x64:cuda${cuda_version} ${TOP}
read -n 1 -r -p "$(c_str R "Do you want to update dockcross-manylinux2014-x64 with " G "cuda${cuda_version}" R " (y/n)?")"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sed -i -e "s/manylinux2014-x64:cuda.../manylinux2014-x64:cuda${cuda_version}/g" ${TOP}/dockcross-manylinux2014-x64
c_echo W "Done"
fi
}
build_local_libcucim_() {
local source_folder=${1:-${TOP}}
local build_type=${2:-debug}
local build_type_str=${3:-Debug}
local prefix=${4:-}
local build_folder=${source_folder}/build-${build_type}
local CMAKE_CMD=${CMAKE_CMD:-cmake}
pushd ${source_folder} > /dev/null
# Copy cufile SDK from host system to temp/cuda
copy_gds_files_ $source_folder
${CMAKE_CMD} -S ${source_folder} -B ${build_folder} -G "Unix Makefiles" \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
-DCMAKE_PREFIX_PATH=${prefix} \
-DCMAKE_BUILD_TYPE=${build_type_str} \
-DCMAKE_INSTALL_PREFIX=${source_folder}/install
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target cucim -- -j $(nproc)
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target install -- -j $(nproc)
popd
}
build_local_cuslide_() {
local source_folder=${1:-${TOP}/cpp/plugins/cucim.kit.cuslide}
local build_type=${2:-debug}
local build_type_str=${3:-Debug}
local prefix=${4:-}
local build_folder=${source_folder}/build-${build_type}
local CMAKE_CMD=${CMAKE_CMD:-cmake}
pushd ${source_folder} > /dev/null
${CMAKE_CMD} -S ${source_folder} -B ${build_folder} -G "Unix Makefiles" \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
-DCMAKE_BUILD_TYPE=${build_type_str} \
-DCMAKE_PREFIX_PATH=${prefix} \
-DCMAKE_INSTALL_PREFIX=${source_folder}/install
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target cucim.kit.cuslide -- -j $(nproc)
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target install -- -j $(nproc)
popd
}
build_local_cucim_() {
local source_folder=${1:-${TOP}/python}
local build_type=${2:-debug}
local build_type_str=${3:-Debug}
local prefix=${4:-}
local build_folder=${source_folder}/build-${build_type}
local CMAKE_CMD=${CMAKE_CMD:-cmake}
pushd ${source_folder} > /dev/null
local python_library=$(python3 -c "import distutils.sysconfig as sysconfig, os; print(os.path.join(sysconfig.get_config_var('LIBDIR'), sysconfig.get_config_var('LDLIBRARY')))")
local python_include_dir=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
${CMAKE_CMD} -S ${source_folder} -B ${build_folder} -G "Unix Makefiles" \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
-DCMAKE_BUILD_TYPE=${build_type_str} \
-DCMAKE_PREFIX_PATH=${prefix} \
-DCMAKE_INSTALL_PREFIX=${source_folder}/install \
-DPYTHON_EXECUTABLE=$(which python3) \
-DPYTHON_LIBRARY=${python_library} \
-DPYTHON_INCLUDE_DIR=${python_include_dir}
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target cucim -- -j $(nproc)
${CMAKE_CMD} --build ${build_folder} --config ${build_type_str} --target install -- -j $(nproc)
popd
}
build_local_desc() { echo 'Build locally
Compile binaries locally
Arguments:
$1 - subcommand [all|libcucim|cuslide|cucim] (default: all)
$2 - build type [debug|release|rel-debug] (default: debug)
'
}
build_local() {
local subcommand="${1:-all}"
local build_type="${2:-debug}"
local build_type_str="Debug"
local prefix=${3:-}
[ "$build_type" = "debug" ] && build_type_str="Debug"
[ "$build_type" = "release" ] && build_type_str="Release"
[ "$build_type" = "rel-debug" ] && build_type_str="RelWithDebInfo"
local old_opt="$(shopt -op errexit);$(shopt -op nounset)" # save old shopts
set -eu
if [ "$subcommand" = "all" ] || [ "$subcommand" = "libcucim" ]; then
build_local_libcucim_ ${TOP} ${build_type} ${build_type_str} ${prefix}
fi
if [ "$subcommand" = "all" ] || [ "$subcommand" = "cuslide" ]; then
build_local_cuslide_ ${TOP}/cpp/plugins/cucim.kit.cuslide ${build_type} ${build_type_str} ${prefix}
fi
if [ "$subcommand" = "all" ] || [ "$subcommand" = "cucim" ]; then
build_local_cucim_ ${TOP}/python ${build_type} ${build_type_str} ${prefix}
fi
# Remove existing library files at python/cucim/src/cucim/clara
rm -f ${TOP}/python/cucim/src/cucim/clara/*.so*
if [ "$subcommand" = "all" ] || [ "$subcommand" = "cucim" ]; then
# We don't need to copy binary if executed by conda-build
if [ "${CONDA_BUILD:-}" != "1" ]; then
# Copy .so files to pybind's build folder
# we don't need to copy symbolic links. Instead copy only libcucim.so.0 (without symbolic link)
cp ${TOP}/build-$build_type/lib*/libcucim.so.0 ${TOP}/python/cucim/src/cucim/clara/
cp -P ${TOP}/cpp/plugins/cucim.kit.cuslide/build-$build_type/lib*/cucim* ${TOP}/python/cucim/src/cucim/clara/
# Copy .so files from pybind's build folder to cucim Python source folder
# Since wheel file doesn't support symbolic link (https://github.com/pypa/wheel/issues/203),
# we don't need to copy symbolic links. Instead copy only libcucim.so.0 (without symbolic link)
#find ${TOP}/python/install/lib -maxdepth 1 -type f -exec cp {} ${TOP}/python/cucimrc/cucim/clara\;
cp ${TOP}/python/build-$build_type/lib/cucim/_cucim.*.so ${TOP}/python/cucim/src/cucim/clara/
# cp ${TOP}/python/build-$build_type/lib/cucim.*.so ${TOP}/python/cucim/src/cucim/clara/
# cp ${TOP}/python/build-$build_type/lib/libcucim.so.0 ${TOP}/python/cucim/src/cucim/clara/
# find ${TOP}/python/build-$build_type/lib -maxdepth 1 -type f -name "lib*.so" -exec cp {} ${TOP}/python/cucim/src/cucim/clara \;
fi
fi
eval "${old_opt}" # restore old shopts
}
copy_gds_files_() {
local root_folder=${1:-${TOP}}
local cufile_search="${root_folder}/temp/cuda/include:${root_folder}/temp/cuda/lib64" #"/usr/local/cuda/include:/usr/local/cuda/lib64 ${PREFIX:-}/include:${PREFIX:-}/lib ${CONDA_PREFIX:-}/include:${CONDA_PREFIX:-}/lib ${root_folder}/temp/cuda/include:${root_folder}/temp/cuda/lib64"
local candidate
local cufile_include
local cufile_lib
for candidate in ${cufile_search}; do
cufile_include="$(echo $candidate | cut -d: -f1)"
cufile_lib="$(echo $candidate | cut -d: -f2)"
if [ -f ${cufile_include}/cufile.h ] && [ -f ${cufile_lib}/libcufile.so ]; then
c_echo W "GDS client library is available at '${cufile_include}/cufile.h' and '${cufile_lib}/libcufile.so'"
break
fi
cufile_include=""
cufile_lib=""
done
if [ ! -f ${cufile_include}/cufile.h ]; then
c_echo_err Y "GDS client library is not available! Downloading the redistributable package to get cufile.h and libraries."
run_command rm -rf ${root_folder}/temp/cuda
run_command mkdir -p ${root_folder}/temp/cuda/include ${root_folder}/temp/cuda/lib64
local temp_tgz_dir=$(mktemp -d)
pushd ${temp_tgz_dir}
run_command wget https://developer.download.nvidia.com/gds/redist/rel-0.95.0/gds-redistrib-0.95.0.tgz
run_command tar xzvf gds-redistrib-0.95.0.tgz
run_command cp -P gds-redistrib-0.95.0/targets/x86_64-linux/include/cufile.h ${root_folder}/temp/cuda/include/
run_command cp -P gds-redistrib-0.95.0/targets/x86_64-linux/lib/* ${root_folder}/temp/cuda/lib64/
popd > /dev/null
run_command rm -r ${temp_tgz_dir}
else
run_command mkdir -p ${root_folder}/temp/cuda/include ${root_folder}/temp/cuda/lib64
if [ "${cufile_include}" != "${root_folder}/temp/cuda/include" ]; then
run_command cp -Pf ${cufile_include}/cufile.h ${root_folder}/temp/cuda/include/ || true
run_command cp -Pf ${cufile_lib}/libcufile* ${root_folder}/temp/cuda/lib64/ || true
fi
fi
}
build_python_package_desc() { echo 'Build Python package
Note: This command does not remove `dist` folder before building.
'
}
build_python_package() {
local ret=0
local old_opt="$(shopt -op errexit);$(shopt -op nounset)" # save old shopts
set -eu
# Copy cufile SDK from host system to temp/cuda
copy_gds_files_
run_command ${TOP}/dockcross-manylinux2014-x64 ./run build_python_package_
ret=$?
eval "${old_opt}" # restore old shopts
return ${ret}
}
repair_wheel_() {
local wheel="$1"
local dest="${2:-./}"
local PLAT="${3:-manylinux2014-x86_64}"
if ! auditwheel show "$wheel"; then
echo "Skipping non-platform wheel ${wheel}"
else
$(head -1 $(which auditwheel) | cut -d'!' -f2) $TOP/scripts/auditwheel_repair.py repair --plat "${PLAT}" -w ${dest} "${wheel}"
fi
}
build_python_package_() {
local SRC_ROOT=${1:-${TOP}}
local BUILD_ROOT=${2:-${TOP}/temp}
local DEST_ROOT=${3:-${TOP}/dist}
local CUCIM_SDK_PATH=${4:-${BUILD_ROOT}/libcucim}
local old_opt="$(shopt -op errexit);$(shopt -op nounset)" # save old shopts
set -eu
# Clear CMakeCache.txt to use the latest options
run_command rm -f ${BUILD_ROOT}/libcucim/CMakeCache.txt
run_command rm -f ${BUILD_ROOT}/cuslide/CMakeCache.txt
run_command rm -f ${BUILD_ROOT}/cucim/CMakeCache.txt
# Create a folder for .whl file that repair_wheel_ is not applied.
TEMP_PYPKG_DIR=${BUILD_ROOT}/py_pkg # $(mktemp -d)
c_echo b "TEMP_PYPKG_DIR=${TEMP_PYPKG_DIR}"
mkdir -p $TEMP_PYPKG_DIR
rm -rf $TEMP_PYPKG_DIR/*
# trap 'rm -rf ${TEMP_PYPKG_DIR}' EXIT
local CMAKE_CMD=${CMAKE_CMD:-cmake}
local CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE:-Release}
local NUM_THREADS=${NUM_THREADS:-$(nproc)}
local PLAT=manylinux2014_x86_64
local pybins
mkdir -p ${DEST_ROOT}
# Remove existing library files at build root
rm -rf ${BUILD_ROOT}/libcucim/lib/*
rm -rf ${BUILD_ROOT}/libcucim/install/lib*/lib*
rm -rf ${BUILD_ROOT}/cuslide/lib/*
rm -rf ${BUILD_ROOT}/cuslide/install/lib*/cucim*
rm -rf ${BUILD_ROOT}/cucim/lib/cucim/*.so*
rm -rf ${BUILD_ROOT}/cucim/install/lib/*.so*
# Build libcucim
${CMAKE_CMD} -S ${SRC_ROOT} -B ${BUILD_ROOT}/libcucim \
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
-DCMAKE_INSTALL_PREFIX=${CUCIM_SDK_PATH}/install
${CMAKE_CMD} --build ${BUILD_ROOT}/libcucim --config ${CMAKE_BUILD_TYPE} --target cucim -- -j ${NUM_THREADS}
${CMAKE_CMD} --build ${BUILD_ROOT}/libcucim --config ${CMAKE_BUILD_TYPE} --target install -- -j ${NUM_THREADS}
# Build cuslide plugin
${CMAKE_CMD} -S ${SRC_ROOT}/cpp/plugins/cucim.kit.cuslide -B ${BUILD_ROOT}/cuslide \
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
-DCMAKE_INSTALL_PREFIX=${BUILD_ROOT}/cuslide/install \
-DCUCIM_SDK_PATH=${CUCIM_SDK_PATH}
${CMAKE_CMD} --build ${BUILD_ROOT}/cuslide --config ${CMAKE_BUILD_TYPE} --target cucim.kit.cuslide -- -j ${NUM_THREADS}
${CMAKE_CMD} --build ${BUILD_ROOT}/cuslide --config ${CMAKE_BUILD_TYPE} --target install -- -j ${NUM_THREADS}
# Build Python bind
pybins="$(echo /opt/python/*/bin)"
if [ "${pybins}" = "/opt/python/*/bin" ]; then
pybins="$(dirname $(which python3))" # for building at host
fi
for PYBIN in ${pybins}; do
local python_library=$(${PYBIN}/python3 -c "import distutils.sysconfig as sysconfig; import os; print(os.path.join(sysconfig.get_config_var('LIBDIR'), sysconfig.get_config_var('LDLIBRARY')))")
local python_include_dir=$(${PYBIN}/python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
${CMAKE_CMD} -S ${SRC_ROOT}/python -B ${BUILD_ROOT}/cucim \
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
-DCMAKE_INSTALL_PREFIX=${BUILD_ROOT}/cucim/install \
-DCUCIM_SDK_PATH=${CUCIM_SDK_PATH} \
-DPYTHON_EXECUTABLE=${PYBIN}/python3 \
-DPYTHON_LIBRARY=${python_library} \
-DPYTHON_INCLUDE_DIR=${python_include_dir}
${CMAKE_CMD} --build ${BUILD_ROOT}/cucim --config ${CMAKE_BUILD_TYPE} --target cucim -- -j ${NUM_THREADS}
${CMAKE_CMD} --build ${BUILD_ROOT}/cucim --config ${CMAKE_BUILD_TYPE} --target install -- -j ${NUM_THREADS}
done
# Remove existing library files at python/cucim/src/cucim/clara
rm -f ${SRC_ROOT}/python/cucim/src/cucim/clara/*.so*
# Copy .so files to pybind's build folder
# (it uses -P to copy symbolic links as they are)
cp -P ${BUILD_ROOT}/libcucim/install/lib*/lib* ${BUILD_ROOT}/cucim/install/lib/
cp -P ${BUILD_ROOT}/cuslide/install/lib*/cucim* ${BUILD_ROOT}/cucim/install/lib/
# Copy .so files from pybind's build folder to cucim Python source folder
# Since wheel file doesn't support symbolic link (https://github.com/pypa/wheel/issues/203),
# we don't need to copy symbolic links. Instead copy only libcucim.so.0 (without symbolic link)
#find ${BUILD_ROOT}/cucim/install/lib -maxdepth 1 -type f -exec cp {} ${SRC_ROOT}/python/cucim/src/cucim/clara/ \;
cp ${BUILD_ROOT}/cucim/install/lib/_cucim.*.so ${SRC_ROOT}/python/cucim/src/cucim/clara/
cp ${BUILD_ROOT}/cucim/install/lib/cucim.*.so ${SRC_ROOT}/python/cucim/src/cucim/clara/
cp ${BUILD_ROOT}/cucim/install/lib/libcucim.so.0 ${SRC_ROOT}/python/cucim/src/cucim/clara/
find ${BUILD_ROOT}/cucim/install/lib -maxdepth 1 -type f -name "lib*.so" -exec cp {} ${SRC_ROOT}/python/cucim/src/cucim/clara/ \;
cd ${SRC_ROOT}/python/cucim
# Remove build folder
rm -rf ${SRC_ROOT}/python/cucim/build
# Compile wheels (one python binary is enough)
pybins="$(echo /opt/python/*/bin)"
[ ! -e /opt/python/cp36-cp36m/bin ] && pybins="$(dirname $(which python3))" # for building at host
pybins="$(echo /opt/python/*/bin)"
if [ "${pybins}" = "/opt/python/*/bin" ]; then # if multiple python binaries not found
pybins="$(dirname $(which python3))" # for building at host
else
pybins=/opt/python/cp36-cp36m/bin # use Python 3.6 for executing setup.py
fi
for PYBIN in ${pybins}; do # /opt/python/*/bin
run_command "${PYBIN}/python3" setup.py bdist_wheel -p $PLAT -d ${TEMP_PYPKG_DIR}
done
# Do not bundle external shared libraries for now.
# (CUDA-related libraries cannot be redistributed without EULA messages confirmed by user)
# Here, we just copy to dist folder.
# cp ${TEMP_PYPKG_DIR}/*.whl ${DEST_ROOT}/
# Bundle external shared libraries into the wheels
for whl in ${TEMP_PYPKG_DIR}/*.whl; do
repair_wheel_ "$whl" "${DEST_ROOT}/" "${PLAT}"
done
# run_command rm -rf ${TEMP_PYPKG_DIR}
# trap -- EXIT
# Copy cpp packages and examples
mkdir -p ${DEST_ROOT}/examples/cpp
cp -P -r ${BUILD_ROOT}/libcucim/install ${DEST_ROOT}/
cp -P -r ${BUILD_ROOT}/cuslide/install/lib/*.so ${DEST_ROOT}/install/lib/
cp -r ${SRC_ROOT}/examples/cpp/tiff_image ${DEST_ROOT}/examples/cpp/
cp ${BUILD_ROOT}/libcucim/CMakeLists.txt.examples.release ${DEST_ROOT}/examples/cpp/CMakeLists.txt
# # Install packages and test
# for PYBIN in /opt/python/*/bin/; do
# "${PYBIN}/pip" install python-manylinux-demo --no-index -f /io/wheelhouse
# (cd "$HOME"; "${PYBIN}/nosetests" pymanylinuxdemo)
# done
# python setup.py bdist_wheel -p manylinux2014-x86_64
eval "${old_opt}" # restore old shopts
}
build_package_desc() { echo 'Build package for release (& gen_docs)
'
}
build_package() {
local SRC_ROOT=${1:-${TOP}}
local BUILD_ROOT=${2:-${TOP}/temp}
local DEST_ROOT=${3:-${TOP}/dist}
local CUCIM_SDK_PATH=${4:-${BUILD_ROOT}/libcucim}
# Clean dist folder
mkdir -p ${DEST_ROOT}
[ -n "${DEST_ROOT}" ] && run_command sudo rm -rf ${DEST_ROOT}/*
build_python_package ${SRC_ROOT} ${BUILD_ROOT} ${DEST_ROOT} ${CUCIM_SDK_PATH}
gen_docs ${DEST_ROOT}/docs
copy_data_ ${SRC_ROOT} ${BUILD_ROOT} ${DEST_ROOT}
# Copy the built wheel file into ${TOP}/notebooks
run_command cp ${DEST_ROOT}/*.whl ${TOP}/notebooks/
}
copy_data_() {
c_echo W "Copy necessary files for packaging..."
local SRC_ROOT=${1:-${TOP}}
local BUILD_ROOT=${2:-${TOP}/temp}
local DEST_ROOT=${3:-${TOP}/dist}
# Create notebooks folder (add notebooks and scripts)
run_command mkdir -p ${DEST_ROOT}/notebooks/static_images
run_command cp $(git ls-files ${SRC_ROOT}/notebooks/*.ipynb) ${DEST_ROOT}/notebooks/
run_command cp ${SRC_ROOT}/notebooks/static_images/*.png ${DEST_ROOT}/notebooks/static_images/
# Create docker folder
run_command mkdir -p ${DEST_ROOT}/docker
run_command cp ${SRC_ROOT}/docker/*-jupyter{,-gds,.txt} ${DEST_ROOT}/docker/
run_command cp ${SRC_ROOT}/docker/*-claratrain{,.txt} ${DEST_ROOT}/docker/
run_command cp ${SRC_ROOT}/docker/*-cmake ${DEST_ROOT}/docker/
run_command cp ${SRC_ROOT}/docker/cufile.json ${DEST_ROOT}/docker/ # Copy cufile.json
# Copy main script
run_command cp ${SRC_ROOT}/scripts/run-dist ${DEST_ROOT}/run
# Create .dockerignore to speed up docker run
echo "notebooks" > ${DEST_ROOT}/.dockerignore
# Copy license files
run_command cp -r ${SRC_ROOT}/3rdparty ${SRC_ROOT}/LICENSE* ${DEST_ROOT}/
}
#==================================================================================
# Section: Example
#==================================================================================
download_testdata_desc() { echo 'Download test data from Docker Hub
'
}
download_testdata() {
c_echo W "Downloading test data..."
run_command mkdir -p ${TOP}/notebooks/input
if [ ! -e ${TOP}/notebooks/input/image.tif ]; then
run_command rm -rf ${TOP}/notebooks/input
id=$(docker create gigony/svs-testdata:little-big)
run_command docker cp $id:/input ${TOP}/notebooks
run_command docker rm -v $id
c_echo G "Test data is downloaded to ${TOP}/notebooks/input!"
else
c_echo G "Test data already exists at ${TOP}/notebooks/input!"
fi
}
launch_notebooks_desc() { echo 'Launch jupyter notebooks
Arguments:
-p <port> - port number
-h <host> - hostname to serve documentation on (default: 0.0.0.0)
-g <path to NVMe device> - launch GDS-enabled container
'
}
launch_notebooks() {
local OPTIND
local port=$(get_unused_ports 1 10000 10030)
local host='0.0.0.0'
local gds_postfix=''
local gds_nvme_path=''
while getopts 'p:h:g:' option;
do
case "${option}" in
p)
port="$OPTARG"
;;
h)
host="$OPTARG"
;;
g)
gds_postfix='-gds'
[ -z "$OPTARG" ] && c_echo_err R "Please specify NVMe path!" && return 1
gds_nvme_path=$(readlink -f "$OPTARG")
[ ! -d "$gds_nvme_path" ] && c_echo_err R "Folder $gds_nvme_path doesn't exist!" && return 1
# Copy cufile SDK from host system to temp/cuda
copy_gds_files_
;;
*)
return 1
esac
done
shift $((OPTIND-1))
download_testdata
run_command cp ${TOP}/dist/*.whl ${TOP}/notebooks
run_command nvidia-docker build -t cucim-jupyter${gds_postfix} -f ${TOP}/docker/Dockerfile-jupyter${gds_postfix}-dev ${TOP}
[ $? -ne 0 ] && return 1
c_echo W "Port " G "$port" W " would be used...(" B "http://$(hostname -I | cut -d' ' -f 1):${port}" W ")"
if [ -z "${gds_postfix}" ]; then
run_command nvidia-docker run --gpus all -it --rm \
-v ${TOP}/notebooks:/notebooks \
-p ${port}:${port} \
cucim-jupyter \
-c "echo -n 'Enter New Password: '; jupyter lab --ServerApp.password=\"\$(python3 -u -c \"from jupyter_server.auth import passwd;pw=input();print(passwd(pw));\" | egrep 'sha|argon')\" --ServerApp.root_dir=/notebooks --allow-root --port=${port} --ip=${host} --no-browser"
else
local MNT_PATH=/nvme
local GDS_IMAGE=cucim-jupyter${gds_postfix}
local BUILD_VER=`uname -r`
local NV_DRIVER=`nvidia-smi -q -i 0 | sed -n 's/Driver Version.*: *\(.*\) *$/\1/p'`
echo "using nvidia driver version $NV_DRIVER on kernel $BUILD_VER"
local ofed_version=$(ofed_info -s | grep MLNX)
if [ $? -eq 0 ]; then
local rdma_core=$(dpkg -s libibverbs-dev | grep "Source: rdma-core")
if [ $? -eq 0 ]; then
local CONFIG_MOFED_VERSION=$(echo $ofed_version | cut -d '-' -f 2)
echo "Found MOFED version $CONFIG_MOFED_VERSION"
fi
local MLNX_SRCS="--volume /usr/src/mlnx-ofed-kernel-${CONFIG_MOFED_VERSION}:/usr/src/mlnx-ofed-kernel-${CONFIG_MOFED_VERSION}:ro"
local MOFED_DEVS="--net=host --volume /sys/class/infiniband_verbs:/sys/class/infiniband_verbs/ "
fi
docker run \
--ipc host \
-it \
--rm \
--gpus all \
-v ${TOP}/notebooks:/notebooks \
-p ${port}:${port} \
--volume /run/udev:/run/udev:ro \
--volume /sys/kernel/config:/sys/kernel/config/ \
--volume /usr/src/nvidia-$NV_DRIVER:/usr/src/nvidia-$NV_DRIVER:ro ${MLNX_SRCS}\
--volume /dev:/dev:ro \
--privileged \
--env NV_DRIVER=${NV_DRIVER} \
--volume /lib/modules/$BUILD_VER/:/lib/modules/$BUILD_VER \
--volume "${MNT_PATH}:/notebooks/nvme:rw" \
${MOFED_DEVS} \
${GDS_IMAGE} \
-c "echo -n 'Enter New Password: '; jupyter lab --ServerApp.password=\"\$(python3 -u -c \"from jupyter_server.auth import passwd;pw=input();print(passwd(pw));\" | egrep 'sha|argon')\" --ServerApp.root_dir=/notebooks --allow-root --port=${port} --ip=${host} --no-browser"
fi
}
#==================================================================================
# Section: Documentation
#==================================================================================
install_tox_() {
if ! command -v tox > /dev/null; then
c_echo G "tox" W " doesn't exists. Installing " G "tox" W "..."
if [ -n "${CONDA_PREFIX}" ]; then
run_command pip3 install tox
else
run_command pip3 install --user tox
fi
hash -r
fi
}
gen_docs_desc() { echo 'Generate document
Generated docs would be avaialable at ${TOP}/dist/docs.
Returns:
None
Exit code:
exit code returned from generating document
'
}
gen_docs() {
local OUTPUT_FOLDER=${1:-${TOP}/dist/docs}
local ret=0
pushd ${TOP}/python/cucim > /dev/null
# Install prerequisites
install_tox_
# Remove existing files in dist/docs
run_command rm -rf ${OUTPUT_FOLDER}/*
# Copy notebook files to python/cucim/dist/docs/notebooks
run_command mkdir -p ${TOP}/python/cucim/docs/notebooks
run_command rm -rf ${TOP}/python/cucim/docs/notebooks/*
run_command mkdir -p ${TOP}/python/cucim/docs/notebooks/static_images
run_command cp -r $(git ls-files ${TOP}/notebooks/*.ipynb) ${TOP}/python/cucim/docs/notebooks/
run_command cp -r ${TOP}/notebooks/static_images/*.png ${TOP}/python/cucim/docs/notebooks/static_images/
tox -e docs -- ${OUTPUT_FOLDER}
ret=$?
# Remove jupyter_execute folder explicitly until the issue is solved
# https://github.com/executablebooks/MyST-NB/issues/129
rm -rf $(dirname ${OUTPUT_FOLDER})/jupyter_execute
popd > /dev/null
return $ret
}
gen_docs_dev_desc() { echo 'Generate document
Launch dev-server for sphinx.
Generated docs would be avaialable at ${TOP}/python/cucim/dist/docs.
Arguments:
-p <port> - port number
-h <host> - hostname to serve documentation on (default: 0.0.0.0)
Returns:
None
Exit code:
exit code returned from generating document
'
}
gen_docs_dev() {
local OPTIND
local port=9999
local host=0.0.0.0
while getopts 'p:h:' option;
do
case "${option}" in
p)
port="$OPTARG"
;;
h)
host="$OPTARG"
;;
*)
echo_err R "Invalid option!"
return 1
esac
done
pushd ${TOP}/python/cucim > /dev/null
# Install prerequisites
install_tox_
# Remove existing files in python/cucim/dist/docs
run_command rm -rf ${TOP}/python/cucim/dist/docs/*
# Copy notebook files to python/cucim/dist/docs/notebooks
run_command mkdir -p ${TOP}/python/cucim/docs/notebooks/static_images
run_command rm -rf ${TOP}/python/cucim/docs/notebooks/*
run_command cp -r $(git ls-files ${TOP}/notebooks/*.ipynb) ${TOP}/python/cucim/docs/notebooks/
run_command cp -r ${TOP}/notebooks/static_images/*.png ${TOP}/python/cucim/docs/notebooks/static_images/
run_command tox -e docs-dev -- --port ${port} --host ${host} docs dist/docs
popd > /dev/null
}
publish_docs_desc() { echo 'Publish generated documents to the server
Publish generated documents to $GITLAB_PUBLISH_PROJECT_URL
The web page is available at the followings:
- $GITLAB_PUBLISH_PROJECT_URL
Arguments:
$1 - If specified, force creating a tag with the specified tag name.
Use "latest" tag to make public documents up to date.
Returns:
None
Exit code:
exit code returned from publishing documents
'
}
publish_docs() {
local release_version="$(cat ${TOP}/VERSION)"
local release_tag="${1:-v${release_version}}"
# Import secrets
import_env_vars_ || return 1
c_echo W "Publishing documents to Gitlab Pages..."
TEMP_DOCS_DIR=$(mktemp -d)
c_echo r "TEMP_DOCS_DIR=${TEMP_DOCS_DIR}"
trap 'rm -rf ${TEMP_DOCS_DIR}' EXIT
pushd ${TEMP_DOCS_DIR} > /dev/null
git clone ${GITLAB_PUBLISH_GIT_URL} --branch init --single-branch
cd ${GITLAB_PUBLISH_PROJECT_NAME}
mkdir -p dist/docs
cp -rf ${TOP}/dist/docs/* dist/docs/
git checkout -b pages
git add dist/docs
git commit -am "Upload home page v$(cat ${TOP}/VERSION)"
git tag -f ${release_tag}
git push -f origin pages
git push -f origin ${release_tag}
popd > /dev/null
# Create tag if custom (such as 'latest') tag is specified
if [ -n "${1:-}" ]; then
git tag -f "$1"
git push -f origin "$1"
fi
run_command rm -rf ${TEMP_DOCS_DIR}
trap -- EXIT
c_echo W "Checkout the published webpage!"
}
#==================================================================================
# Section: Release
#==================================================================================
bump_version_desc() { echo 'Bump version (internal use)
Executes bump2version(https://github.com/c4urself/bump2version) with
a post-processing.
`bump2version` package would be installed if not available.
Post-processing includes:
- Updating version in *.ipynb files
Returns:
Outputs executed by bump2version
Exit code:
exit code returned from bump2version
'
}
bump_version() {
local part=${1:-}
local ret=0
if ! command -v bump2version > /dev/null; then
c_echo G "bump2version" W " doesn't exists. Installing " G "bump2version" W "..."
if [ -n "${CONDA_PREFIX}" ]; then
run_command pip3 install bump2version
else
run_command pip3 install --user bump2version
fi
hash -r
fi
pushd $TOP/python/cucim
case "$part" in
major|minor|patch)
local current_version="$(bump2version --dry-run --list --allow-dirty $part | grep -Po 'current_version=\K[\d\.]+')"
local new_version="$(bump2version --dry-run --list --allow-dirty $part | grep -Po 'new_version=\K[\d\.]+')"
c_echo W "current_version=" G "${current_version}"
c_echo W "new_version=" G "${new_version}"
local version_from_tag="$(git tag | grep -xE 'v[0-9\.]+' | sort --version-sort | tail -n 1 | tr -d 'v')"
# Print error if versions are not in sync.
if [ "${current_version}" != "${version_from_tag}" ]; then
c_echo_err R "version (" W "${current_version}" R ") from VERSION file doesn't match with version (" W "${version_from_tag}" R ") from tag."
c_echo_err W "Please sync those two versions!"
popd
return 1
fi
bump2version "$@"
ret=$?
if [ $ret -eq 0 ]; then
local file_name
for file_name in ${TOP}/notebooks/*.ipynb; do
c_echo b "processing " g "${file_name} ..."
sed -ie "s#cucim-${current_version}#cucim-${new_version}#" "${file_name}"
sed -ie "s#cucim ${current_version}#cucim ${new_version}#" "${file_name}"
sed -ie "s#cucim==${current_version}#cucim==${new_version}#" "${file_name}"
sed -ie "s#cuclara-image ${current_version}#cuclara-image ${new_version}#" "${file_name}"
sed -ie "s#cuclara-image-${current_version}#cuclara-image-${new_version}#" "${file_name}"
done