knn.cpp 820 Bytes
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
#include <Python.h>
#include <torch/script.h>

rusty1s's avatar
rusty1s committed
4
5
#include "cpu/knn_cpu.h"

rusty1s's avatar
rusty1s committed
6
7
8
9
10
11
12
13
#ifdef WITH_CUDA
#include "cuda/knn_cuda.h"
#endif

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

rusty1s's avatar
rusty1s committed
14
15
16
17
torch::Tensor knn(torch::Tensor x, torch::Tensor y,
                  torch::optional<torch::Tensor> ptr_x,
                  torch::optional<torch::Tensor> ptr_y, int64_t k, bool cosine,
                  int64_t num_workers) {
rusty1s's avatar
rusty1s committed
18
19
  if (x.device().is_cuda()) {
#ifdef WITH_CUDA
rusty1s's avatar
rusty1s committed
20
    return knn_cuda(x, y, ptr_x, ptr_y, k, cosine);
rusty1s's avatar
rusty1s committed
21
22
23
24
#else
    AT_ERROR("Not compiled with CUDA support");
#endif
  } else {
rusty1s's avatar
rusty1s committed
25
    if (cosine)
26
      AT_ERROR("`cosine` argument not supported on CPU");
rusty1s's avatar
rusty1s committed
27
    return knn_cpu(x, y, ptr_x, ptr_y, k, num_workers);
rusty1s's avatar
rusty1s committed
28
29
30
31
32
  }
}

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