How to set the default device to torch::kCUDA?

Hello,

How can I set the default device to CUDA with libtorch C++?

Thanks in advance!

You can do it like so:

torch::Device device = torch::kCPU;
if (torch::cuda::is_available()) {
  device = torch::kCUDA;
}

@beazt thanks for your answer, but What I want to get is like:

auto tensor = torch::randn({2,2})

Here, the default device of tensor is kCPU tensor.device(). But I want for all tensors that I created switch the default device to kCUDA without doing this my_tensor = my_tensor.to(torch::kCUDA) for each tensor.

Is there a way to do this simultaneously for all created tensors ?

As far as I know, there is no way to do it automatically at initialization. You have to do it manually for each tensor, I’m afraid.

@beazt Thanks a lot!

I thinked about fast solution. I will use the manual method. Thanks

For anyone running across this in 2024, see set_default_device(): torch.set_default_device — PyTorch 2.6 documentation