array_index_select_uvm.cu 4.88 KB
Newer Older
1
/**
2
 *  Copyright (c) 2019-2022 by Contributors
3
4
 * @file array/cuda/uvm/array_index_select_uvm.cu
 * @brief Array index select GPU implementation
5
6
 */
#include <dgl/array.h>
7

8
#include "../../../runtime/cuda/cuda_common.h"
9
#include "../array_index_select.cuh"
10
#include "../utils.h"
11
#include "./array_index_select_uvm.cuh"
12
13
14
15
16
17

namespace dgl {
using runtime::NDArray;
namespace aten {
namespace impl {

18
template <typename DType, typename IdType>
19
NDArray IndexSelectCPUFromGPU(NDArray array, IdArray index) {
20
  cudaStream_t stream = runtime::getCurrentCUDAStream();
21
22
23
24
25
  const int64_t arr_len = array->shape[0];
  const int64_t len = index->shape[0];
  int64_t num_feat = 1;
  std::vector<int64_t> shape{len};

26
  CHECK(array.IsPinned());
27
  const DType* array_data = static_cast<DType*>(cuda::GetDevicePointer(array));
28
  CHECK_EQ(index->ctx.device_type, kDGLCUDA);
29
30
31
32
33
34
35

  for (int d = 1; d < array->ndim; ++d) {
    num_feat *= array->shape[d];
    shape.emplace_back(array->shape[d]);
  }

  NDArray ret = NDArray::Empty(shape, array->dtype, index->ctx);
36
  if (len == 0 || arr_len * num_feat == 0) return ret;
37
38
  DType* ret_data = static_cast<DType*>(ret->data);

39
40
41
42
  auto res = Sort(index, cuda::_NumberOfBits(arr_len));
  const IdType* idx_data = static_cast<IdType*>(res.first->data);
  const int64_t* perm_data = static_cast<int64_t*>(res.second->data);

43
  if (num_feat == 1) {
44
45
46
47
    const int nt = cuda::FindNumThreads(len);
    const int nb = (len + nt - 1) / nt;
    CUDA_KERNEL_CALL(
        IndexSelectSingleKernel, nb, nt, 0, stream, array_data, idx_data, len,
48
        arr_len, ret_data, perm_data);
49
  } else {
50
51
52
53
54
55
56
57
58
    dim3 block(256, 1);
    while (static_cast<int64_t>(block.x) >= 2 * num_feat) {
      block.x /= 2;
      block.y *= 2;
    }
    const dim3 grid((len + block.y - 1) / block.y);
    if (num_feat * sizeof(DType) < 2 * CACHE_LINE_SIZE) {
      CUDA_KERNEL_CALL(
          IndexSelectMultiKernel, grid, block, 0, stream, array_data, num_feat,
59
          idx_data, len, arr_len, ret_data, perm_data);
60
61
62
    } else {
      CUDA_KERNEL_CALL(
          IndexSelectMultiKernelAligned, grid, block, 0, stream, array_data,
63
          num_feat, idx_data, len, arr_len, ret_data, perm_data);
64
    }
65
66
67
68
  }
  return ret;
}

69
// floating point types are treated as their equal width integer types
70
71
72
73
74
75
76
77
template NDArray IndexSelectCPUFromGPU<int8_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int8_t, int64_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int16_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int16_t, int64_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int32_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int32_t, int64_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int64_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelectCPUFromGPU<int64_t, int64_t>(NDArray, IdArray);
78

79
template <typename DType, typename IdType>
80
void IndexScatterGPUToCPU(NDArray dest, IdArray index, NDArray source) {
81
  cudaStream_t stream = runtime::getCurrentCUDAStream();
82
83
84
85
86
87
88
89
  const DType* source_data = static_cast<DType*>(source->data);
  const IdType* idx_data = static_cast<IdType*>(index->data);
  const int64_t arr_len = dest->shape[0];
  const int64_t len = index->shape[0];
  int64_t num_feat = 1;
  std::vector<int64_t> shape{len};

  CHECK(dest.IsPinned());
90
  DType* dest_data = static_cast<DType*>(cuda::GetDevicePointer(dest));
91
92
  CHECK_EQ(index->ctx.device_type, kDGLCUDA);
  CHECK_EQ(source->ctx.device_type, kDGLCUDA);
93
94
95
96
97

  for (int d = 1; d < source->ndim; ++d) {
    num_feat *= source->shape[d];
  }

98
  if (len == 0) return;
99
100

  if (num_feat == 1) {
101
102
103
104
105
    const int nt = cuda::FindNumThreads(len);
    const int nb = (len + nt - 1) / nt;
    CUDA_KERNEL_CALL(
        IndexScatterSingleKernel, nb, nt, 0, stream, source_data, idx_data, len,
        arr_len, dest_data);
106
  } else {
107
108
109
110
111
112
113
114
115
    dim3 block(256, 1);
    while (static_cast<int64_t>(block.x) >= 2 * num_feat) {
      block.x /= 2;
      block.y *= 2;
    }
    const dim3 grid((len + block.y - 1) / block.y);
    CUDA_KERNEL_CALL(
        IndexScatterMultiKernel, grid, block, 0, stream, source_data, num_feat,
        idx_data, len, arr_len, dest_data);
116
117
118
119
120
121
122
123
124
125
126
127
  }
}

// floating point types are treated as their equal width integer types
template void IndexScatterGPUToCPU<int8_t, int32_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int8_t, int64_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int16_t, int32_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int16_t, int64_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int32_t, int32_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int32_t, int64_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int64_t, int32_t>(NDArray, IdArray, NDArray);
template void IndexScatterGPUToCPU<int64_t, int64_t>(NDArray, IdArray, NDArray);
128
129
130
131

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