CUDADevice.h 786 B

12345678910111213141516171819202122232425262728
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <ATen/cuda/Exceptions.h>
  4. #include <cuda.h>
  5. #include <cuda_runtime.h>
  6. namespace at::cuda {
  7. inline Device getDeviceFromPtr(void* ptr) {
  8. cudaPointerAttributes attr{};
  9. AT_CUDA_CHECK(cudaPointerGetAttributes(&attr, ptr));
  10. #if !defined(USE_ROCM)
  11. TORCH_CHECK(attr.type != cudaMemoryTypeUnregistered,
  12. "The specified pointer resides on host memory and is not registered with any CUDA device.");
  13. #endif
  14. return {c10::DeviceType::CUDA, static_cast<DeviceIndex>(attr.device)};
  15. }
  16. } // namespace at::cuda
  17. #else
  18. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  19. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)