array_nonzero.cc 689 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*!
 *  Copyright (c) 2020 by Contributors
 * \file array/cpu/array_nonzero.cc
 * \brief Array nonzero CPU implementation
 */
#include <dgl/array.h>

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

13
template <DGLDeviceType XPU, typename IdType>
14
15
16
17
18
19
20
21
22
IdArray NonZero(IdArray array) {
  std::vector<int64_t> ret;
  const IdType* data = array.Ptr<IdType>();
  for (int64_t i = 0; i < array->shape[0]; ++i)
    if (data[i] != 0)
      ret.push_back(i);
  return NDArray::FromVector(ret, array->ctx);
}

23
24
template IdArray NonZero<kDGLCPU, int32_t>(IdArray);
template IdArray NonZero<kDGLCPU, int64_t>(IdArray);
25
26
27
28

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