array_index_select.cc 2.35 KB
Newer Older
1
/**
2
 *  Copyright (c) 2019 by Contributors
3
4
 * @file array/cpu/array_index_select.cc
 * @brief Array index select CPU implementation
5
6
 */
#include <dgl/array.h>
7
#include <dgl/runtime/parallel_for.h>
8
9
10
11
12
13

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

14
template <DGLDeviceType XPU, typename DType, typename IdType>
15
NDArray IndexSelect(NDArray array, IdArray index) {
16
17
18
  CHECK_EQ(array->shape[0], array.NumElements())
      << "Only support tensor"
      << " whose first dimension equals number of elements, e.g. (5,), (5, 1)";
19

20
21
22
23
24
25
  const DType* array_data = static_cast<DType*>(array->data);
  const IdType* idx_data = static_cast<IdType*>(index->data);
  const int64_t arr_len = array->shape[0];
  const int64_t len = index->shape[0];
  NDArray ret = NDArray::Empty({len}, array->dtype, array->ctx);
  DType* ret_data = static_cast<DType*>(ret->data);
26
27
28
29
30
31
32
33
34
35
  runtime::parallel_for(
      0,
      len,
      1000,  // Thread scheduling overhead is bigger with tiny grain size.
      [idx_data, arr_len, ret_data, array_data] (size_t begin, size_t end) {
        for (size_t i = begin; i < end; ++i) {
          CHECK_LT(idx_data[i], arr_len) << "Index out of range.";
          ret_data[i] = array_data[idx_data[i]];
        }
      });
36
37
38
  return ret;
}

39
40
41
42
43
44
45
46
template NDArray IndexSelect<kDGLCPU, int32_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, int32_t, int64_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, int64_t, int32_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, int64_t, int64_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, float, int32_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, float, int64_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, double, int32_t>(NDArray, IdArray);
template NDArray IndexSelect<kDGLCPU, double, int64_t>(NDArray, IdArray);
47

48
template <DGLDeviceType XPU, typename DType>
49
DType IndexSelect(NDArray array, int64_t index) {
50
51
52
53
  const DType* data = static_cast<DType*>(array->data);
  return data[index];
}

54
55
56
57
template int32_t IndexSelect<kDGLCPU, int32_t>(NDArray array, int64_t index);
template int64_t IndexSelect<kDGLCPU, int64_t>(NDArray array, int64_t index);
template float IndexSelect<kDGLCPU, float>(NDArray array, int64_t index);
template double IndexSelect<kDGLCPU, double>(NDArray array, int64_t index);
58

59
60
61
}  // namespace impl
}  // namespace aten
}  // namespace dgl