Skip to content

Commit

Permalink
Implement method for listing devices from python
Browse files Browse the repository at this point in the history
  • Loading branch information
sbaldu committed Dec 20, 2023
1 parent 90c1d49 commit 2a378f6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
44 changes: 39 additions & 5 deletions CLUEstering/alpaka/BindingModules/CLUEstering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sklearn.preprocessing import StandardScaler
import CLUE_Convolutional_Kernels as clue_kernels
import CLUE_CPU_Serial as cpu_serial
# import CLUE_CPU_TBB as cpu_tbb
import CLUE_CPU_TBB as cpu_tbb
import CLUE_GPU_CUDA as gpu_cuda

def test_blobs(n_samples: int, n_dim: int , n_blobs: int = 4, mean: float = 0,
Expand Down Expand Up @@ -476,6 +476,36 @@ def choose_kernel(self,
raise ValueError("Invalid kernel. The allowed choices for the"
+ " kernels are: flat, exp, gaus and custom.")

def list_devices(self, backend: str = "all") -> None:
"""
Lists the devices available for the chosen backend.
Parameters
----------
backend : string, optional
The backend for which the devices are listed. The allowed values are
'all', 'cpu serial', 'cpu tbb' and 'gpu cuda'.
The default value is 'all'.
Raises
------
ValueError : If the backend is not valid.
"""

if backend == "all":
cpu_serial.listDevices('cpu serial')
cpu_tbb.listDevices('cpu tbb')
gpu_cuda.listDevices('gpu cuda')
elif backend == "cpu serial":
cpu_serial.listDevices(backend)
elif backend == "cpu tbb":
cpu_tbb.listDevices(backend)
elif backend == "gpu cuda":
gpu_cuda.listDevices(backend)
else:
raise ValueError("Invalid backend. The allowed choices for the"
+ " backend are: all, cpu serial, cpu tbb and gpu cuda.")

def run_clue(self,
backend: str = "cpu serial",
block_size: int = 1024,
Expand Down Expand Up @@ -832,8 +862,12 @@ def to_csv(self, output_folder: str, file_name: str) -> None:
if __name__ == "__main__":
c = clusterer(0.4,5,1.)
c.read_data('./sissa.csv')
# c.run_clue(backend="cpu serial", verbose=True)
c.run_clue(backend="cpu tbb", verbose=True)
c.run_clue(backend="gpu cuda", verbose=True)
c.run_clue(backend="cpu serial", verbose=True)
# c.run_clue(backend="cpu tbb", verbose=True)
# c.run_clue(backend="gpu cuda", verbose=True)
c.cluster_plotter()
c.to_csv('./','sissa_output_tbb.csv')
# c.to_csv('./','sissa_output_tbb.csv')
c.list_devices('cpu serial')
c.list_devices('cpu tbb')
c.list_devices('gpu cuda')
c.list_devices()
15 changes: 15 additions & 0 deletions CLUEstering/alpaka/BindingModules/binding_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
#include <stdint.h>

namespace alpaka_serial_sync {
void listDevices(const std::string& backend) {
const char tab = '\t';
const std::vector<Device> devices = alpaka::getDevs<Platform>();
if (devices.empty()) {
std::cout << "No devices found for the " << backend << " backend." << std::endl;
return;
} else {
std::cout << backend << " devices found: \n";
for (size_t i{}; i < devices.size(); ++i) {
std::cout << tab << "device " << i << ": " << alpaka::getName(devices[i]) << '\n';
}
}
}

std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
Expand Down Expand Up @@ -202,6 +216,7 @@ namespace alpaka_serial_sync {
PYBIND11_MODULE(CLUE_CPU_Serial, m) {
m.doc() = "Binding of the CLUE algorithm running serially on CPU";

m.def("listDevices", &listDevices, "List the available devices for the CPU serial backend");
m.def("mainRun",
pybind11::overload_cast<float,
float,
Expand Down
15 changes: 15 additions & 0 deletions CLUEstering/alpaka/BindingModules/binding_cpu_tbb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
#include <stdint.h>

namespace alpaka_tbb_async {
void listDevices(const std::string& backend) {
const char tab = '\t';
const std::vector<Device> devices = alpaka::getDevs<Platform>();
if (devices.empty()) {
std::cout << "No devices found for the " << backend << " backend." << std::endl;
return;
} else {
std::cout << backend << " devices found: \n";
for (size_t i{}; i < devices.size(); ++i) {
std::cout << tab << "Device " << i << ": " << alpaka::getName(devices[i]) << '\n';
}
}
}

std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
Expand Down Expand Up @@ -216,6 +230,7 @@ namespace alpaka_tbb_async {
/* pybind11::class_<CustomKernel, ConvolutionalKernel>(m, "CustomKernel") */
/* .def(pybind11::init<kernel_t>()) */
/* .def("operator()", &CustomKernel::operator()); */
m.def("listDevices", &listDevices, "List the available devices for the TBB backend");
m.def("mainRun",
pybind11::overload_cast<float,
float,
Expand Down
15 changes: 15 additions & 0 deletions CLUEstering/alpaka/BindingModules/binding_gpu_cuda.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
using cms::alpakatools::initialise;

namespace alpaka_cuda_async {
void listDevices(const std::string& backend) {
const char tab = '\t';
const std::vector<Device> devices = alpaka::getDevs<Platform>();
if (devices.empty()) {
std::cout << "No devices found for the " << backend << " backend." << std::endl;
return;
} else {
std::cout << backend << " devices found: \n";
for (size_t i{}; i < devices.size(); ++i) {
std::cout << tab << "device " << i << ": " << alpaka::getName(devices[i]) << '\n';
}
}
}

std::vector<std::vector<int>> mainRun(float dc,
float rhoc,
float outlier,
Expand Down Expand Up @@ -208,6 +222,7 @@ namespace alpaka_cuda_async {
PYBIND11_MODULE(CLUE_GPU_CUDA, m) {
m.doc() = "Binding of the CLUE algorithm running on CUDA GPUs";

m.def("listDevices", &listDevices, "List the available devices for the CUDA backend");
m.def("mainRun",
pybind11::overload_cast<float,
float,
Expand Down

0 comments on commit 2a378f6

Please sign in to comment.