Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android build with Cross Compilation for BLAS and OpenCL back-ends #848

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c954e33
Android build
lealgo May 10, 2019
6afb880
Move lzma link argument after opencl detection
lealgo May 13, 2019
c9039f3
Initial support for cross-compilation
lealgo May 19, 2019
75eb1ba
Remove unneeded -ldl from Android cross-files, remove /usr/include/ f…
lealgo May 20, 2019
f0b1c16
Add cross-file for Cortex-A57
lealgo May 25, 2019
1430507
Target lower Android API levels for wider device support
lealgo May 29, 2019
4173b92
Merge branch 'master' into android-build, getting the new eigen support
lealgo May 30, 2019
c933b1a
Add alternate paths for the tuning file on Android
lealgo Jun 4, 2019
68281b4
Build armv7a with NDK r14b
lealgo Jun 6, 2019
3d55004
Revert "Add alternate paths for the tuning file on Android"
lealgo Jun 10, 2019
241ed4a
Revert to master's tuning file location
lealgo Jun 10, 2019
090e68c
add link to zlib issue with older NDK's
lealgo Jun 16, 2019
5535d75
v0.23.0-dev -> v0.23.0-rc1
mooskagh Nov 21, 2019
12bc3f0
Merge remote-tracking branch 'origin/master' into release/0.23
mooskagh Nov 27, 2019
65e13d8
v0.23.0-rc2
mooskagh Nov 27, 2019
e4d50c4
Merge remote-tracking branch 'upstream/release/0.23' into android-build
lealgo Nov 28, 2019
b966995
Fix meson opencl vars messed up after merge
lealgo Nov 28, 2019
89a9ec3
make eigen the last blas option (#1025)
borg323 Nov 28, 2019
02fc8e0
v0.23.0-rc2 -> v0.23.0
mooskagh Dec 1, 2019
2498564
Merge remote-tracking branch 'upstream/release/0.23' into android-build
lealgo Dec 2, 2019
120566e
Add PIE switches to armv7 cross-file
lealgo Dec 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cross-files/aarch64-linux-android
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

[host_machine]
system = 'android'
cpu_family = 'arm'
cpu = 'aarch64'
endian = 'little'

[properties]
needs_exe_wrapper = true
cpp_args = ['-Dandroid']
cpp_link_args = ['-ldl', '-llog', '-static-libstdc++']

[paths]

[binaries]
c = 'aarch64-linux-android28-clang'
cpp = 'aarch64-linux-android28-clang++'
ar = 'aarch64-linux-android-ar'
strip = 'aarch64-linux-android-strip'
ld = 'aarch64-linux-android-ld'
ranlib = 'aarch64-linux-android-ranlib'
as = 'aarch64-linux-android-as'


23 changes: 23 additions & 0 deletions cross-files/arm-linux-androideabi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

[host_machine]
system = 'android'
cpu_family = 'arm'
cpu = 'armv7a'
endian = 'little'

[properties]
needs_exe_wrapper = true
cpp_args = ['-Dandroid']
cpp_link_args = ['-ldl', '-llog', '-static-libstdc++']

[paths]

[binaries]
c = 'armv7a-linux-androideabi28-clang'
cpp = 'armv7a-linux-androideabi28-clang++'
ar = 'arm-linux-androideabi-ar'
strip = 'arm-linux-androideabi-strip'
ld = 'arm-linux-androideabi-ld'
ranlib = 'arm-linux-androideabi-ranlib'
as = 'arm-linux-androideabi-as'

25 changes: 25 additions & 0 deletions cross-files/arm-linux-gnueabi
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'armv7'
endian = 'little'

[properties]
needs_exe_wrapper = true
cpp_args = []
cpp_link_args = ['-ldl', '-latomic']
c_link_args = ['-ldl', '-lpthread']

[paths]

[binaries]
c = 'arm-linux-gnueabi-gcc'
cpp = 'arm-linux-gnueabi-g++'
ar = 'arm-linux-gnueabi-ar'
strip = 'arm-linux-gnueabi-strip'
ld = 'arm-linux-gnueabi-ld'
ranlib = 'arm-linux-gnueabi-ranlib'
as = 'arm-linux-gnueabi-as'
pkgconfig = 'arm-linux-gnueabi-pkg-config'

19 changes: 19 additions & 0 deletions cross_compilation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# Cross compilation for Android, Linux ARM, etc.

## Pre-requisites

You'll need a toolchain like NDK or GNU ARM. A set of cross-files is provided, you can choose one of them or create your own. These are the platforms currently tested:

* Android aarch64
* Android armv7-a
* Linux ARM

## Build

Once the toolchain is installed and a Meson cross file is chosen, to build simply run:

`CC=clang CXX=clang++ ./build.sh --cross-file cross-files/arm-linux-androideabi`

You might have to remove the build/ directory if it exists.

21 changes: 12 additions & 9 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if cc.get_id() == 'clang' or cc.get_id() == 'gcc'
add_project_arguments('-Wextra', language : 'cpp')
add_project_arguments('-pedantic', language : 'cpp')

if get_option('buildtype') == 'release'
if get_option('buildtype') == 'release' and cc.has_argument('-march=native')
add_project_arguments('-march=native', language : 'cpp')
endif
endif
Expand Down Expand Up @@ -132,9 +132,11 @@ if host_machine.system() == 'windows'
files += 'src/utils/filesystem.win32.cc'
else
files += 'src/utils/filesystem.posix.cc'
deps += [
cc.find_library('pthread'),
if host_machine.system() != 'android'
deps += [
cc.find_library('pthread'),
]
endif
endif


Expand Down Expand Up @@ -296,24 +298,24 @@ if get_option('build_backends')
## OpenCL
## ~~~~~

has_opencl = false
opencl_dep = []

opencl_libdirs = get_option('opencl_libdirs')
opencl_lib=cc.find_library('OpenCL', dirs: opencl_libdirs, required: false)

opencl_framework=dependency('OpenCL', required: false)
if opencl_framework.found()
deps += [ opencl_framework ]
has_opencl = true
opencl_dep += [ opencl_framework ]

elif opencl_lib.found()

deps += [ opencl_lib ]
has_opencl = true
opencl_dep += [ opencl_lib ]
else
opencl_dep += [ subproject('opencl').get_variable('opencl_dep') ]

endif

if get_option('opencl') and has_opencl
if get_option('opencl')

opencl_files = [
'src/neural/opencl/network_opencl.cc',
Expand All @@ -329,6 +331,7 @@ if get_option('build_backends')

includes += include_directories(get_option('opencl_include'))
files += opencl_files
deps += opencl_dep
has_backends = true

endif
Expand Down
2 changes: 1 addition & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ option('openblas_include',

option('opencl_include',
type: 'array',
value: ['/usr/include/'],
# value: ['/usr/include/'],
Copy link
Contributor Author

@lealgo lealgo May 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to comment this path as it seems to confuse the Android NDK, it mixes the headers and fail compilation. The GNU toolchain doesn't have that problem. Any idea on how to solve this?

description: 'Paths to OpenCL include directories')

option('tensorflow_libdir',
Expand Down
6 changes: 6 additions & 0 deletions src/neural/opencl/OpenCLTuner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@

#include "utils/logging.h"

// Use the temporary directory on Android as the engine may run with restricted file access
#ifdef android
const auto TUNER_FILE_LOCAL = std::string("/data/local/tmp/leelaz_opencl_tuning");
#else
const auto TUNER_FILE_LOCAL = std::string("leelaz_opencl_tuning");
#endif

constexpr auto MAX_ERROR = 1e-4f;

static void sgemmBatched_ref(const std::vector<float>& a,
Expand Down
4 changes: 4 additions & 0 deletions subprojects/opencl.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[wrap-git]
directory = opencl
url = https://github.com/borg323/OpenCL-ICD-Loader.git
revision = meson