"src/git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "29d2afbfe2e09a4ee7cc51455e51ce8b8c0e252d"
array_scatter.cc 2.38 KB
Newer Older
1
2
3
4
5
6
/*!
 *  Copyright (c) 2019 by Contributors
 * \file array/cpu/array_scatter.cc
 * \brief Array scatter CPU implementation
 */
#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 Scatter(NDArray array, IdArray indices) {
16
17
  NDArray result =
      NDArray::Empty({indices->shape[0]}, array->dtype, array->ctx);
18
19
20
21
22
23
24
25
26
27
28

  const DType *array_data = static_cast<DType *>(array->data);
  const IdType *indices_data = static_cast<IdType *>(indices->data);
  DType *result_data = static_cast<DType *>(result->data);

  for (int64_t i = 0; i < indices->shape[0]; ++i)
    result_data[indices_data[i]] = array_data[i];

  return result;
}

29
30
31
32
33
34
35
36
template NDArray Scatter<kDGLCPU, int32_t, int32_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, int64_t, int32_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, float, int32_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, double, int32_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, int32_t, int64_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, int64_t, int64_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, float, int64_t>(NDArray, IdArray);
template NDArray Scatter<kDGLCPU, double, int64_t>(NDArray, IdArray);
37

38
template <DGLDeviceType XPU, typename DType, typename IdType>
39
40
void Scatter_(IdArray index, NDArray value, NDArray out) {
  const int64_t len = index->shape[0];
41
42
43
  const IdType *idx = index.Ptr<IdType>();
  const DType *val = value.Ptr<DType>();
  DType *outd = out.Ptr<DType>();
44
45
46
47
48
  runtime::parallel_for(0, len, [&](size_t b, size_t e) {
    for (auto i = b; i < e; ++i) {
      outd[idx[i]] = val[i];
    }
  });
49
50
}

51
52
53
54
55
56
57
58
template void Scatter_<kDGLCPU, int32_t, int32_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, int64_t, int32_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, float, int32_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, double, int32_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, int32_t, int64_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, int64_t, int64_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, float, int64_t>(IdArray, NDArray, NDArray);
template void Scatter_<kDGLCPU, double, int64_t>(IdArray, NDArray, NDArray);
59

60
61
62
};  // namespace impl
};  // namespace aten
};  // namespace dgl