From c434cef59af574af949875c8a436c1e15839edea Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Tue, 13 Feb 2024 19:38:39 -0600 Subject: [PATCH] Defer cuFile feature checks until finding kvikio package (#342) This PR closes #341. The kvikio `INTERFACE_COMPILE_DEFINITIONS` were being set based on the packages available during the libkvikio conda build (e.g. CUDA 12.2 since #328), which might not be the same packages/versions as when libkvikio is actually being used (e.g. to build libcudf with CUDA 12.0). Because we built libkvikio with CUDA 12.2 and then tried to use it with CUDA 12.0 devcontainers, the build failed to find the cuFile Stream APIs that were introduced in CUDA 12.2. This PR defers these definitions until the call to `find_package`, which will then use the exact cuFile features present (if cuFile is available at all) when building a package like cudf that depends on kvikio. The libkvikio example/test binary is built with the cuFile features available at build time, for use in the `libkvikio-tests` conda package. However, this test binary will still be compatible with a runtime where cuFile is unavailable or is version 12.0, as it is dlopen-ing the library and has runtime checks for the batch/stream features it tries to use. I did local testing of this PR with cudf devcontainers. I tested both 12.0 and 12.2 to reproduce (and fix) the failure, and also tested clean builds of libcudf after removing `libcufile` (to test when cuFile is not found). All seems to work as intended. Authors: - Bradley Dice (https://github.com/bdice) Approvers: - Robert Maynard (https://github.com/robertmaynard) - Vyas Ramasubramani (https://github.com/vyasr) URL: https://github.com/rapidsai/kvikio/pull/342 --- cpp/CMakeLists.txt | 61 +++++++++++++++++++++++++++++-------- cpp/examples/CMakeLists.txt | 14 ++++++++- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 42a05508c6..94b06b8021 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -1,5 +1,5 @@ # ============================================================================= -# Copyright (c) 2021-2023, NVIDIA CORPORATION. +# Copyright (c) 2021-2024, 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 @@ -70,14 +70,14 @@ else() else() set(cuFile_BATCH_API_FOUND TRUE) endif() - message(STATUS "Found cuFile's Batch API: ${cuFile_BATCH_API_FOUND}") + message(STATUS "Found cuFile Batch API: ${cuFile_BATCH_API_FOUND}") string(FIND "${CUFILE_H_STR}" "cuFileReadAsync" cuFileReadAsync_location) if(cuFileReadAsync_location EQUAL "-1") set(cuFile_STREAM_API_FOUND FALSE) else() set(cuFile_STREAM_API_FOUND TRUE) endif() - message(STATUS "Found cuFile's Stream API: ${cuFile_STREAM_API_FOUND}") + message(STATUS "Found cuFile Stream API: ${cuFile_STREAM_API_FOUND}") endif() # library targets @@ -91,16 +91,6 @@ target_include_directories( target_link_libraries(kvikio INTERFACE Threads::Threads) target_link_libraries(kvikio INTERFACE CUDA::toolkit) -if(cuFile_FOUND) - target_link_libraries(kvikio INTERFACE cufile::cuFile_interface) - target_compile_definitions(kvikio INTERFACE KVIKIO_CUFILE_FOUND) - if(cuFile_BATCH_API_FOUND) - target_compile_definitions(kvikio INTERFACE KVIKIO_CUFILE_BATCH_API_FOUND) - endif() - if(cuFile_STREAM_API_FOUND) - target_compile_definitions(kvikio INTERFACE KVIKIO_CUFILE_STREAM_API_FOUND) - endif() -endif() target_link_libraries(kvikio INTERFACE ${CMAKE_DL_LIBS}) target_compile_features(kvikio INTERFACE cxx_std_17) @@ -137,12 +127,56 @@ Provide targets for KvikIO: C++ bindings for cuFile. ]=] ) +set(final_code_string + [=[ +get_property(already_set_kvikio DIRECTORY PROPERTY kvikio_already_set_defines SET) +if(NOT already_set_kvikio) + set_property(DIRECTORY PROPERTY kvikio_already_set_defines "ON") + + # Find cuFile and determine which features are supported + find_package(cuFile QUIET) + if(NOT cuFile_FOUND) + message(WARNING "KvikIO: cuFile not found") + else() + file(READ "${cuFile_INCLUDE_DIRS}/cufile.h" CUFILE_H_STR) + string(FIND "${CUFILE_H_STR}" "cuFileBatchIOSetUp" cuFileBatchIOSetUp_location) + if(cuFileBatchIOSetUp_location EQUAL "-1") + set(cuFile_BATCH_API_FOUND FALSE) + else() + set(cuFile_BATCH_API_FOUND TRUE) + endif() + message(STATUS "KvikIO: Found cuFile Batch API: ${cuFile_BATCH_API_FOUND}") + string(FIND "${CUFILE_H_STR}" "cuFileReadAsync" cuFileReadAsync_location) + if(cuFileReadAsync_location EQUAL "-1") + set(cuFile_STREAM_API_FOUND FALSE) + else() + set(cuFile_STREAM_API_FOUND TRUE) + endif() + message(STATUS "KvikIO: Found cuFile Stream API: ${cuFile_STREAM_API_FOUND}") + endif() + + # Enable supported cuFile features in KvikIO + if(cuFile_FOUND) + target_link_libraries(kvikio::kvikio INTERFACE cufile::cuFile_interface) + target_compile_definitions(kvikio::kvikio INTERFACE KVIKIO_CUFILE_FOUND) + if(cuFile_BATCH_API_FOUND) + target_compile_definitions(kvikio::kvikio INTERFACE KVIKIO_CUFILE_BATCH_API_FOUND) + endif() + if(cuFile_STREAM_API_FOUND) + target_compile_definitions(kvikio::kvikio INTERFACE KVIKIO_CUFILE_STREAM_API_FOUND) + endif() + endif() +endif() +]=] +) + rapids_export( INSTALL kvikio EXPORT_SET kvikio-exports GLOBAL_TARGETS kvikio NAMESPACE kvikio:: DOCUMENTATION doc_string + FINAL_CODE_BLOCK final_code_string ) # build export targets @@ -152,4 +186,5 @@ rapids_export( GLOBAL_TARGETS kvikio NAMESPACE kvikio:: DOCUMENTATION doc_string + FINAL_CODE_BLOCK final_code_string ) diff --git a/cpp/examples/CMakeLists.txt b/cpp/examples/CMakeLists.txt index bb653ed8fb..42b646022c 100644 --- a/cpp/examples/CMakeLists.txt +++ b/cpp/examples/CMakeLists.txt @@ -1,5 +1,5 @@ # ============================================================================= -# Copyright (c) 2021-2023, NVIDIA CORPORATION. +# Copyright (c) 2021-2024, 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 @@ -20,6 +20,18 @@ set_target_properties(BASIC_IO_TEST PROPERTIES INSTALL_RPATH "\$ORIGIN/../../lib target_include_directories(BASIC_IO_TEST PRIVATE ../include ${cuFile_INCLUDE_DIRS}) target_link_libraries(BASIC_IO_TEST PRIVATE kvikio CUDA::cudart) +# Enable supported cuFile features in KvikIO examples +if(cuFile_FOUND) + target_link_libraries(BASIC_IO_TEST PRIVATE cufile::cuFile_interface) + target_compile_definitions(BASIC_IO_TEST PRIVATE KVIKIO_CUFILE_FOUND) + if(cuFile_BATCH_API_FOUND) + target_compile_definitions(BASIC_IO_TEST PRIVATE KVIKIO_CUFILE_BATCH_API_FOUND) + endif() + if(cuFile_STREAM_API_FOUND) + target_compile_definitions(BASIC_IO_TEST PRIVATE KVIKIO_CUFILE_STREAM_API_FOUND) + endif() +endif() + if(CMAKE_COMPILER_IS_GNUCXX) set(KVIKIO_CXX_FLAGS "-Wall;-Werror;-Wno-unknown-pragmas") target_compile_options(BASIC_IO_TEST PRIVATE "$<$:${KVIKIO_CXX_FLAGS}>")