rapidsai / cuml

cuML - RAPIDS Machine Learning Library
https://docs.rapids.ai/api/cuml/stable/
Apache License 2.0
4.26k stars 535 forks source link

[BUG] Update CMakeLists_standalone.txt for cuML cpp examples #3540

Open teju85 opened 3 years ago

teju85 commented 3 years ago

We currently have 2 examples dbscan and kmeans showing how to setup a C++ project and use libcuml++ inside them. They also come with CMakeLists_standalone.txt which are supposed to be used by such folks to start right-off-the-bat. However, they don't seem to be updated, especially after all the library-related changes that have went into the cmake files of cuML project. We should keep these standalone files updated as per the changes made in the main project.

We discovered this issue while @akifcorduk was trying to setup a standalone C++ project to debug his TSP-related work. @akifcorduk please share the cmake file showing the changes you had to make while trying to set this one up.

akifcorduk commented 3 years ago

The minimum standalone possible Cmake configuration that works for me:

#
# Copyright (c) 2019, NVIDIA CORPORATION.
#
# Licensed 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.
#
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(kmeans_example LANGUAGES CXX CUDA)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-extended-lambda --expt-relaxed-constexpr")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Werror=cross-execution-space-call -Wno-deprecated-declarations -Xptxas --disable-warnings")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -Wall,-Wno-error=sign-compare,-Wno-error=unused-but-set-variable")

include(FetchContent)

# - THRUST/CUB
message("Fetching Thrust")

FetchContent_Declare(
    thrust
    GIT_REPOSITORY https://github.com/thrust/thrust.git
    # August 28, 2020
    GIT_TAG        52a8bda46c5c2128414d1d47f546b486ff0be2f0
)

FetchContent_GetProperties(thrust)
if(NOT thrust_POPULATED)
  FetchContent_Populate(thrust)
  # We are not using the thrust CMake targets, so no need to call `add_subdirectory()`.
endif()
set(THRUST_INCLUDE_DIR "${thrust_SOURCE_DIR}")

if(IS_DIRECTORY ${CUML_INCLUDE_DIR})
    # CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES is needed so that cuda_runtime.h is found
    # CUML_INCLUDE_DIR is needed so that kmeans/kmeans_c.h is found
    include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES} ${CUML_INCLUDE_DIR} ${THRUST_INCLUDE_DIR})
else()
    message(FATAL_ERROR "CUML_INCLUDE_DIR not specified.")
endif(IS_DIRECTORY ${CUML_INCLUDE_DIR})
if(IS_DIRECTORY ${CUML_LIBRARY_DIR})
    # CUML_LIBRARY_DIR point to the director where libcuml++.so lives
    link_directories(${CUML_LIBRARY_DIR})
else()
    message(FATAL_ERROR "CUML_LIBRARY_DIR not specified.")
endif(IS_DIRECTORY ${CUML_LIBRARY_DIR})

find_package(OpenMP)

set(FAISS_GPU_ARCHS "${FAISS_GPU_ARCHS} -gencode arch=compute_70,code=compute_70")

include(ExternalProject)
set(FAISS_DIR ${CMAKE_CURRENT_BINARY_DIR}/faiss CACHE STRING
    "Path to FAISS source directory")
  ExternalProject_Add(faiss
    GIT_REPOSITORY    https://github.com/facebookresearch/faiss.git
    GIT_TAG           a5b850dec6f1cd6c88ab467bfd5e87b0cac2e41d
    CONFIGURE_COMMAND LIBS=-pthread
                      CPPFLAGS=-w
                      LDFLAGS=-L${CMAKE_INSTALL_PREFIX}/lib
                              ${CMAKE_CURRENT_BINARY_DIR}/faiss/src/faiss/configure
                          --prefix=${CMAKE_CURRENT_BINARY_DIR}/faiss
                          --with-blas=${BLAS_LIBRARIES}
                          --with-cuda=${CUDA_TOOLKIT_ROOT_DIR}
                          --with-cuda-arch=${FAISS_GPU_ARCHS}
                          -v
    PREFIX            ${FAISS_DIR}
    BUILD_COMMAND     make -j${PARALLEL_LEVEL} VERBOSE=1
    BUILD_BYPRODUCTS  ${FAISS_DIR}/lib/libfaiss.a
    BUILD_ALWAYS      1
    INSTALL_COMMAND   make -s install > /dev/null
    UPDATE_COMMAND    ""
    BUILD_IN_SOURCE   1
    PATCH_COMMAND     patch -p1 -N < ${CMAKE_CURRENT_SOURCE_DIR}/cmake/faiss_cuda11.patch || true)

  ExternalProject_Get_Property(faiss install_dir)
  add_library(FAISS::FAISS STATIC IMPORTED)
  add_dependencies(FAISS::FAISS faiss)
  set_property(TARGET FAISS::FAISS PROPERTY
    IMPORTED_LOCATION ${FAISS_DIR}/lib/libfaiss.a)
  set(FAISS_INCLUDE_DIRS "${FAISS_DIR}/src")

add_executable(tsp_test 
  src/tsp.cu
  src/tsp_test.cu)

set_property(TARGET tsp_test PROPERTY CUDA_ARCHITECTURES 70)
# Need to set linker language to CUDA to link the CUDA Runtime
set_target_properties(tsp_test PROPERTIES LINKER_LANGUAGE "CUDA")
# Link cuml
target_link_libraries(tsp_test cublas cusolver cusparse cudart cuda cuml++ FAISS::FAISS blas)

if(OpenMP_CXX_FOUND)
  target_link_libraries(tsp_test 
      ${OpenMP_CXX_LIB_NAMES})
endif(OpenMP_CXX_FOUND)

Blas package found by cmake's find_package was incompatible with Faiss library. So it is required to give the blas library path of the conda environment as a cmake argument. I also had to copy raft include directory(which was under include/cuml/) to one level higher(include/) to be able to find raft header files in my project.

The following is the cmake command to build the above cmake script: cmake .. -DCUML_LIBRARY_DIR="$CONDA_PREFIX/lib" -DBLAS_LIBRARIES=$CONDA_PREFIX/lib/libopenblas.so.0 -DCUML_INCLUDE_DIR="$CONDA_PREFIX/include"

github-actions[bot] commented 3 years ago

This issue has been labeled inactive-30d due to no recent activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with a comment indicating any updates or changes to the original issue and/or confirm this issue still needs to be addressed. This issue will be labeled inactive-90d if there is no activity in the next 60 days.

github-actions[bot] commented 3 years ago

This issue has been labeled inactive-90d due to no recent activity in the past 90 days. Please close this issue if no further response or action is needed. Otherwise, please respond with a comment indicating any updates or changes to the original issue and/or confirm this issue still needs to be addressed.

ITFUHENG commented 2 years ago

How to fix it up?