Skip to content

Commit

Permalink
[RPC] Update build support for cross compiling apps/cpp_rpc with Open…
Browse files Browse the repository at this point in the history
…CL (apache#6229)

* Standardize support for building and cross compiling apps/cpp_rpc.
* Add cmake coverage for building the C++ RPC server binary
  and update documentation.
* Add support for linking against custom OpenCL SDK employing
  a custom find_opencl macro. This can be useful when cross
  compiling with a custom OpenCL device driver.

* Update OpenCL related documentation.

* Add embedded linux build instructions to apps/cpp_rpc/README.md and
ensure pthread is linked against when OS=Linux is defined. Remove
outdated apps/cpp_rpc/Makefile.
  • Loading branch information
csullivan authored and Trevor Morris committed Aug 26, 2020
1 parent 2af7076 commit 6705b17
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 73 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ project(tvm C CXX)
# Utility functions
include(cmake/util/Util.cmake)
include(cmake/util/FindCUDA.cmake)
include(cmake/util/FindOpenCL.cmake)
include(cmake/util/FindVulkan.cmake)
include(cmake/util/FindLLVM.cmake)
include(cmake/util/FindROCM.cmake)
Expand Down Expand Up @@ -77,9 +78,6 @@ tvm_option(USE_TARGET_ONNX "Build with ONNX Codegen support" OFF)
tvm_option(USE_ARM_COMPUTE_LIB "Build with Arm Compute Library" OFF)
tvm_option(USE_ARM_COMPUTE_LIB_GRAPH_RUNTIME "Build with Arm Compute Library graph runtime" OFF)

if(USE_CPP_RPC AND UNIX)
message(FATAL_ERROR "USE_CPP_RPC is only supported with WIN32. Use the Makefile for non-Windows.")
endif()

# include directories
include_directories(${CMAKE_INCLUDE_PATH})
Expand Down
12 changes: 12 additions & 0 deletions apps/cpp_rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ if(WIN32)
target_compile_definitions(tvm_rpc PUBLIC -DNOMINMAX)
endif()

if (OS)
if (OS STREQUAL "Linux")
set_property(TARGET tvm_rpc PROPERTY LINK_FLAGS -lpthread)
endif()
endif()

if(USE_OPENCL)
if (ANDROID_ABI)
set_property(TARGET tvm_rpc PROPERTY LINK_FLAGS -fuse-ld=gold)
endif()
endif()

target_include_directories(
tvm_rpc
PUBLIC "../../include"
Expand Down
53 changes: 0 additions & 53 deletions apps/cpp_rpc/Makefile

This file was deleted.

44 changes: 29 additions & 15 deletions apps/cpp_rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,38 @@
This folder contains a simple recipe to make RPC server in c++.

## Usage (Non-Windows)
- Build tvm runtime
- Make the rpc executable [Makefile](Makefile).
`make CXX=/path/to/cross compiler g++/ TVM_RUNTIME_DIR=/path/to/tvm runtime library directory/ OS=Linux`
if you want to compile it for embedded Linux, you should add `OS=Linux`.
if the target os is Android, you doesn't need to pass OS argument.
You could cross compile the TVM runtime like this:
```
cd tvm
mkdir arm_runtime
cp cmake/config.cmake arm_runtime
cd arm_runtime
cmake .. -DCMAKE_CXX_COMPILER="/path/to/cross compiler g++/"
make runtime
- Configure the tvm cmake build with `config.cmake` ensuring that `USE_CPP_RPC` is set to `ON` in the config.
- If cross compiling for Android, add the following options to the cmake config or specify them when invoking cmake:
```
# Whether to build the C++ RPC server binary
set(USE_CPP_RPC ON)
# Path to the Android NDK cmake toolchain
set(CMAKE_TOOLCHAIN_FILE $ENV{ANDROID_NDK}/build/cmake/android.toolchain.cmake)
# The Android ABI and platform to target
set(ANDROID_ABI "arm64-v8a")
set(ANDROID_PLATFORM android-28)
```
- Similarly, if cross compiling for embedded Linux add the following options to cmake config:
```
# Needed to ensure pthread is linked
set(OS Linux)
# Path to the desired C++ cross compiler
set(CMAKE_CXX_COMPILER /path/to/cross/compiler/executable)
```
- If linking against a custom device OpenCL library is needed, in the config specify the path to the OpenCL SDK containing the include/CL headers and lib/ or lib64/libOpenCL.so:
```
set(USE_OPENCL /path/to/opencl-sdk)
```

- From within the configured tvm build directory, compile `tvm_runtime` and the `tvm_rpc` server:
```
cd $TVM_ROOT/build
make -jN tvm_runtime tvm_rpc
```
- Use `./tvm_rpc server` to start the RPC server

## Usage (Windows)
- Build tvm with the argument -DUSE_CPP_RPC
- Configure the tvm cmake build with `config.cmake` ensuring that `USE_CPP_RPC` is set to `ON` in the config.
- Install [LLVM pre-build binaries](https://releases.llvm.org/download.html), making sure to select the option to add it to the PATH.
- Verify Python 3.6 or newer is installed and in the PATH.
- Use `<tvm_output_dir>\tvm_rpc.exe` to start the RPC server
Expand All @@ -59,4 +73,4 @@ Command line usage
```

## Note
Currently support is only there for Linux / Android / Windows environment and proxy mode doesn't be supported currently.
Currently support is only there for Linux / Android / Windows environment and proxy mode isn't supported currently.
8 changes: 8 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ set(USE_SDACCEL OFF)
set(USE_AOCL OFF)

# Whether enable OpenCL runtime
#
# Possible values:
# - ON: enable OpenCL with cmake's auto search
# - OFF: disable OpenCL
# - /path/to/opencl-sdk: use specific path to opencl-sdk
set(USE_OPENCL OFF)

# Whether enable Metal runtime
Expand Down Expand Up @@ -96,6 +101,9 @@ set(RUST_SGX_SDK "/path/to/rust-sgx-sdk")
# Whether enable RPC runtime
set(USE_RPC ON)

# Whether to build the C++ RPC server binary
set(USE_CPP_RPC OFF)

# Whether embed stackvm into the runtime
set(USE_STACKVM_RUNTIME OFF)

Expand Down
6 changes: 4 additions & 2 deletions cmake/modules/OpenCL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.

# OPENCL Module
find_package(OpenCL QUIET)
find_opencl(${USE_OPENCL})

if(OpenCL_FOUND)
# always set the includedir when cuda is available
Expand Down Expand Up @@ -49,7 +49,9 @@ else()
endif(USE_AOCL)

if(USE_OPENCL)
find_package(OpenCL REQUIRED)
if (NOT OpenCL_FOUND)
find_package(OpenCL REQUIRED)
endif()
message(STATUS "Build with OpenCL support")
file(GLOB RUNTIME_OPENCL_SRCS src/runtime/opencl/*.cc)
list(APPEND TVM_RUNTIME_LINKER_LIBS ${OpenCL_LIBRARIES})
Expand Down
70 changes: 70 additions & 0 deletions cmake/util/FindOpenCL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

#######################################################
# Enhanced version of find OpenCL.
#
# Usage:
# find_opencl(${USE_OPENCL})
#
# - When USE_OPENCL=ON, use auto search
# - When USE_OPENCL=/path/to/opencl-sdk-path, use the sdk.
# Can be useful when cross compiling and cannot rely on
# CMake to provide the correct library as part of the
# requested toolchain.
#
# Provide variables:
#
# - OpenCL_FOUND
# - OpenCL_INCLUDE_DIRS
# - OpenCL_LIBRARIES
#

macro(find_opencl use_opencl)
set(__use_opencl ${use_opencl})
if(IS_DIRECTORY ${__use_opencl})
set(__opencl_sdk ${__use_opencl})
message(STATUS "Custom OpenCL SDK PATH=" ${__use_opencl})
elseif(IS_DIRECTORY $ENV{OPENCL_SDK})
set(__opencl_sdk $ENV{OPENCL_SDK})
else()
set(__opencl_sdk "")
endif()

if(__opencl_sdk)
set(OpenCL_INCLUDE_DIRS ${__opencl_sdk}/include)
if (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY STREQUAL "ONLY")
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
endif()
find_library(OpenCL_LIBRARIES NAMES OpenCL PATHS ${__opencl_sdk}/lib ${__opencl_sdk}/lib64)
if(OpenCL_LIBRARIES)
set(OpenCL_FOUND TRUE)
endif()
endif(__opencl_sdk)

# No user provided OpenCL include/libs found
if(NOT OpenCL_FOUND)
if(__use_opencl STREQUAL "ON")
find_package(OpenCL QUIET)
endif()
endif()

if(OpenCL_FOUND)
message(STATUS "OpenCL_INCLUDE_DIRS=" ${OpenCL_INCLUDE_DIRS})
message(STATUS "OpenCL_LIBRARIES=" ${OpenCL_LIBRARIES})
endif(OpenCL_FOUND)
endmacro(find_opencl)

0 comments on commit 6705b17

Please sign in to comment.