Is it possible, using LibTorch, to dynamically (programatically) select device, e.g., use GPU if machine has it or fallback to CPU?
I think that it is similar to python API
torch::DeviceType device_type;
if (torch::cuda::is_available()) {
device_type = torch::kCUDA;
} else {
device_type = torch::kCPU;
}
torch::Device device(device_type);
Net model; // derived torch::nn::Module
model.to(device);
Oh I did not care about “single binary” (I thought it was “single source”). I think it should be tough because you need to link cuda libraries. I’m not sure that the initialization of cuda devices is such lazy.