Unify cudaGetDeviceCount implementations. (#18445)
authorEdward Yang <ezyang@fb.com>
Tue, 26 Mar 2019 16:42:41 +0000 (09:42 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 26 Mar 2019 16:50:14 +0000 (09:50 -0700)
commit515238e0a54f1a00a67c984dd65d906acdc57034
tree68c9dc1d39d4526246628c8a96644946ef1f9ebf
parentcf094d4edcc7928f9a4a368b3e2eeb22579b29b0
Unify cudaGetDeviceCount implementations. (#18445)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18445
ghimport-source-id: 30d018737bf6989bc68b7e3676f44e0ca6141fde

Stack from [ghstack](https://github.com/ezyang/ghstack):
* #18242 Test running a CUDA build on CPU machine.
* **#18445 Unify cudaGetDeviceCount implementations.**

I went about doing this by searching for calls to cudaGetDeviceCount,
and then methodically replacing them with references to c10::cuda::device_count()
or at::cuda::device_count().

There is a point to doing this: the various implementations wildly differed
in their handling of what to do when cudaGetDeviceCount returns an error.
The final standardized behavior is that **all errors are swallowed** and
we return device count of zero.  This indirectly fixes running CUDA builds
on CPU, which was broken in #17847.

I added 'noexcept' to the 'deviceCount' virtual method on DeviceGuardImpl.
This is a BC-breaking change for anyone inheriting from DeviceGuardImpl
but all you need to do is put 'noexcept' on your method and it is backwards
compatible with older libtorch.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Differential Revision: D14612189

fbshipit-source-id: 3c8d186e3dd623c0e27625212c7ce30f75d943cb
14 files changed:
aten/src/ATen/cuda/CUDAContext.h
aten/src/ATen/cuda/detail/CUDAHooks.cpp
aten/src/ATen/detail/CPUGuardImpl.h
aten/src/ATen/hip/impl/HIPGuardImplMasqueradingAsCUDA.h
c10/core/impl/DeviceGuardImplInterface.h
c10/core/impl/FakeGuardImpl.h
c10/core/impl/VirtualGuardImpl.h
c10/cuda/CUDACachingAllocator.cpp
c10/cuda/CUDAFunctions.h
c10/cuda/impl/CUDAGuardImpl.h
test/cpp_extensions/msnpu_extension.cpp
torch/csrc/api/src/cuda.cpp
torch/csrc/autograd/profiler_cuda.cpp
torch/csrc/cuda/Module.cpp