2
#include <cuda_runtime.h>
#include <torch/torch.h>
#include <iostream>

int main() {
    std::cout << "CUDA device count: " << torch::cuda::device_count() << std::endl;
    std::cout << "CUDA is available: " << (torch::cuda::is_available() ? "Yes" : "No") << std::endl;
    std::cout << "cuDNN is available: " << (torch::cuda::cudnn_is_available() ? "Yes" : "No") <<        std::endl;
    int deviceCount;
    cudaGetDeviceCount(&deviceCount);
    std::cout << "Number of CUDA devices: " << deviceCount << std::endl;
    return 0;
}

Output:

CUDA device count: 0
CUDA is available: No
cuDNN is available: No
Number of CUDA devices: 1

runtime seems to work, torch won't show CUDA I'm using MSVC 2022, windows 11. I installed latest pytorch with CUDA support
libtorch-win-shared-with-deps-2.4.0+cu124.zip (I also tried release), my nvcc --version shows:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Fri_Jun_14_16:44:19_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.6, V12.6.20
Build cuda_12.6.r12.6/compiler.34431801_0

I tried different versions of pytorch with no success. I can access CUDA from python code using pytorch with no problems, but i need to access it in C++

4
  • If Windows, you can also detect cuda by DXGI enum and checking Vendor ID 4318 Commented Aug 3 at 15:26
  • i just need to use libtorch with cuda, but it won't untill it detects cuda device. so detecting cuda outside of torch isn't an issue
    – yegor
    Commented Aug 3 at 16:08
  • Is there a direct ml version to download? Commented Aug 3 at 17:21
  • i downloaded libtorch-win-shared-with-deps-2.4.0+cu124.zip and release version of libtorch with cuda support from the official pytorch site, but it won't find any cuda device.
    – yegor
    Commented Aug 3 at 19:34

1 Answer 1

1

manually loading torch_cuda.dll does fix the problem:

    HMODULE torchCudaDll = LoadLibraryA("torch_cuda.dll");
    if (torchCudaDll == NULL) {
        std::cerr << "Failed to load torch_cuda.dll. Error code: " << GetLastError() << std::endl;

}
    else {
        std::cout << "Successfully loaded torch_cuda.dll" << std::endl;
        //  free the library when you're done:
        // FreeLibrary(torchCudaDll);
    }

apparently that was an old bug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.