common.h 2.74 KB
Newer Older
1
2
3
4
5
6
7
8
/**
 *  Copyright (c) 2017-2023 by Contributors
 * @file cuda/common.h
 * @brief Common utilities for CUDA
 */
#ifndef GRAPHBOLT_CUDA_COMMON_H_
#define GRAPHBOLT_CUDA_COMMON_H_

9
#include <c10/cuda/CUDACachingAllocator.h>
10
11
#include <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
12
13
14
15
#include <torch/script.h>

#include <memory>
#include <unordered_map>
16
17
18
19

namespace graphbolt {
namespace cuda {

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * @brief This class is designed to allocate workspace storage
 * and to get a nonblocking thrust execution policy
 * that uses torch's CUDA memory pool and the current cuda stream:

 * cuda::CUDAWorkspaceAllocator allocator;
 * const auto stream = torch::cuda::getDefaultCUDAStream();
 * const auto exec_policy = thrust::cuda::par_nosync(allocator).on(stream);

 * Now, one can pass exec_policy to thrust functions

 * To get an integer array of size 1000 whose lifetime is managed by unique_ptr,
 * use:

 * auto int_array = allocator.AllocateStorage<int>(1000);

 * int_array.get() gives the raw pointer.
 */
38
struct CUDAWorkspaceAllocator {
39
40
41
  // Required by thrust to satisfy allocator requirements.
  using value_type = char;

42
  explicit CUDAWorkspaceAllocator() { at::globalContext().lazyInitCUDA(); }
43
44
45

  CUDAWorkspaceAllocator& operator=(const CUDAWorkspaceAllocator&) = default;

46
47
48
  void operator()(void* ptr) const {
    c10::cuda::CUDACachingAllocator::raw_delete(ptr);
  }
49
50
51

  // Required by thrust to satisfy allocator requirements.
  value_type* allocate(std::ptrdiff_t size) const {
52
53
    return reinterpret_cast<value_type*>(
        c10::cuda::CUDACachingAllocator::raw_alloc(size));
54
55
56
57
58
59
60
61
62
63
64
65
66
  }

  // Required by thrust to satisfy allocator requirements.
  void deallocate(value_type* ptr, std::size_t) const { operator()(ptr); }

  template <typename T>
  std::unique_ptr<T, CUDAWorkspaceAllocator> AllocateStorage(
      std::size_t size) const {
    return std::unique_ptr<T, CUDAWorkspaceAllocator>(
        reinterpret_cast<T*>(allocate(sizeof(T) * size)), *this);
  }
};

67
68
69
70
71
72
73
74
75
76
template <typename T>
inline bool is_zero(T size) {
  return size == 0;
}

template <>
inline bool is_zero<dim3>(dim3 size) {
  return size.x == 0 || size.y == 0 || size.z == 0;
}

77
78
#define CUDA_CALL(func) C10_CUDA_CHECK((func))

79
80
81
82
83
84
85
86
87
88
89
90
#define CUDA_KERNEL_CALL(kernel, nblks, nthrs, shmem, stream, ...)    \
  {                                                                   \
    if (!graphbolt::cuda::is_zero((nblks)) &&                         \
        !graphbolt::cuda::is_zero((nthrs))) {                         \
      (kernel)<<<(nblks), (nthrs), (shmem), (stream)>>>(__VA_ARGS__); \
      C10_CUDA_KERNEL_LAUNCH_CHECK();                                 \
    }                                                                 \
  }

}  // namespace cuda
}  // namespace graphbolt
#endif  // GRAPHBOLT_CUDA_COMMON_H_