diff options
Diffstat (limited to 'Tests/CudaOnly/Fatbin')
-rw-r--r-- | Tests/CudaOnly/Fatbin/CMakeLists.txt | 25 | ||||
-rw-r--r-- | Tests/CudaOnly/Fatbin/main.cu | 58 |
2 files changed, 83 insertions, 0 deletions
diff --git a/Tests/CudaOnly/Fatbin/CMakeLists.txt b/Tests/CudaOnly/Fatbin/CMakeLists.txt new file mode 100644 index 0000000..db0dc22 --- /dev/null +++ b/Tests/CudaOnly/Fatbin/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.18) +project(CudaFATBIN LANGUAGES CUDA) + + +set(CMAKE_CUDA_ARCHITECTURES all-major) + +add_library(CudaFATBIN OBJECT +${CMAKE_CURRENT_SOURCE_DIR}/../CUBIN/kernelA.cu +${CMAKE_CURRENT_SOURCE_DIR}/../CUBIN/kernelB.cu +${CMAKE_CURRENT_SOURCE_DIR}/../CUBIN/kernelC.cu) + +set_property(TARGET CudaFATBIN PROPERTY CUDA_FATBIN_COMPILATION ON) + +# Will use `cuModuleLoadFatBinary` to load the fatbinaries +add_executable(CudaOnlyFatbin main.cu) +target_compile_features(CudaOnlyFatbin PRIVATE cuda_std_11) +target_compile_definitions(CudaOnlyFatbin PRIVATE "FATBIN_FILE_PATHS=\"$<JOIN:$<TARGET_OBJECTS:CudaFATBIN>,~_~>\"") + +find_package(CUDAToolkit REQUIRED) +target_link_libraries(CudaOnlyFatbin PRIVATE CUDA::cuda_driver) + +if(APPLE) + # Help the static cuda runtime find the driver (libcuda.dyllib) at runtime. + set_property(TARGET CudaOnlyFatbin PROPERTY BUILD_RPATH ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}) +endif() diff --git a/Tests/CudaOnly/Fatbin/main.cu b/Tests/CudaOnly/Fatbin/main.cu new file mode 100644 index 0000000..89af0e3 --- /dev/null +++ b/Tests/CudaOnly/Fatbin/main.cu @@ -0,0 +1,58 @@ +#include <iostream> +#include <string> +#include <vector> + +#include <cuda.h> + +#define GENERATED_HEADER(x) GENERATED_HEADER1(x) +#define GENERATED_HEADER1(x) <x> + +static std::string input_paths = { FATBIN_FILE_PATHS }; + +int main() +{ + const std::string delimiter = "~_~"; + input_paths += delimiter; + + size_t end = 0; + size_t previous_end = 0; + std::vector<std::string> actual_paths; + while ((end = input_paths.find(delimiter, previous_end)) != + std::string::npos) { + actual_paths.emplace_back( + input_paths.substr(previous_end, end - previous_end)); + previous_end = end + 3; + } + + cuInit(0); + int count = 0; + cuDeviceGetCount(&count); + if (count == 0) { + std::cerr << "No CUDA devices found\n"; + return 1; + } + + CUdevice device; + cuDeviceGet(&device, 0); + + CUcontext context; + cuCtxCreate(&context, 0, device); + + CUmodule module; + for (auto p : actual_paths) { + if (p.find(".fatbin") == std::string::npos) { + std::cout << p << " Doesn't have the .fatbin suffix" << p << std::endl; + return 1; + } + std::cout << "trying to load fatbin: " << p << std::endl; + CUresult result = cuModuleLoad(&module, p.c_str()); + std::cout << "module pointer: " << module << '\n'; + if (result != CUDA_SUCCESS || module == nullptr) { + std::cerr << "Failed to load the embedded fatbin with error: " + << static_cast<unsigned int>(result) << '\n'; + return 1; + } + } + + return 0; +} |