array_cumsum.cc 1.27 KB
Newer Older
1
/**
2
 *  Copyright (c) 2020 by Contributors
3
4
 * @file array/cpu/array_cumsum.cc
 * @brief Array cumsum CPU implementation
5
6
7
8
9
10
11
12
 */
#include <dgl/array.h>

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

13
template <DGLDeviceType XPU, typename IdType>
14
15
16
IdArray CumSum(IdArray array, bool prepend_zero) {
  const int64_t len = array.NumElements();
  if (len == 0)
17
18
    return !prepend_zero ? array
                         : aten::Full(0, 1, array->dtype.bits, array->ctx);
19
20
21
22
23
  if (prepend_zero) {
    IdArray ret = aten::NewIdArray(len + 1, array->ctx, array->dtype.bits);
    const IdType* in_d = array.Ptr<IdType>();
    IdType* out_d = ret.Ptr<IdType>();
    out_d[0] = 0;
24
    for (int64_t i = 0; i < len; ++i) out_d[i + 1] = out_d[i] + in_d[i];
25
26
27
28
29
30
    return ret;
  } else {
    IdArray ret = aten::NewIdArray(len, array->ctx, array->dtype.bits);
    const IdType* in_d = array.Ptr<IdType>();
    IdType* out_d = ret.Ptr<IdType>();
    out_d[0] = in_d[0];
31
    for (int64_t i = 1; i < len; ++i) out_d[i] = out_d[i - 1] + in_d[i];
sangwzh's avatar
sangwzh committed
32
    std::cout << "limm cpu ret : " << ret << std::endl;
33
34
35
36
    return ret;
  }
}

37
38
template IdArray CumSum<kDGLCPU, int32_t>(IdArray, bool);
template IdArray CumSum<kDGLCPU, int64_t>(IdArray, bool);
39
40
41
42

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