#include <torch/torch.h>
#include <iostream>
int main() {
std::cout << "CUDA available: " << torch::cuda::is_available() << std::endl;
return 0;
}
上述代码的输出结果为:CUDA available: 1
#include <torch/torch.h>
#include <iostream>
int main()
{
// 检查CUDA是否可用
if (!torch::cuda::is_available())
{
std::cerr << "CUDA not available! Exiting..." << std::endl;
return -1;
}
torch::Device device(torch::kCUDA);
std::cout << "Using CUDA device: " << device << std::endl;
// 在GPU上创建张量
torch::Tensor a = torch::randn({2, 3}, device);
torch::Tensor b = torch::ones({2, 3}, device);
// GPU上的张量运算
torch::Tensor c = a + b * 2.5;
// 将结果复制回CPU并打印
std::cout << "Result tensor:\n"
<< c.cpu() << std::endl;
// std::cout << "LibTorch版本: "
// << TORCH_VERSION_MAJOR << "."
// << TORCH_VERSION_MINOR << "."
// << TORCH_VERSION_PATCH << std::endl;
return 0;
}
这段代码输出结果为:
Using CUDA device: cuda
terminate called after throwing an instance of 'c10::Error'
what(): CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
Exception raised from c10_cuda_check_implementation at /pytorch/c10/cuda/CUDAException.cpp:43 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0xd4 (0xffff9b5aa9e4 in /lib/libc10.so)
frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 0xe4 (0xffff9b55d384 in /lib/libc10.so)
frame #2: c10::cuda::c10_cuda_check_implementation(int, char const*, char const*, int, bool) + 0x2e8 (0xffff62572b58 in /lib/libc10_cuda.so)
frame #3: <unknown function> + 0x1c41f94 (0xffff65af1f94 in /lib/libtorch_cuda.so)
frame #4: void at::native::templates::cuda::normal_kernel<at::CUDAGeneratorImpl*>(at::TensorBase const&, double, double, at::CUDAGeneratorImpl*) + 0x8c (0xffff65af298c in /lib/libtorch_cuda.so)
frame #5: <unknown function> + 0x16a1428 (0xffff9c8d1428 in /lib/libtorch_cpu.so)
frame #6: at::native::normal_(at::Tensor&, double, double, std::optional<at::Generator>) + 0x38 (0xffff9c8d1798 in /lib/libtorch_cpu.so)
frame #7: <unknown function> + 0x3193408 (0xffff67043408 in /lib/libtorch_cuda.so)
frame #8: <unknown function> + 0x3196050 (0xffff67046050 in /lib/libtorch_cuda.so)
frame #9: at::_ops::normal_::call(at::Tensor&, double, double, std::optional<at::Generator>) + 0x1cc (0xffff9d1da18c in /lib/libtorch_cpu.so)
frame #10: at::native::randn(c10::ArrayRef<long>, std::optional<at::Generator>, std::optional<c10::ScalarType>, std::optional<c10::Layout>, std::optional<c10::Device>, std::optional<bool>) + 0x130 (0xffff9cb53130 in /lib/libtorch_cpu.so)
frame #11: at::native::randn(c10::ArrayRef<long>, std::optional<c10::ScalarType>, std::optional<c10::Layout>, std::optional<c10::Device>, std::optional<bool>) + 0x44 (0xffff9cb53284 in /lib/libtorch_cpu.so)
frame #12: <unknown function> + 0x259151c (0xffff9d7c151c in /lib/libtorch_cpu.so)
frame #13: at::_ops::randn::redispatch(c10::DispatchKeySet, c10::ArrayRef<c10::SymInt>, std::optional<c10::ScalarType>, std::optional<c10::Layout>, std::optional<c10::Device>, std::optional<bool>) + 0xec (0xffff9cfd1a5c in /lib/libtorch_cpu.so)
frame #14: <unknown function> + 0x23dc0ac (0xffff9d60c0ac in /lib/libtorch_cpu.so)
frame #15: at::_ops::randn::call(c10::ArrayRef<c10::SymInt>, std::optional<c10::ScalarType>, std::optional<c10::Layout>, std::optional<c10::Device>, std::optional<bool>) + 0x1cc (0xffff9d04a66c in /lib/libtorch_cpu.so)
frame #16: <unknown function> + 0x9eb8 (0xaaaab7cb9eb8 in ./test)
frame #17: <unknown function> + 0xa324 (0xaaaab7cba324 in ./test)
frame #18: <unknown function> + 0x3cd4 (0xaaaab7cb3cd4 in ./test)
frame #19: <unknown function> + 0x273fc (0xffff9b1473fc in /lib/aarch64-linux-gnu/libc.so.6)
frame #20: __libc_start_main + 0x98 (0xffff9b1474cc in /lib/aarch64-linux-gnu/libc.so.6)
frame #21: <unknown function> + 0x3af0 (0xaaaab7cb3af0 in ./test)
Aborted (core dumped)
这是什么原因