-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathConfigureCrossCompile.cmake
More file actions
92 lines (88 loc) · 5.04 KB
/
Copy pathConfigureCrossCompile.cmake
File metadata and controls
92 lines (88 loc) · 5.04 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
# This file adds the necessary configurations to cross compile
# mlpack for embedded systems. You need to set the following variables
# from the command line: CMAKE_SYSROOT and TOOLCHAIN_PREFIX.
# This file will compile OpenBLAS if it is downloaded and it is not
# available on your system in order to find the BLAS library. If OpenBLAS will
# be compiled, the OPENBLAS_TARGET variable must be set. This can be done
# by, e.g., setting ARCH_NAME (which will set OPENBLAS_TARGET in
# `flags-config.cmake`).
if (CMAKE_CROSSCOMPILING)
include(CMake/crosscompile-arch-config.cmake)
if (NOT CMAKE_SYSROOT AND (NOT TOOLCHAIN_PREFIX))
message(FATAL_ERROR "Neither CMAKE_SYSROOT nor TOOLCHAIN_PREFIX are set; please set both of them and try again.")
elseif(NOT CMAKE_SYSROOT)
message(FATAL_ERROR "Cannot configure: CMAKE_SYSROOT must be set when performing cross-compiling!")
elseif(NOT TOOLCHAIN_PREFIX)
message(FATAL_ERROR "Cannot configure: TOOLCHAIN_PREFIX must be set when performing cross-compiling!")
endif()
# Now make sure that we can still compile a simple test program.
# (This ensures we didn't add any bad CXXFLAGS.)
# Note that OUTPUT_VARIABLE is only available in newer versions of CMake!
# CMake 3.23 (silently) introduced the variable.
include(CheckCXXSourceCompiles)
if (CMAKE_VERSION VERSION_LESS "3.22.0")
check_cxx_source_compiles("int main() { return 0; }" COMPILE_SUCCESS)
if (NOT COMPILE_SUCCESS)
message(FATAL_ERROR "The C++ cross-compiler at ${CMAKE_CXX_COMPILER} is "
"not able to compile a trivial test program. Check the CXXFLAGS!")
endif ()
else ()
check_cxx_source_compiles("int main() { return 0; }" COMPILE_SUCCESS
OUTPUT_VARIABLE COMPILE_OUTPUT)
if (NOT COMPILE_SUCCESS)
message(FATAL_ERROR "The C++ cross-compiler at ${CMAKE_CXX_COMPILER} is "
"not able to compile a trivial test program. Compiler output:\n\n"
"${COMPILE_OUTPUT}")
endif ()
endif ()
endif()
macro(search_openblas version)
set(BLA_STATIC ON)
find_package(BLAS)
if (NOT BLAS_FOUND OR (NOT BLAS_LIBRARIES))
if(NOT OPENBLAS_TARGET)
message(FATAL_ERROR "Cannot compile OpenBLAS: OPENBLAS_TARGET is not set. Either set that variable, or set BOARD_NAME correctly!")
endif()
get_deps(https://github.com/xianyi/OpenBLAS/releases/download/v${version}/OpenBLAS-${version}.tar.gz OpenBLAS OpenBLAS-${version}.tar.gz)
if (NOT MSVC)
if (NOT EXISTS "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a")
set(ENV{COMMON_OPT} "${CMAKE_OPENBLAS_FLAGS}") # Pass our flags to OpenBLAS
# NN-on-device memory fit (riscv64). OpenBLAS lazily allocates a per-GEMM
# scratch buffer (BUFFER_SIZE -- 32 MB on riscv64) sized for its default
# N-block (SGEMM_DEFAULT_R = 12288). That single 32 MB allocation does
# not fit on a ~28 MB device, so the first f32 matrix-multiply -- e.g. the
# neural network's dense layers -- is OOM-killed at startup. (Random
# forest and KNN avoid that big GEMM path, which is why only the NN
# failed.) Our matrices are tiny, so shrink the N-block to 2048 and the
# buffer to 8 MB for the generic riscv64 target; there is no measurable
# speed cost. See README.md / BINARY_SIZE.md for the full investigation.
if(OPENBLAS_TARGET STREQUAL "RISCV64_GENERIC")
execute_process(COMMAND sed -i
"/#ifdef RISCV64_GENERIC/,/#endif/{s/_DEFAULT_R 12288/_DEFAULT_R 2048/;s/_DEFAULT_R 8192/_DEFAULT_R 2048/;s/_DEFAULT_R 4096/_DEFAULT_R 2048/}"
"${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/param.h")
execute_process(COMMAND sed -i
"s/( 32 << 20)/( 8 << 20)/"
"${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/common_riscv64.h")
message(STATUS
"OpenBLAS(riscv64): shrank GEMM buffer 32MB->8MB and N-block "
"12288->2048 so the f32 neural net fits in ~28 MB RAM.")
endif()
# USE_THREAD=0 / NUM_THREADS=1 / USE_OPENMP=0: build a single-threaded
# OpenBLAS. On a single-core, 64 MB target (Milk-V Duo) the threaded
# build spawns worker threads that busy-wait (spin) at startup, which
# starves the main thread on one core and hangs the program before it
# even runs. Single-threaded also removes the per-thread GEMM buffers.
execute_process(COMMAND make TARGET=${OPENBLAS_TARGET} BINARY=${OPENBLAS_BINARY} HOSTCC=gcc CC=${CMAKE_C_COMPILER} FC=${CMAKE_FORTRAN_COMPILER} NO_SHARED=1 USE_THREAD=0 NUM_THREADS=1 USE_OPENMP=0
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version})
endif()
file(GLOB OPENBLAS_LIBRARIES "${CMAKE_BINARY_DIR}/deps/OpenBLAS-${version}/libopenblas.a")
set(BLAS_openblas_LIBRARY ${OPENBLAS_LIBRARIES})
set(LAPACK_openblas_LIBRARY ${OPENBLAS_LIBRARIES})
set(BLA_VENDOR OpenBLAS)
set(BLAS_FOUND ON)
endif()
endif()
find_library(GFORTRAN NAMES libgfortran.a)
find_library(PTHREAD NAMES libpthread.a)
set(CROSS_COMPILE_SUPPORT_LIBRARIES ${GFORTRAN} ${PTHREAD})
endmacro()