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

#include "utils.h"

rusty1s's avatar
rusty1s committed
5
6
7
8
at::Tensor random_walk_cpu(torch::Tensor row, torch::Tensor col,
                           torch::Tensor start, int64_t walk_length, double p,
                           double q, int64_t num_nodes) {

rusty1s's avatar
rusty1s committed
9
10
11
12
13
14
15
16
  auto deg = degree(row, num_nodes);
  auto cum_deg = at::cat({at::zeros(1, deg.options()), deg.cumsum(0)}, 0);

  auto rand = at::rand({start.size(0), (int64_t)walk_length},
                       start.options().dtype(at::kFloat));
  auto out =
      at::full({start.size(0), (int64_t)walk_length + 1}, -1, start.options());

rusty1s's avatar
rusty1s committed
17
18
19
20
21
22
  auto deg_d = deg.DATA_PTR<int64_t>();
  auto cum_deg_d = cum_deg.DATA_PTR<int64_t>();
  auto col_d = col.DATA_PTR<int64_t>();
  auto start_d = start.DATA_PTR<int64_t>();
  auto rand_d = rand.DATA_PTR<float>();
  auto out_d = out.DATA_PTR<int64_t>();
rusty1s's avatar
rusty1s committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  for (ptrdiff_t n = 0; n < start.size(0); n++) {
    int64_t cur = start_d[n];
    auto i = n * (walk_length + 1);
    out_d[i] = cur;

    for (ptrdiff_t l = 1; l <= (int64_t)walk_length; l++) {
      cur = col_d[cum_deg_d[cur] +
                  int64_t(rand_d[n * walk_length + (l - 1)] * deg_d[cur])];
      out_d[i + l] = cur;
    }
  }

  return out;
}