tensoradapter.h 2.92 KB
Newer Older
sangwzh's avatar
sangwzh committed
1
// !!! This is a file automatically generated by hipify!!!
2
/**
3
 *  Copyright (c) 2020-2022 by Contributors
4
5
 * @file tensoradapter.h
 * @brief Header file for functions exposed by the adapter library.
6
 *
7
8
 * Functions in this library must be exported with extern "C" so that DGL can
 * locate them with dlsym(3) (or GetProcAddress on Windows).
9
10
11
12
13
 */

#ifndef TENSORADAPTER_H_
#define TENSORADAPTER_H_

14
#ifdef DGL_USE_CUDA
sangwzh's avatar
sangwzh committed
15
#include <hip/hip_runtime.h>
16
#endif  // DGL_USE_CUDA
17
18
19
20
21

namespace tensoradapter {

extern "C" {

22
/**
23
 * @brief Allocate a piece of CPU memory via
24
 * PyTorch's CPUAllocator
25
 *
26
27
 * @param nbytes The size to be allocated.
 * @return Pointer to the allocated memory.
28
29
30
 */
void* CPURawAlloc(size_t nbytes);

31
/**
32
 * @brief Free the CPU memory.
33
 *
34
 * @param ptr Pointer to the memory to be freed.
35
 */
36
void CPURawDelete(void* ptr);
37

38
#ifdef DGL_USE_CUDA
39
/**
40
 * @brief Allocate a piece of GPU memory via
41
42
 * PyTorch's THCCachingAllocator.
 *
43
44
45
 * @param nbytes The size to be allocated.
 * @param stream The stream to be allocated on.
 * @return Pointer to the allocated memory.
46
 */
sangwzh's avatar
sangwzh committed
47
void* CUDARawAlloc(size_t nbytes, hipStream_t stream);
48

49
/**
50
 * @brief Free the GPU memory.
51
 *
52
 * @param ptr Pointer to the memory to be freed.
53
 */
54
void CUDARawDelete(void* ptr);
55

56
/**
57
 * @brief Get the current CUDA stream.
58
 */
sangwzh's avatar
sangwzh committed
59
hipStream_t CUDACurrentStream();
60

61
/**
62
 * @brief Let the caching allocator know which streams are using this tensor.
63
 *
64
65
66
 * @param ptr Pointer of the tensor to be recorded.
 * @param stream The stream that is using this tensor.
 * @param device_id Device of the tensor.
67
 */
sangwzh's avatar
sangwzh committed
68
void RecordStream(void* ptr, hipStream_t stream, int device_id);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

/**
 * @brief Allocate a piece of pinned CPU memory via
 *     PyTorch's CachingHostAllocator.
 *
 * @param nbytes The size to be allocated.
 * @param ctx Pointer to the PyTorch storage ctx ptr returned from the
 *     allocator.
 * @param deleter Pointer to the delete function ptr returned from the
 *     allocator.
 * @return Raw pointer to the allocated memory.
 */
void* CUDARawHostAlloc(size_t nbytes, void** ctx, void** raw_deleter);

/**
 * @brief 'Free' the pinned CPU memory via
 *     inserting the memory block back to the free list.
 *
 * @param deleter Pointer to the delete function ptr returned from the
 *     allocator.
 */
void CUDARawHostDelete(void** raw_deleter);

/**
 * @brief 'Record' a CUDA stream (usually from a copy kernel) for the pinned
 *     memory via PyTorch's CachingHostAllocator.
 *
 * @param data Pointer of the tensor to be recorded.
 * @param ctx PyTorch storage ctx ptr returned from the allocator.
 * @param stream The stream that currently consumes this tensor.
 * @param device_id Device of the tensor.
 */
void CUDARecordHostAlloc(
sangwzh's avatar
sangwzh committed
102
    void* data, void* ctx, hipStream_t stream, int device_id);
103
104
105
106
107
108

/**
 * @brief Release cached pinned memory allocations via cudaHostFree.
 */
void CUDAHostAllocatorEmptyCache();

109
#endif  // DGL_USE_CUDA
110
111
112
113
114
}

};  // namespace tensoradapter

#endif  // TENSORADAPTER_H_