Files
mlpack/doc/embedded/crosscompile_example.md
T
Omar ShritandRyan Curtin 58115633dd Add Findmlpack.cmake (#3872)
* Add Findmlack.cmake

I opened this PR, because it was so difficult to revive the old one.

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Adding the comments from the previous PR

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add the docs again

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add depedencies functions

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Make the filename compatible with the standard

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move the dependencies search from CMakeLists

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove MATHJAX

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Refactor the CMake into dependencies

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove Findarmadillo since it exist in CMake

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove Arpack as well, already in the main stream

Signed-off-by: Omar Shrit <omar@avontech.fr>

* This needs to go, tooo old

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move cereal and ensmallen to mlpack

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Get rid of the empty files

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove Doxygen generation file

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Finish Findmlpack.cmake file

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Increase Cmake version to 3.11 to avoid the warning

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add POST_BUILD to get rid of the warning

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove really old support for MACOS 11 and 9

This is more like 15 years old already

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Already specified no need to do it again

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Fix according to comments from @rcurtin

Signed-off-by: Omar Shrit <omar@avontech.fr>

* add message for armadillo

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Try to fix windows and cross CI at the same time

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Let us add brutally STB and Armadillo

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove STB image independant file from here

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Adding more into findmlpack

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move things from CMake as they are not directly relevant

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move the autdownloader to findmlpack

Signed-off-by: Omar Shrit <omar@avontech.fr>

* remove the autodownload

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove cmakeextras, added for obsure reasons

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove license for really old stuff

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Re-order the file how it should be

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move out of dependency

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove Dependencies, as it is no longer used

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add stb to this file again

Signed-off-by: Omar Shrit <omar@avontech.fr>

* reorganize functions

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add find armadillo

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove Dependencies files and autdownload

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Make it use the internal functions

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Finishing most of the cmake modifications

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Fix a couple of last things

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Change the name and remove old file

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Fix all comments left by @rcurtin

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Fix the docs for each of the function

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Move the cross compilation to inside fetch and make it as an auto detect

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Cut the parts that are refactored

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Fix function name

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Remove the ""

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Fix text and code

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Apply comments from @rcurtin

Signed-off-by: Omar Shrit <omar@avontech.fr>

* modify CMakeLists accordingly

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Add PR in the history

Signed-off-by: Omar Shrit <omar@avontech.fr>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update CMake/mlpack.cmake

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update HISTORY.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

* Update doc/embedded/crosscompile_example.md

Co-authored-by: Ryan Curtin <ryan@ratml.org>

---------

Signed-off-by: Omar Shrit <omar@avontech.fr>
Co-authored-by: Ryan Curtin <ryan@ratml.org>
2025-02-25 21:00:53 +01:00

4.7 KiB

Crosscompile mlpack example for an embedded hardware

In this article, we explore how to add mlpack to a CMake project that cross-compiles code to embedded hardware. See also these related other guides, which may be useful to read before this one:

Cloning mlpack example respository

mlpack has an example repository that shows a number of applications and use cases for mlpack, including embedded deployment. In this tutorial, we are interested in the embedded/ directory, which provides a CMake project template that compiles a random forest application to embedded hardware. This project template can be adapted to a new project, or its pieces can be incorporated into an existing CMake project.

The first step is to clone the examples repository:

git clone git@github.com:mlpack/examples.git

Next, let's look at the CMakeLists.txt (e.g. the CMake configuration) in the embedded/crosscompile_random_forest/ directory.

CMakeLists.txt

The first part of the code, printed below or available here, defines the project name and includes two useful CMake configuration files:

  • mlpack.cmake: finds mlpack's dependencies and download them if necessary.
  • ConfigureCrossCompile.cmake: set up CMake configuration for cross-compilation.
  • crosscompile-toolchain.cmake: invoke CMake crosscompilation infrastructure.
  • crosscompile-arch-config.cmake: add necessary flags depending on the architecture (optional).

Then we need to call fetch_mlpack() will download mlpack including all dependencies, cross-compile OpenBLAS and set up all the necessary parameters to find these dependencies. Most of mlpack's dependencies are header-only with the exception of OpenBLAS; thus this is expected to be a quick step.

fetch_mlpack() will detect if cross compilation is necessary or not depending on the command that is executed when running cmake. Based on this, it will compile OpenBLAS.

cmake_minimum_required(VERSION 3.11)
project(main)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
include(CMake/mlpack.cmake)
include(CMake/ConfigureCrossCompile.cmake)

// Download all of mlpack's dependencies and cross-compile OpenBLAS.
fetch_mlpack()

Setting up include directories and source files

The last part of our CMakeFiles.txt consists of merging all of the components above together. First we need to start including the directories of our program. You will need to do the same for your software if you are trying to integrate this example into an existing codebase; this should be done by defining an include variable using set directive or add them directly in target_include_directories. Regarding the source code, a similar process by either adding then to add_executables or by appending the source files to SOURCES_FILES variable.

Finally do not forget to add any external library that you need to link against in target_link_libraries.

## Add your source files to SOURCES_FILES list
set(SOURCE_FILES main.cpp)
add_executable(RandomForest main.cpp ${SOURCES_FILES})

# If needed, add any additional include directories here.
target_include_directories(RandomForest PRIVATE
  ${MLPACK_INCLUDE_DIRS}
  # Add more include directories here...
)

# If your application needs to link against more than just mlpack's
# dependencies, be sure to list them here.
target_link_libraries(RandomForest PRIVATE -static
  ${MLPACK_LIBRARIES}
  # List additional dependencies to link against here.
)

Optimization and cross-compilation

If you are interested in adding specific compiler flags to optimize operations on your hardware, you can either set CMAKE_CXX_FLAGS manually, or look at the copy of CMake/crosscompile-arch-flags.cmake in your project, find the appropriate ARCH_NAME section, and add the new compilation flags to that file.

Once you have added all the source files and the headers for your applications, you can create your own build directory and build the software using cmake, and then make. Your cmake command should be similar to the following:

cmake \
    -DARCH_NAME=(Check below) \
    -DCMAKE_CROSSCOMPILING=ON \
    -DCMAKE_TOOLCHAIN_FILE=../CMake/crosscompile-toolchain.cmake \
    -DTOOLCHAIN_PREFIX=(Check below) \
    -DCMAKE_SYSROOT=(Check below) \

In order to fill the TOOLCHAIN_PREFIX and CMAKE_SYSROOT variables, use this table.

If your preferred architecture is missing, or if the table needs an update, please submit a PR to the repository and help us keep it up to date!