torch.cpp 1011 Bytes
Newer Older
1
/*!
2
 *  Copyright (c) 2020-2022 by Contributors
3
4
5
6
 * \file torch/torch.cpp
 * \brief Implementation of PyTorch adapter library.
 */

7
#include <tensoradapter_exports.h>
8
#include <c10/core/CPUAllocator.h>
9
10
#ifdef DGL_USE_CUDA
#include <c10/cuda/CUDACachingAllocator.h>
11
#include <c10/cuda/CUDAStream.h>
12
#include <cuda_runtime.h>
13
#endif  // DGL_USE_CUDA
14

15
16
17
18
namespace tensoradapter {

extern "C" {

19
20
21
22
23
24
TA_EXPORTS void* CPURawAlloc(size_t nbytes) {
  return c10::GetCPUAllocator()->raw_allocate(nbytes);
}

TA_EXPORTS void CPURawDelete(void* ptr) {
  c10::GetCPUAllocator()->raw_deallocate(ptr);
25
26
}

27
#ifdef DGL_USE_CUDA
28
29
30
TA_EXPORTS void* CUDARawAlloc(size_t nbytes, cudaStream_t stream) {
  return c10::cuda::CUDACachingAllocator::raw_alloc_with_stream(
    nbytes, stream);
31
32
}

33
TA_EXPORTS void CUDARawDelete(void* ptr) {
34
35
  c10::cuda::CUDACachingAllocator::raw_delete(ptr);
}
36
37
38
39

TA_EXPORTS cudaStream_t CUDACurrentStream() {
  return at::cuda::getCurrentCUDAStream();
}
40
41
#endif  // DGL_USE_CUDA

42
43
44
};

};  // namespace tensoradapter