graclus_cpu.cpp 2.24 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
#include "graclus_cpu.h"

#include "utils.h"

rusty1s's avatar
rusty1s committed
5
6
torch::Tensor graclus_cpu(torch::Tensor rowptr, torch::Tensor col,
                          torch::optional<torch::Tensor> optional_weight) {
rusty1s's avatar
rusty1s committed
7

rusty1s's avatar
rusty1s committed
8
  CHECK_CPU(rowptr);
rusty1s's avatar
rusty1s committed
9
  CHECK_CPU(col);
rusty1s's avatar
rusty1s committed
10
  CHECK_INPUT(rowptr.dim() == 1 && col.dim() == 1);
rusty1s's avatar
rusty1s committed
11
12
  if (optional_weight.has_value()) {
    CHECK_CPU(optional_weight.value());
rusty1s's avatar
rusty1s committed
13
    CHECK_INPUT(optional_weight.value().dim() == 1);
rusty1s's avatar
rusty1s committed
14
15
16
    CHECK_INPUT(optional_weight.value().numel() == col.numel());
  }

rusty1s's avatar
rusty1s committed
17
18
19
  int64_t num_nodes = rowptr.numel() - 1;
  auto out = torch::full(num_nodes, -1, rowptr.options());
  auto node_perm = torch::randperm(num_nodes, rowptr.options());
rusty1s's avatar
rusty1s committed
20
21
22

  auto rowptr_data = rowptr.data_ptr<int64_t>();
  auto col_data = col.data_ptr<int64_t>();
rusty1s's avatar
rusty1s committed
23
  auto node_perm_data = node_perm.data_ptr<int64_t>();
rusty1s's avatar
rusty1s committed
24
25
26
  auto out_data = out.data_ptr<int64_t>();

  if (!optional_weight.has_value()) {
rusty1s's avatar
rusty1s committed
27
28
    for (int64_t n = 0; n < num_nodes; n++) {
      auto u = node_perm_data[n];
rusty1s's avatar
rusty1s committed
29
30
31
32
33
34

      if (out_data[u] >= 0)
        continue;

      out_data[u] = u;

rusty1s's avatar
rusty1s committed
35
36
37
38
39
40
      int64_t row_start = rowptr_data[u], row_end = rowptr_data[u + 1];
      auto edge_perm = torch::randperm(row_end - row_start, rowptr.options());
      auto edge_perm_data = edge_perm.data_ptr<int64_t>();

      for (auto e = 0; e < row_end - row_start; e++) {
        auto v = col_data[row_start + edge_perm_data[e]];
rusty1s's avatar
rusty1s committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54

        if (out_data[v] >= 0)
          continue;

        out_data[u] = std::min(u, v);
        out_data[v] = std::min(u, v);
        break;
      }
    }
  } else {
    auto weight = optional_weight.value();
    AT_DISPATCH_ALL_TYPES(weight.scalar_type(), "weighted_graclus", [&] {
      auto weight_data = weight.data_ptr<scalar_t>();

rusty1s's avatar
rusty1s committed
55
56
      for (auto n = 0; n < num_nodes; n++) {
        auto u = node_perm_data[n];
rusty1s's avatar
rusty1s committed
57
58
59
60
61
62
63

        if (out_data[u] >= 0)
          continue;

        auto v_max = u;
        scalar_t w_max = (scalar_t)0.;

rusty1s's avatar
rusty1s committed
64
65
        for (auto e = rowptr_data[u]; e < rowptr_data[u + 1]; e++) {
          auto v = col_data[e];
rusty1s's avatar
rusty1s committed
66
67
68
69

          if (out_data[v] >= 0)
            continue;

rusty1s's avatar
rusty1s committed
70
          if (weight_data[e] >= w_max) {
rusty1s's avatar
rusty1s committed
71
            v_max = v;
rusty1s's avatar
rusty1s committed
72
            w_max = weight_data[e];
rusty1s's avatar
rusty1s committed
73
74
75
76
77
78
79
80
81
82
83
          }
        }

        out_data[u] = std::min(u, v_max);
        out_data[v_max] = std::min(u, v_max);
      }
    });
  }

  return out;
}