diff options
author | Cedric Nugteren <web@cedricnugteren.nl> | 2017-10-29 16:45:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-29 16:45:56 +0100 |
commit | 19c53f6dd06e1fc2b3f71ab738bd97af070fe627 (patch) | |
tree | 894631428f17b75dc144def614ffde885beeca21 | |
parent | fa6e5e67f585b77d34c3031c176de9a0f7904aa9 (diff) | |
parent | f24d611e575656a8e472f46b9c5c54f3c9634378 (diff) |
Merge pull request #208 from CNugteren/android_support
Added Android support
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 40 | ||||
-rw-r--r-- | README.md | 26 | ||||
-rw-r--r-- | src/clpp11.hpp | 6 | ||||
-rw-r--r-- | src/utilities/android.hpp | 47 | ||||
-rw-r--r-- | test/correctness/tester.cpp | 1 |
6 files changed, 107 insertions, 14 deletions
@@ -7,6 +7,7 @@ Development (next version) * Two CUDA API sample programs are added: SGEMM and DAXPY * All correctness tests and performance clients work on CUDA like they did for OpenCL - Kernels are now cached based on their tuning parameters: fits the use-case of 'OverrideParameters' +- Cross-compiling for Android is now supported using CMake; instructions are added to the README - Improved performance for small GEMM problems by going from 3 to 1 optional temporary buffers - Various minor fixes and enhancements - Added tuned parameters for various devices (see README) diff --git a/CMakeLists.txt b/CMakeLists.txt index 610e5149..28093980 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,23 +165,35 @@ if(TUNERS) endif() endif() -# Locates the reference BLAS libraries in case the tests need to be compiled. The "FindclBLAS.cmake", -# "FindCBLAS.cmake" and "FindcuBLAS.cmake" are included. -if(CLIENTS OR TESTS) - find_package(CBLAS) - if(OPENCL) - find_package(clBLAS) +# Don't search for system libraries when cross-compiling +if(${CMAKE_SYSTEM_NAME} STREQUAL Android) + if(TESTS) + message(STATUS "Compilation of the tests disabled for the Android target") + set(TESTS OFF) endif() - if(CUBLAS) - find_package(cuBLAS) + if(CLIENTS) + message(STATUS "Head-to-head performance comparison not supported in the clients for the Android target") endif() - if(NOT CLBLAS_FOUND AND NOT CBLAS_FOUND) - if(TESTS) - message(STATUS "Could NOT find clBLAS nor a CPU BLAS, disabling the compilation of the tests") - set(TESTS OFF) +else() + + # Locates the reference BLAS libraries in case the tests need to be compiled. The "FindclBLAS.cmake", + # "FindCBLAS.cmake" and "FindcuBLAS.cmake" are included. + if(CLIENTS OR TESTS) + find_package(CBLAS) + if(OPENCL) + find_package(clBLAS) + endif() + if(CUBLAS) + find_package(cuBLAS) endif() - if(CLIENTS) - message(STATUS "Could NOT find clBLAS nor a CPU BLAS, head-to-head performance comparison not supported in the clients") + if(NOT CLBLAS_FOUND AND NOT CBLAS_FOUND) + if(TESTS) + message(STATUS "Could NOT find clBLAS nor a CPU BLAS, disabling the compilation of the tests") + set(TESTS OFF) + endif() + if(CLIENTS) + message(STATUS "Could NOT find clBLAS nor a CPU BLAS, head-to-head performance comparison not supported in the clients") + endif() endif() endif() endif() @@ -331,6 +331,32 @@ Since there is no half-precision data-type in C or C++, OpenCL provides the `cl_ The `samples/haxpy.c` example shows how to use these convenience functions when calling the half-precision BLAS routine HAXPY. +Notes for Android +------------- + +For deployment on Android, there are three options to consider. + +First of all, you can use Google's recommended route of installing Android Studio with the NDK, and then use the JNI to interface to the CLBlast library. For this, we refer to the official Android Studio documentation and the online tutorials. + +Alternatively, you can cross-compile the library and the test/client/tuner executables directly. To do so, first install the NDK, then find your vendor's OpenCL library (e.g. in `/system/vendor/lib`), get OpenCL headers from the Khronos registry, and invoke CMake as follows: + + cmake .. \ + -DCMAKE_SYSTEM_NAME=Android \ + -DCMAKE_SYSTEM_VERSION=19 \ # Set the appropriate Android API level + -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a \ # Set the appropriate device architecture (e.g. armeabi-v7a or arm64-v8a) + -DCMAKE_ANDROID_NDK=$ANDROID_NDK_PATH \ # Assumes $ANDROID_NDK_PATH points to your NDK installation + -DCMAKE_ANDROID_STL_TYPE=gnustl_static \ + -DOPENCL_ROOT=/path/to/vendor/OpenCL/lib/folder/ # Should contain libOpenCL.so and CL/cl.h + +For any potential issues, first check [cmath 'has not been declared' errors](https://stackoverflow.com/questions/45183525/compilation-error-with-ndk-using-cstatic/46433625). Also, if you are encountering errors such as `#error Bionic header ctype.h does not define either _U nor _CTYPE_U`, make sure CMake is not including system paths. + +Finally, a third option is to use the [Collective Knowledge framework](https://github.com/ctuning/ck) in combination with the NDK, e.g. as follows: + + sudo pip install ck + ck pull repo:ck-math + ck install package:lib-clblast-master-universal --target_os=android21-arm64 + + Known issues ------------- diff --git a/src/clpp11.hpp b/src/clpp11.hpp index 2335caef..82fc44fd 100644 --- a/src/clpp11.hpp +++ b/src/clpp11.hpp @@ -43,6 +43,7 @@ #include <memory> // std::shared_ptr #include <numeric> // std::accumulate #include <cstring> // std::strlen +#include <cstdio> // fprintf, stderr // OpenCL #define CL_USE_DEPRECATED_OPENCL_1_2_APIS // to disable deprecation warnings @@ -52,6 +53,11 @@ #include <CL/opencl.h> #endif +// Android support (missing C++11 functions to_string, stod, and stoi) +#ifdef __ANDROID__ + #include "utilities/android.hpp" +#endif + // Exception classes #include "cxpp11_common.hpp" diff --git a/src/utilities/android.hpp b/src/utilities/android.hpp new file mode 100644 index 00000000..64e9b3f4 --- /dev/null +++ b/src/utilities/android.hpp @@ -0,0 +1,47 @@ + +// ================================================================================================= +// This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This +// project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- +// width of 100 characters per line. +// +// Author(s): +// Cedric Nugteren <www.cedricnugteren.nl> +// +// This file provides macro's and definitions to make compilation work for Android. Note that this +// header should only be included when compiling for Android, e.g. when __ANDROID__ is defined. +// +// ================================================================================================= + +#ifndef CLBLAST_ANDROID_HPP_ +#define CLBLAST_ANDROID_HPP_ + +// ================================================================================================= + +#include <cstdlib> +#include <string> +#include <sstream> + +namespace std { + +// No support for these standard library functions when compiling with the GNU C++ STL +template<typename T> +std::string to_string(T value) { + std::ostringstream os; + os << value; + return os.str(); +} +inline double stod(const std::string& value) { + return std::atof(value.c_str()); +} +inline int stoi( const std::string& str, std::size_t* pos = 0, int base = 10) { + char * p_end; + const auto result = std::strtol(str.c_str(), &p_end, base); + return result; +} + +} + +// ================================================================================================= + +// CLBLAST_ANDROID_HPP_ +#endif diff --git a/test/correctness/tester.cpp b/test/correctness/tester.cpp index 165aca35..a10736ea 100644 --- a/test/correctness/tester.cpp +++ b/test/correctness/tester.cpp @@ -15,6 +15,7 @@ #include <vector> #include <iostream> #include <cmath> +#include <cstdio> #include <cstdlib> #include "test/correctness/tester.hpp" |