radius.cpp 2.26 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
#include <Python.h>
#include <torch/script.h>
3
#include <iostream>
rusty1s's avatar
rusty1s committed
4
5
6
7

#ifdef WITH_CUDA
#include "cuda/radius_cuda.h"
#endif
Alexander Liao's avatar
Alexander Liao committed
8
#include "cpu/radius_cpu.h"
rusty1s's avatar
rusty1s committed
9
10
11
12
13

#ifdef _WIN32
PyMODINIT_FUNC PyInit__radius(void) { return NULL; }
#endif

14
torch::Tensor radius(torch::Tensor x, torch::Tensor y, torch::optional<torch::Tensor> ptr_x,
15
                     torch::optional<torch::Tensor> ptr_y, double r, int64_t max_num_neighbors, int64_t n_threads) {
rusty1s's avatar
rusty1s committed
16
17
  if (x.device().is_cuda()) {
#ifdef WITH_CUDA
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    if (!(ptr_x.has_value()) && !(ptr_y.has_value())) {
      auto batch_x = torch::tensor({0,torch::size(x,0)}).to(torch::kLong).to(torch::kCUDA);
      auto batch_y = torch::tensor({0,torch::size(y,0)}).to(torch::kLong).to(torch::kCUDA);
      return radius_cuda(x, y, batch_x, batch_y, r, max_num_neighbors);
    }
    else if (!(ptr_x.has_value())) {
      auto batch_x = torch::tensor({0,torch::size(x,0)}).to(torch::kLong).to(torch::kCUDA);
      auto batch_y = ptr_y.value();
      return radius_cuda(x, y, batch_x, batch_y, r, max_num_neighbors);
    }
    else if (!(ptr_y.has_value())) {
      auto batch_x = ptr_x.value();
      auto batch_y = torch::tensor({0,torch::size(y,0)}).to(torch::kLong).to(torch::kCUDA);
      return radius_cuda(x, y, batch_x, batch_y, r, max_num_neighbors);
    }
    auto batch_x = ptr_x.value();
    auto batch_y = ptr_y.value();
    return radius_cuda(x, y, batch_x, batch_y, r, max_num_neighbors);
rusty1s's avatar
rusty1s committed
36
37
38
39
#else
    AT_ERROR("Not compiled with CUDA support");
#endif
  } else {
40
    if (!(ptr_x.has_value()) && !(ptr_y.has_value())) {
41
      return radius_cpu(x,y,r,max_num_neighbors, n_threads);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    }
    if (!(ptr_x.has_value())) {
      auto batch_x = torch::zeros({torch::size(x,0)}).to(torch::kLong);
      auto batch_y = ptr_y.value();
      return batch_radius_cpu(x, y, batch_x, batch_y, r, max_num_neighbors);
    }
    else if (!(ptr_y.has_value())) {
      auto batch_x = ptr_x.value();
      auto batch_y = torch::zeros({torch::size(y,0)}).to(torch::kLong);
      return batch_radius_cpu(x, y, batch_x, batch_y, r, max_num_neighbors);
    }
    auto batch_x = ptr_x.value();
    auto batch_y = ptr_y.value();
    return batch_radius_cpu(x, y, batch_x, batch_y, r, max_num_neighbors);
rusty1s's avatar
rusty1s committed
56
57
58
59
60
  }
}

static auto registry =
    torch::RegisterOperators().op("torch_cluster::radius", &radius);