disjoint_union.cc 4.27 KB
Newer Older
1
/**
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 *   Copyright (c) 2022, NVIDIA CORPORATION.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
16
17
 * @file array/cpu/disjoint_union.cc
 * @brief Disjoint union CPU implementation.
18
 */
19
20
21

#include <dgl/array.h>
#include <dgl/runtime/parallel_for.h>
22

23
24
25
26
27
28
29
#include <tuple>

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

30
template <DGLDeviceType XPU, typename IdType>
31
32
33
34
35
36
37
38
std::tuple<IdArray, IdArray, IdArray> _ComputePrefixSums(
    const std::vector<COOMatrix>& coos) {
  IdArray prefix_src_arr =
      NewIdArray(coos.size(), coos[0].row->ctx, coos[0].row->dtype.bits);
  IdArray prefix_dst_arr =
      NewIdArray(coos.size(), coos[0].row->ctx, coos[0].row->dtype.bits);
  IdArray prefix_elm_arr =
      NewIdArray(coos.size(), coos[0].row->ctx, coos[0].row->dtype.bits);
39
40
41
42
43

  auto prefix_src = prefix_src_arr.Ptr<IdType>();
  auto prefix_dst = prefix_dst_arr.Ptr<IdType>();
  auto prefix_elm = prefix_elm_arr.Ptr<IdType>();

44
  dgl::runtime::parallel_for(0, coos.size(), [&](IdType b, IdType e) {
45
46
47
48
49
50
51
    for (IdType i = b; i < e; ++i) {
      prefix_src[i] = coos[i].num_rows;
      prefix_dst[i] = coos[i].num_cols;
      prefix_elm[i] = coos[i].row->shape[0];
    }
  });

52
53
54
  return std::make_tuple(
      CumSum(prefix_src_arr, true), CumSum(prefix_dst_arr, true),
      CumSum(prefix_elm_arr, true));
55
56
}

57
template <DGLDeviceType XPU, typename IdType>
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
COOMatrix DisjointUnionCoo(const std::vector<COOMatrix>& coos) {
  bool has_data = false;
  bool row_sorted = true;
  bool col_sorted = true;
  // check if data index array
  for (size_t i = 0; i < coos.size(); ++i) {
    CHECK_SAME_DTYPE(coos[0].row, coos[i].row);
    CHECK_SAME_CONTEXT(coos[0].row, coos[i].row);
    has_data |= COOHasData(coos[i]);
  }

  auto prefixes = _ComputePrefixSums<XPU, IdType>(coos);
  auto prefix_src = static_cast<IdArray>(std::get<0>(prefixes)).Ptr<IdType>();
  auto prefix_dst = static_cast<IdArray>(std::get<1>(prefixes)).Ptr<IdType>();
  auto prefix_elm = static_cast<IdArray>(std::get<2>(prefixes)).Ptr<IdType>();

  IdArray result_src = NewIdArray(
75
      prefix_elm[coos.size()], coos[0].row->ctx, coos[0].row->dtype.bits);
76
  IdArray result_dst = NewIdArray(
77
      prefix_elm[coos.size()], coos[0].col->ctx, coos[0].col->dtype.bits);
78
79
80
  IdArray result_dat = NullArray();
  if (has_data) {
    result_dat = NewIdArray(
81
        prefix_elm[coos.size()], coos[0].row->ctx, coos[0].row->dtype.bits);
82
83
84
85
86
87
  }

  auto res_src_data = result_src.Ptr<IdType>();
  auto res_dst_data = result_dst.Ptr<IdType>();
  auto res_dat_data = result_dat.Ptr<IdType>();

88
  dgl::runtime::parallel_for(0, coos.size(), [&](IdType b, IdType e) {
89
    for (IdType i = b; i < e; ++i) {
90
      const aten::COOMatrix& coo = coos[i];
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      if (!coo.row_sorted) row_sorted = false;
      if (!coo.col_sorted) col_sorted = false;

      auto edges_src = coo.row.Ptr<IdType>();
      auto edges_dst = coo.col.Ptr<IdType>();
      auto edges_dat = coo.data.Ptr<IdType>();

      for (IdType j = 0; j < coo.row->shape[0]; j++) {
        res_src_data[prefix_elm[i] + j] = edges_src[j] + prefix_src[i];
      }

      for (IdType j = 0; j < coo.row->shape[0]; j++) {
        res_dst_data[prefix_elm[i] + j] = edges_dst[j] + prefix_dst[i];
      }

      if (has_data) {
        for (IdType j = 0; j < coo.row->shape[0]; j++) {
          const auto d = (!COOHasData(coo)) ? j : edges_dat[j];
109
          res_dat_data[prefix_elm[i] + j] = d + prefix_elm[i];
110
111
112
113
114
        }
      }
    }
  });
  return COOMatrix(
115
116
      prefix_src[coos.size()], prefix_dst[coos.size()], result_src, result_dst,
      result_dat, row_sorted, col_sorted);
117
118
}

119
120
121
122
template COOMatrix DisjointUnionCoo<kDGLCPU, int32_t>(
    const std::vector<COOMatrix>& coos);
template COOMatrix DisjointUnionCoo<kDGLCPU, int64_t>(
    const std::vector<COOMatrix>& coos);
123
124
125
126

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