"src/vscode:/vscode.git/clone" did not exist on "384c83aa9a1f268e5587d5ea1ea9f4c040845167"
csr_get_data.cu 3.85 KB
Newer Older
1
/**
2
 *  Copyright (c) 2021 by Contributors
3
4
 * @file array/cuda/csr_get_data.cu
 * @brief Retrieve entries of a CSR matrix
5
6
 */
#include <dgl/array.h>
7

8
#include <numeric>
9
10
11
#include <unordered_set>
#include <vector>

12
13
14
15
16
17
18
19
20
21
#include "../../runtime/cuda/cuda_common.h"
#include "./utils.h"

namespace dgl {

using runtime::NDArray;

namespace aten {
namespace impl {

22
template <DGLDeviceType XPU, typename IdType, typename DType>
23
NDArray CSRGetData(
24
25
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, DType filler) {
26
27
28
29
  const int64_t rowlen = rows->shape[0];
  const int64_t collen = cols->shape[0];

  CHECK((rowlen == collen) || (rowlen == 1) || (collen == 1))
30
      << "Invalid row and col id array.";
31
32
33
34
35
36

  const int64_t row_stride = (rowlen == 1 && collen != 1) ? 0 : 1;
  const int64_t col_stride = (collen == 1 && rowlen != 1) ? 0 : 1;

  const int64_t rstlen = std::max(rowlen, collen);
  IdArray rst = NDArray::Empty({rstlen}, weights->dtype, rows->ctx);
37
  if (rstlen == 0) return rst;
38

39
  cudaStream_t stream = runtime::getCurrentCUDAStream();
40
41
42
  const int nt = cuda::FindNumThreads(rstlen);
  const int nb = (rstlen + nt - 1) / nt;
  if (return_eids)
43
44
    BUG_IF_FAIL(DGLDataTypeTraits<DType>::dtype == rows->dtype)
        << "DType does not match row's dtype.";
45

46
47
48
49
  const IdType* indptr_data = csr.indptr.Ptr<IdType>();
  const IdType* indices_data = csr.indices.Ptr<IdType>();
  const IdType* data_data = CSRHasData(csr) ? csr.data.Ptr<IdType>() : nullptr;
  if (csr.is_pinned) {
50
51
52
53
    CUDA_CALL(
        cudaHostGetDevicePointer(&indptr_data, csr.indptr.Ptr<IdType>(), 0));
    CUDA_CALL(
        cudaHostGetDevicePointer(&indices_data, csr.indices.Ptr<IdType>(), 0));
54
    if (CSRHasData(csr)) {
55
56
      CUDA_CALL(
          cudaHostGetDevicePointer(&data_data, csr.data.Ptr<IdType>(), 0));
57
58
59
    }
  }

60
  // TODO(minjie): use binary search for sorted csr
61
62
63
64
65
  CUDA_KERNEL_CALL(
      cuda::_LinearSearchKernel, nb, nt, 0, stream, indptr_data, indices_data,
      data_data, rows.Ptr<IdType>(), cols.Ptr<IdType>(), row_stride, col_stride,
      rstlen, return_eids ? nullptr : weights.Ptr<DType>(), filler,
      rst.Ptr<DType>());
66
67
68
  return rst;
}

69
template NDArray CSRGetData<kDGLCUDA, int32_t, __half>(
70
71
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, __half filler);
72
template NDArray CSRGetData<kDGLCUDA, int64_t, __half>(
73
74
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, __half filler);
75
76
77
78
79
80
81
82
#if BF16_ENABLED
template NDArray CSRGetData<kDGLCUDA, int32_t, __nv_bfloat16>(
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, __nv_bfloat16 filler);
template NDArray CSRGetData<kDGLCUDA, int64_t, __nv_bfloat16>(
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, __nv_bfloat16 filler);
#endif  // BF16_ENABLED
83
template NDArray CSRGetData<kDGLCUDA, int32_t, float>(
84
85
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, float filler);
86
template NDArray CSRGetData<kDGLCUDA, int64_t, float>(
87
88
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, float filler);
89
template NDArray CSRGetData<kDGLCUDA, int32_t, double>(
90
91
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, double filler);
92
template NDArray CSRGetData<kDGLCUDA, int64_t, double>(
93
94
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, double filler);
95
96

// For CSRGetData<XPU, IdType>(CSRMatrix, NDArray, NDArray)
97
template NDArray CSRGetData<kDGLCUDA, int32_t, int32_t>(
98
99
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, int32_t filler);
100
template NDArray CSRGetData<kDGLCUDA, int64_t, int64_t>(
101
102
    CSRMatrix csr, NDArray rows, NDArray cols, bool return_eids,
    NDArray weights, int64_t filler);
103
104
105
106

}  // namespace impl
}  // namespace aten
}  // namespace dgl