Skip to content

Commit

Permalink
Allow rapids_test to be used without CUDAToolkit (#480)
Browse files Browse the repository at this point in the history
Fixes #478

Authors:
  - Robert Maynard (https://github.com/robertmaynard)

Approvers:
  - Jacob Faibussowitsch (https://github.com/Jacobfaib)

URL: #480
  • Loading branch information
robertmaynard authored Nov 2, 2023
1 parent f5462bc commit ae1c8e8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
30 changes: 19 additions & 11 deletions rapids-cmake/test/detail/generate_resource_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@
* limitations under the License.
*/

#ifdef HAVE_CUDA
#include <cuda_runtime_api.h>
#endif

#include <iostream>
#include <string>
#include <vector>

struct version {
int major = 1;
int minor = 0;
version() : json_major(1), json_minor(0) {}
int json_major;
int json_minor;
};

struct gpu {
gpu(int i) : id{i} {};
gpu(int i, const cudaDeviceProp& prop) : id{i}, memory{prop.totalGlobalMem}, slots{100} {}
int id = 0;
size_t memory = 0;
int slots = 0;
gpu(int i) : id(i), memory(0), slots(0){};
gpu(int i, size_t mem) : id(i), memory(mem), slots(100) {}
int id;
size_t memory;
int slots;
};

// A hard-coded JSON printer that generates a ctest resource-specification file:
// https://cmake.org/cmake/help/latest/manual/ctest.1.html#resource-specification-file
void to_json(std::ostream& buffer, version const& v)
{
buffer << "\"version\": {\"major\": " << v.major << ", \"minor\": " << v.minor << "}";
buffer << "\"version\": {\"major\": " << v.json_major << ", \"minor\": " << v.json_minor << "}";
}
void to_json(std::ostream& buffer, gpu const& g)
{
Expand All @@ -48,18 +51,23 @@ int main()
{
std::vector<gpu> gpus;
int nDevices = 0;

#ifdef HAVE_CUDA
cudaGetDeviceCount(&nDevices);
if (nDevices == 0) {
gpus.emplace_back(0);
gpus.push_back(gpu(0));
} else {
for (int i = 0; i < nDevices; ++i) {
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, i);
gpus.emplace_back(i, prop);
gpus.push_back(gpu(i, prop.totalGlobalMem));
}
}
#else
gpus.push_back(gpu(0));
#endif

version v{1, 0};
version v;
std::cout << "{\n";
to_json(std::cout, v);
std::cout << ",\n";
Expand Down
14 changes: 8 additions & 6 deletions rapids-cmake/test/generate_resource_spec.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function(rapids_test_generate_resource_spec DESTINATION filepath)
{
"version": {"major": 1, "minor": 0},
"local": [{
"gpus": [{"id":"0", "slots": 100}]
"gpus": [{"id":"0", "slots": 0}]
}]
}
]=])
Expand All @@ -67,10 +67,12 @@ function(rapids_test_generate_resource_spec DESTINATION filepath)
set(error_file ${PROJECT_BINARY_DIR}/rapids-cmake/detect_gpus.stderr.log)

if(NOT EXISTS "${eval_exe}")
find_package(CUDAToolkit REQUIRED)
find_package(CUDAToolkit QUIET)
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/rapids-cmake/")

set(compile_options "-I${CUDAToolkit_INCLUDE_DIRS}")
if(CUDAToolkit_FOUND)
set(compile_options "-I${CUDAToolkit_INCLUDE_DIRS}" "-DHAVE_CUDA")
endif()
set(link_options ${CUDA_cudart_LIBRARY})
set(compiler "${CMAKE_CXX_COMPILER}")
if(NOT DEFINED CMAKE_CXX_COMPILER)
Expand All @@ -79,13 +81,13 @@ function(rapids_test_generate_resource_spec DESTINATION filepath)

execute_process(COMMAND "${compiler}" "${eval_file}" ${compile_options} ${link_options} -o
"${eval_exe}" OUTPUT_VARIABLE compile_output
ERROR_VARIABLE compile_output COMMAND_ECHO STDOUT)
ERROR_VARIABLE compile_output)
endif()

if(NOT EXISTS "${eval_exe}")
message(STATUS "rapids_test_generate_resource_spec failed to build detection executable, presuming 1 GPU."
message(STATUS "rapids_test_generate_resource_spec failed to build detection executable, presuming no GPUs."
)
message(STATUS "rapids_test_generate_resource_spec compile failure details are ${compile_output}"
message(STATUS "rapids_test_generate_resource_spec compile[${compiler} ${compile_options} ${link_options}] failure details are ${compile_output}"
)
file(WRITE "${filepath}" "${gpu_json_contents}")
else()
Expand Down
2 changes: 2 additions & 0 deletions testing/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ add_cmake_config_test(install_relocatable-wrong-component.cmake SHOULD_FAIL "${w
add_cmake_ctest_test(add-impossible-allocation SHOULD_FAIL "Insufficient resources for test")
add_cmake_config_test(add-with-install-component.cmake)
add_cmake_config_test(add-with-no-gpus.cmake)

add_cmake_ctest_test(add-no-cuda-toolkit)
if(RAPIDS_CMAKE_TESTING_GPU_COUNT GREATER 0)
add_cmake_ctest_test(add-allocation-simple)
add_cmake_ctest_test(add-allocation-throws-runtime-error)
Expand Down
29 changes: 29 additions & 0 deletions testing/test/add-no-cuda-toolkit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#=============================================================================
# Copyright (c) 2023, 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.20)
project(rapids-test-project LANGUAGES CXX)

include(${rapids-cmake-dir}/rapids-test.cmake)

#Disable searching for the CUDA Toolkit so we emulate
#support for CPU only rapids-cmake-testing support
set(CMAKE_DISABLE_FIND_PACKAGE_CUDAToolkit True)

rapids_test_init()

add_executable(verify_alloc main.cpp)
enable_testing()
rapids_test_add(NAME simple_test COMMAND verify_alloc)
20 changes: 20 additions & 0 deletions testing/test/add-no-cuda-toolkit/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023, 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.
*/

#include <iostream>
#include <vector>

int main() { return 0; }

0 comments on commit ae1c8e8

Please sign in to comment.