mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: Closes https://github.com/pytorch/pytorch/pull/9107 Some details about how this was done: - For now, the allocators for CPU and CUDA are different (unifying the allocators is a bigger change to make, I'll contribute this in a later patch). To smooth this over, the allocator field now stores a void* instead of THAllocator* or THCDeviceAllocator*; to make this clear the field is renamed to allocatorVoidPtr. - Some THStorage functions which were generated per-scalar are now generalized, and thus moved out of the generic/ library. This way they can be called directly from a non-code-generated at::Storage - THCState is moved into a C++ header. This is actually not really related to this particular diff, but I'll need it soon to replace THAllocator/THCDeviceAllocator with at::Allocator (C++, so I can't mention it in a C header file.) - THPPointer needs to be adjusted, since there is no more type refinement between THStorage/THCStorage for it to template match over. This is a little tricky, because I can't refer to THCStorage_free unless we actually compile with CUDA. So there's two copies of the function now: one for the CPU build, one for the CUDA build. If we ever split CUDA/non-CUDA Python builds, you will have to indirect this through some dynamic dispatch. I want to soon replace the THCDeviceAllocator pointers in THCState with at::Allocator, but I can't reference a C++ namespaced type from C code, so THCState needs to move. Signed-off-by: Edward Z. Yang <ezyang@fb.com> Closes https://github.com/pytorch/pytorch/pull/9087 Reviewed By: orionr Differential Revision: D8712072 Pulled By: ezyang fbshipit-source-id: c6e1ea236cd1df017b42a7fffb2dbff20d50a284
49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#define __STDC_FORMAT_MACROS
|
|
|
|
#include "torch/csrc/python_headers.h"
|
|
#ifdef _MSC_VER
|
|
#include <Windows.h>
|
|
#endif
|
|
#include <structmember.h>
|
|
|
|
#define THP_HOST_HALF
|
|
|
|
#include <stdbool.h>
|
|
#include <TH/TH.h>
|
|
// See Note [TH abstraction violation]
|
|
// - Used to get at the allocator associated with a storage
|
|
#include <TH/THStorage.hpp>
|
|
#include <libshm.h>
|
|
#include "THP.h"
|
|
#include "allocators.h"
|
|
#include "copy_utils.h"
|
|
#include "DynamicTypes.h"
|
|
|
|
#ifdef USE_CUDA
|
|
#include <THC/THCStorage.hpp>
|
|
#endif
|
|
|
|
#include "generic/Storage.cpp"
|
|
#include <TH/THGenerateAllTypes.h>
|
|
|
|
#include "generic/Storage.cpp"
|
|
#include <TH/THGenerateHalfType.h>
|
|
|
|
// NB: If you ever divest libtorch of USE_CUDA, you'll have to virtualize
|
|
// the CUDA call.
|
|
template<>
|
|
void THPPointer<THStorage>::free() {
|
|
if (ptr) {
|
|
if (ptr->backend == at::kCPU) {
|
|
THStorage_free(ptr);
|
|
} else {
|
|
AT_ASSERT(ptr->backend == at::kCUDA);
|
|
#ifdef USE_CUDA
|
|
THCStorage_free(at::globalContext().lazyInitCUDA(), ptr);
|
|
#else
|
|
AT_ERROR("Cannot free THCStorage when not built with CUDA");
|
|
#endif
|
|
}
|
|
}
|
|
}
|