Skip to content

Commit

Permalink
Reduce binding code boilerplate (#44)
Browse files Browse the repository at this point in the history
* Substitute `run*` functions with a single template

* Template `mainRun` functions

* Remove repeted includes
  • Loading branch information
sbaldu authored Jul 18, 2024
1 parent a2356b9 commit e9a398d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 1,063 deletions.
141 changes: 16 additions & 125 deletions CLUEstering/alpaka/BindingModules/binding_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
#include <alpaka/alpaka.hpp>
#include <vector>

#include "../CLUE/CLUEAlgoAlpaka.h"
#include "../CLUE/Run.h"
#include "../DataFormats/Points.h"
#include "../DataFormats/alpaka/PointsAlpaka.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <stdint.h>

namespace alpaka_serial_sync {
void listDevices(const std::string& backend) {
Expand All @@ -27,158 +23,53 @@ namespace alpaka_serial_sync {
}
}

template <typename Kernel>
std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
int pPBin,
const std::vector<std::vector<float>>& coords,
const std::vector<float>& weights,
const FlatKernel& kernel,
const Kernel& kernel,
int Ndim,
size_t block_size,
size_t device_id) {
auto const dev_acc = alpaka::getDevByIdx<Acc1D>(device_id);
const auto dev_acc = alpaka::getDevByIdx<Acc1D>(device_id);

// Create the queue
Queue queue_(dev_acc);

// Running the clustering algorithm //
switch (Ndim) {
[[unlikely]] case (1):
return run1(
return run<1, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (2):
return run2(
return run<2, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (3):
return run3(
return run<3, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (4):
return run4(
return run<4, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (5):
return run5(
return run<5, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (6):
return run6(
return run<6, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (7):
return run7(
return run<7, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (8):
return run8(
return run<8, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (9):
return run9(
return run<9, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (10):
return run10(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] default:
std::cout << "This library only works up to 10 dimensions\n";
return {};
}
}

std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
int pPBin,
const std::vector<std::vector<float>>& coords,
const std::vector<float>& weights,
const ExponentialKernel& kernel,
int Ndim,
size_t block_size,
size_t device_id) {
auto const dev_acc = alpaka::getDevByIdx<Acc1D>(device_id);

// Create the queue
Queue queue_(dev_acc);

// Running the clustering algorithm //
switch (Ndim) {
[[unlikely]] case (1):
return run1(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (2):
return run2(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (3):
return run3(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (4):
return run4(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (5):
return run5(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (6):
return run6(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (7):
return run7(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (8):
return run8(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (9):
return run9(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (10):
return run10(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] default:
std::cout << "This library only works up to 10 dimensions\n";
return {};
}
}

std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
int pPBin,
const std::vector<std::vector<float>>& coords,
const std::vector<float>& weights,
const GaussianKernel& kernel,
int Ndim,
size_t block_size,
size_t device_id) {
auto const dev_acc = alpaka::getDevByIdx<Acc1D>(device_id);

// Create the queue
Queue queue_(dev_acc);

// Running the clustering algorithm //
switch (Ndim) {
[[unlikely]] case (1):
return run1(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (2):
return run2(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[likely]] case (3):
return run3(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (4):
return run4(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (5):
return run5(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (6):
return run6(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (7):
return run7(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (8):
return run8(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (9):
return run9(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] case (10):
return run10(
return run<10, Kernel>(
dc, rhoc, outlier, pPBin, coords, weights, kernel, queue_, block_size);
[[unlikely]] default:
std::cout << "This library only works up to 10 dimensions\n";
Expand All @@ -202,7 +93,7 @@ namespace alpaka_serial_sync {
const FlatKernel&,
int,
size_t,
size_t>(&mainRun),
size_t>(&mainRun<FlatKernel>),
"mainRun");
m.def("mainRun",
pybind11::overload_cast<float,
Expand All @@ -214,7 +105,7 @@ namespace alpaka_serial_sync {
const ExponentialKernel&,
int,
size_t,
size_t>(&mainRun),
size_t>(&mainRun<ExponentialKernel>),
"mainRun");
m.def("mainRun",
pybind11::overload_cast<float,
Expand All @@ -226,7 +117,7 @@ namespace alpaka_serial_sync {
const GaussianKernel&,
int,
size_t,
size_t>(&mainRun),
size_t>(&mainRun<GaussianKernel>),
"mainRun");
}
}; // namespace alpaka_serial_sync
Loading

0 comments on commit e9a398d

Please sign in to comment.