Add Python bindings for CUDA cluster finder
Build on RHEL8 / build (push) Successful in 2m50s
Build on RHEL9 / build (push) Successful in 2m57s
Run tests using data on local RHEL8 / build (push) Successful in 3m38s

- Add bind_ClusterFinderCUDA.hpp with pybind11 bindings for
  ClusterFinderCUDA
- Build CUDA bindings as separate _aare_cuda.so to avoid
  segfaults from mixing nvcc and gcc compiled code in the
  same shared object
- Re-export CUDA classes onto _aare in __init__.py so user
  code uses `from aare import ClusterFinderCUDA` regardless
  of which .so hosts the class
- Factory in ClusterFinder.py selects backend; RuntimeError
  if GPU requested on CPU-only build
- Update python/CMakeLists.txt: _aare_cuda module gated
  behind AARE_CUDA and AARE_PYTHON_BINDINGS
- Add validation notebook: ~20x speedup vs sequential ClusterFinder
This commit is contained in:
kferjaoui committed 2026-04-23 11:43:40 +02:00
1 parent 3ed773e520
commit e894bdac9b
7 files changed
+766 -29

No files matched your search

+50 -15
View File
@@ -15,20 +15,50 @@ else()
find_package(pybind11 2.13 REQUIRED)
endif()
# Add the compiled python extension
pybind11_add_module(
_aare # name of the module
src/module.cpp # source file
)
set_target_properties(_aare PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)
# ---- Main CPU module --------------------------------------------------------
# module.cpp is the only source for the main module. When AARE_CUDA=ON, the
# CUDA bindings live in a *separate* Python extension (_aare_cuda.so) loaded
# independently at runtime. This isolates the nvcc-compiled translation unit
# into its own ELF image so pybind11's type registry cannot be corrupted by
# weak-symbol collisions between gcc-emitted and nvcc-emitted template
# instantiations.
pybind11_add_module(_aare NO_EXTRAS src/module.cpp)
target_link_libraries(_aare PRIVATE aare_core aare_compiler_flags)
target_include_directories(_aare SYSTEM PRIVATE
$<TARGET_PROPERTY:Minuit2::Minuit2,INTERFACE_INCLUDE_DIRECTORIES>
)
set_target_properties(_aare PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/aare
INTERPROCEDURAL_OPTIMIZATION FALSE
)
# ---- CUDA module (separate .so) --------------------------------------------
if(AARE_CUDA)
pybind11_add_module(_aare_cuda NO_EXTRAS src/cuda_bindings.cu)
target_link_libraries(_aare_cuda PRIVATE aare_cuda aare_compiler_flags)
target_include_directories(_aare_cuda SYSTEM PRIVATE
$<TARGET_PROPERTY:Minuit2::Minuit2,INTERFACE_INCLUDE_DIRECTORIES>
)
set_target_properties(_aare_cuda PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/aare
INTERPROCEDURAL_OPTIMIZATION FALSE
CUDA_RESOLVE_DEVICE_SYMBOLS ON
CUDA_SEPARABLE_COMPILATION ON
)
target_compile_options(_aare_cuda PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr>
$<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-fvisibility=hidden>
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-fPIC>
)
endif()
# List of python files to be copied to the build directory
set( PYTHON_FILES
@@ -51,9 +81,9 @@ foreach(FILE ${PYTHON_FILES})
configure_file(${FILE} ${CMAKE_BINARY_DIR}/${FILE} )
endforeach(FILE ${PYTHON_FILES})
set_target_properties(_aare PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/aare
)
# set_target_properties(_aare PROPERTIES
# LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/aare
# )
set(PYTHON_EXAMPLES
examples/play.py
@@ -69,8 +99,13 @@ endforeach(FILE ${PYTHON_EXAMPLES})
if(AARE_INSTALL_PYTHONEXT)
set(AARE_PY_INSTALL_TARGETS _aare)
if(AARE_CUDA)
list(APPEND AARE_PY_INSTALL_TARGETS _aare_cuda)
endif()
install(
TARGETS _aare
TARGETS ${AARE_PY_INSTALL_TARGETS}
EXPORT "${TARGETS_EXPORT_NAME}"
LIBRARY DESTINATION aare
COMPONENT python
@@ -80,5 +115,5 @@ if(AARE_INSTALL_PYTHONEXT)
FILES ${PYTHON_FILES}
DESTINATION aare
COMPONENT python
)
)
endif()
+38
View File
@@ -49,6 +49,44 @@ def ClusterFinderMT(image_size, cluster_size = (3,3), dtype=np.int32, n_sigma=5,
return cls(image_size, n_sigma=n_sigma, capacity=capacity, n_threads=n_threads)
def _cuda_available():
"""True if this build of aare was compiled with -DAARE_CUDA=ON."""
return hasattr(_aare, "ClusterFinderCUDA_Cluster3x3i")
def ClusterFinderCUDA(image_size, cluster_size=(3,3), n_sigma=5, dtype=np.int32,
capacity=1024, n_streams=1):
"""
Factory function to create a ClusterFinderCUDA object. Provides a cleaner
syntax for the templated ClusterFinderCUDA in C++. API mirrors
ClusterFinder() plus CUDA-specific knobs (n_streams).
.. code-block:: python
from aare import ClusterFinderCUDA
cf = ClusterFinderCUDA(image_size=(512, 1024),
cluster_size=(3, 3),
n_sigma=5,
n_streams=4)
for frame in pedestal_frames:
cf.push_pedestal_frame(frame)
for i, frame in enumerate(data_frames):
cf.find_clusters(frame, frame_number=i)
clusters = cf.steal_clusters()
"""
if not _cuda_available():
raise RuntimeError(
"ClusterFinderCUDA is not available in this build of aare. "
"Rebuild with -DAARE_CUDA=ON (and -DAARE_PYTHON_BINDINGS=ON)."
)
cls = _get_class("ClusterFinderCUDA", cluster_size, dtype)
return cls(image_size,
n_sigma=n_sigma,
capacity=capacity,
n_streams=n_streams)
def ClusterCollector(clusterfindermt, dtype=np.int32):
"""
Factory function to create a ClusterCollector object. Provides a cleaner syntax for
+18
View File
@@ -2,6 +2,23 @@
# Make the compiled classes that live in _aare available from aare.
from . import _aare
# ---- CUDA module (optional) ------------------------------------------------
# When the package was built with AARE_CUDA=ON, a sibling extension
# _aare_cuda contains the ClusterFinderCUDA_* classes. We re-export them
# onto _aare so user code can do `from aare import ClusterFinderCUDA_*`
# regardless of which .so physically hosts the class. On a CPU-only build
# the import fails silently and ClusterFinderCUDA_* classes simply aren't
# present; the factory in ClusterFinder.py handles that case with a clear
# RuntimeError.
try:
from . import _aare_cuda as _aare_cuda_mod
for _name in dir(_aare_cuda_mod):
if _name.startswith("ClusterFinderCUDA"):
setattr(_aare, _name, getattr(_aare_cuda_mod, _name))
del _name
except ImportError:
pass
from . import transform
from ._aare import File, RawMasterFile, RawSubFile, JungfrauDataFile
@@ -14,6 +31,7 @@ from ._aare import corner
# from ._aare import ClusterFinderMT, ClusterCollector, ClusterFileSink, ClusterVector_i
from .ClusterFinder import ClusterFinder, ClusterCollector, ClusterFinderMT, ClusterFileSink, ClusterFile
from .ClusterFinder import ClusterFinderCUDA, _cuda_available
from .ClusterVector import ClusterVector
from .Cluster import Cluster
+101
View File
@@ -0,0 +1,101 @@
// SPDX-License-Identifier: MPL-2.0
#pragma once
#include "aare/ClusterFinderCUDA.hpp"
#include "aare/ClusterVector.hpp"
#include "aare/NDView.hpp"
#include "aare/Pedestal.hpp"
#include "np_helper.hpp"
#include <cstdint>
#include <pybind11/pybind11.h>
// #include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
using pd_type = double;
using namespace aare;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
namespace aare {
template <typename T, uint8_t ClusterSizeX, uint8_t ClusterSizeY,
typename CoordType = uint16_t>
void define_ClusterFinderCUDA(py::module &m, const std::string &typestr) {
auto class_name = fmt::format("ClusterFinderCUDA_{}", typestr);
using ClusterType = Cluster<T, ClusterSizeX, ClusterSizeY, CoordType>;
using CF = ClusterFinderCUDA<ClusterType, uint16_t, pd_type>;
py::class_<CF>(m, class_name.c_str())
.def(py::init<Shape<2>, pd_type, size_t, int>(),
py::arg("image_size"),
py::arg("n_sigma") = 5.0,
py::arg("capacity") = 1'000'000,
py::arg("n_streams") = 1)
.def_property(
"nSigma",
&CF::get_nSigma,
&CF::set_nSigma,
R"(Number of sigma above the pedestal to consider a photon during cluster finding.)")
.def("push_pedestal_frame",
[](CF &self, py::array_t<uint16_t> frame) {
auto view = make_view_2d(frame);
self.push_pedestal_frame(view);
})
.def("clear_pedestal", &CF::clear_pedestal)
.def_property_readonly(
"pedestal",
[](CF &self) {
auto pd = new NDArray<pd_type, 2>{};
*pd = self.pedestal();
return return_image_data(pd);
})
.def_property_readonly(
"noise",
[](CF &self) {
auto arr = new NDArray<pd_type, 2>{};
*arr = self.noise();
return return_image_data(arr);
})
.def(
"steal_clusters",
[](CF &self, bool realloc_same_capacity) {
ClusterVector<ClusterType> clusters =
self.steal_clusters(realloc_same_capacity);
return clusters;
},
py::arg("realloc_same_capacity") = false)
.def(
"find_clusters",
[](CF &self, py::array_t<uint16_t> frame, uint64_t frame_number) {
auto view = make_view_2d(frame);
self.find_clusters(view, frame_number);
},
py::arg("frame"), py::arg("frame_number") = 0)
.def(
"find_clusters_batched",
[](CF &self, py::array_t<uint16_t> frames, uint64_t first_frame) {
// frames is expected as a 3D numpy array (n_frames, nrows, ncols)
auto view = make_view_3d(frames);
return self.find_clusters_batched(view, first_frame);
},
py::arg("frames"), py::arg("first_frame") = 0,
R"(Process a 3D array of frames (n_frames, nrows, ncols) in parallel
across the configured CUDA streams. Returns a list of ClusterVector, one per
input frame.)");
}
} // namespace aare
#pragma GCC diagnostic pop
+67
View File
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MPL-2.0
//
// CUDA-only Python extension module. Registers ClusterFinderCUDA along with
// the ClusterVector and Cluster types it exposes in its return values, so
// the module is self-contained — users can call steal_clusters() and get
// back a usable ClusterVector without _aare needing to be imported first.
#include "bind_Cluster.hpp"
#include "bind_ClusterVector.hpp"
#include "bind_ClusterFinderCUDA.hpp"
#include <pybind11/pybind11.h>
namespace py = pybind11;
// Register the Cluster + ClusterVector pair for one (T, N, M) combination.
// Subset of DEFINE_CLUSTER_BINDINGS from module.cpp: we register what
// ClusterFinderCUDA actually returns, nothing more. File I/O, eta and
// reduce_to_2x2 stay on the CPU side.
#define DEFINE_CUDA_CLUSTER_TYPES(T, N, M, U, TYPE_CODE) \
define_ClusterVector<T, N, M, U>(m, "Cluster" #N "x" #M #TYPE_CODE); \
define_Cluster<T, N, M, U>(m, #N "x" #M #TYPE_CODE);
#define DEFINE_BINDINGS_CLUSTERFINDER_CUDA(T, N, M, U, TYPE_CODE) \
aare::define_ClusterFinderCUDA<T, N, M, U>( \
m, "Cluster" #N "x" #M #TYPE_CODE);
PYBIND11_MODULE(_aare_cuda, m) {
// Types first — finders reference them in their signatures.
// SFINAE excludes 2x2 on ClusterFinderCUDA, so we skip it here too.
DEFINE_CUDA_CLUSTER_TYPES(int, 3, 3, uint16_t, i);
DEFINE_CUDA_CLUSTER_TYPES(double, 3, 3, uint16_t, d);
DEFINE_CUDA_CLUSTER_TYPES(float, 3, 3, uint16_t, f);
DEFINE_CUDA_CLUSTER_TYPES(int, 5, 5, uint16_t, i);
DEFINE_CUDA_CLUSTER_TYPES(double, 5, 5, uint16_t, d);
DEFINE_CUDA_CLUSTER_TYPES(float, 5, 5, uint16_t, f);
DEFINE_CUDA_CLUSTER_TYPES(int, 7, 7, uint16_t, i);
DEFINE_CUDA_CLUSTER_TYPES(double, 7, 7, uint16_t, d);
DEFINE_CUDA_CLUSTER_TYPES(float, 7, 7, uint16_t, f);
DEFINE_CUDA_CLUSTER_TYPES(int, 9, 9, uint16_t, i);
DEFINE_CUDA_CLUSTER_TYPES(double, 9, 9, uint16_t, d);
DEFINE_CUDA_CLUSTER_TYPES(float, 9, 9, uint16_t, f);
// Finders
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(int, 3, 3, uint16_t, i);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(double, 3, 3, uint16_t, d);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(float, 3, 3, uint16_t, f);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(int, 5, 5, uint16_t, i);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(double, 5, 5, uint16_t, d);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(float, 5, 5, uint16_t, f);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(int, 7, 7, uint16_t, i);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(double, 7, 7, uint16_t, d);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(float, 7, 7, uint16_t, f);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(int, 9, 9, uint16_t, i);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(double, 9, 9, uint16_t, d);
DEFINE_BINDINGS_CLUSTERFINDER_CUDA(float, 9, 9, uint16_t, f);
}
#undef DEFINE_CUDA_CLUSTER_TYPES
#undef DEFINE_BINDINGS_CLUSTERFINDER_CUDA
+422
View File
@@ -0,0 +1,422 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "789d5aab-0c75-4ed3-94bf-e66172f6737c",
"metadata": {},
"outputs": [],
"source": [
"import sys; sys.path.append('/home/ferjao_k/aare/build')\n",
"\n",
"from pathlib import Path\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
"import numpy as np\n",
"import boost_histogram as bh\n",
"import time\n",
"\n",
"from aare import File, ClusterFinder, ClusterFinderMT, ClusterCollector, ClusterFinderCUDA"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3f6089d6-2245-4aca-aad3-2417c16afae2",
"metadata": {},
"outputs": [],
"source": [
"def make_hist(clusters):\n",
" h = bh.Histogram(bh.axis.Regular(100, -2, 4000))\n",
" h.fill(clusters.sum())\n",
" return h"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "415b09d4-d0d0-4166-8601-9d84302617bd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image size: (400, 400)\n",
"Pedestal frames: 1000\n",
"Data frames: 40000\n"
]
}
],
"source": [
"base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/')\n",
"f = File(base / 'Moench03new/cu_half_speed_master_4.json')\n",
"\n",
"n_frames_pd = 1000\n",
"N = 40000\n",
"cluster_size = (3, 3)\n",
"image_size = (f.rows, f.cols)\n",
"capacity = 100_000 #3_000_000\n",
"\n",
"print(f'Image size: {image_size}')\n",
"print(f'Pedestal frames: {n_frames_pd}')\n",
"print(f'Data frames: {N}')"
]
},
{
"cell_type": "markdown",
"id": "5531b79d-12ad-4de6-84b7-0224b10d13c3",
"metadata": {},
"source": [
"## Pedestal (both finders trained on identical frames)"
]
},
{
"cell_type": "markdown",
"id": "e33b421f-969a-41d6-8aa6-5bd859a88fb3",
"metadata": {},
"source": [
"- Modify the boolean `SERIAL` to choose between the sequential CPU version (ClusterFinder) and its multi-threaded homologue (ClusterFinderMT)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6ee7c469-5a23-421a-9bb4-a76e8f57a0c1",
"metadata": {},
"outputs": [],
"source": [
"SERIAL = True"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9d8f69bf-27f6-48c8-b2da-4157d67e6b04",
"metadata": {},
"outputs": [],
"source": [
"if(SERIAL):\n",
" cf_cpu = ClusterFinder(image_size, cluster_size, capacity=capacity)\n",
"else:\n",
" cf_cpu = ClusterFinderMT(image_size, cluster_size, capacity=capacity, n_threads=24)\n",
" sink = ClusterCollector(cf_cpu)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cbdcb805-708b-4205-bda9-2aa163d0e81f",
"metadata": {},
"outputs": [],
"source": [
"N_STREAMS = 1 \n",
"cf_cuda = ClusterFinderCUDA(image_size, cluster_size, capacity=capacity, n_streams=N_STREAMS)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1546f405-1bf6-4073-ab35-8134be695a6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pedestal (1000 frames): 0.498s\n"
]
}
],
"source": [
"t0 = time.perf_counter()\n",
"for _ in range(n_frames_pd):\n",
" img = f.read_frame()\n",
" cf_cpu.push_pedestal_frame(img.copy())\n",
" cf_cuda.push_pedestal_frame(img.copy())\n",
"print(f'Pedestal ({n_frames_pd} frames): {time.perf_counter() - t0:.3f}s')"
]
},
{
"cell_type": "markdown",
"id": "5df035f0-7a27-4d5a-8f8d-c94e745bcd1a",
"metadata": {},
"source": [
"## Read all data frames into memory (I/O out of the timing loop)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4573be3d-5ba8-4e18-bab0-c874f2b7dcb2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading 40000 frames: 2.002s (19985 FPS, 6098.804 GB/s)\n"
]
}
],
"source": [
"f.seek(n_frames_pd)\n",
"t0 = time.perf_counter()\n",
"data = f.read_n(N)\n",
"t_io = time.perf_counter() - t0\n",
"print(f'Reading {N} frames: {t_io:.3f}s ({N/t_io:.0f} FPS, '\n",
" f'{f.bytes_per_frame * N / 1024**2 / t_io:.3f} GB/s)')"
]
},
{
"cell_type": "markdown",
"id": "c1e9362e-dcc2-41af-bfa1-c62d0968477e",
"metadata": {},
"source": [
"## CPU clustering"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "fbb14fda-2852-4b73-ba2c-9952abac1d99",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU clustering: 73.978s (541 FPS, 55449596 clusters, 1386.24/frame)\n"
]
}
],
"source": [
"t0 = time.perf_counter()\n",
"for frame in data:\n",
" cf_cpu.find_clusters(frame)\n",
"t_cpu = time.perf_counter() - t0\n",
"\n",
"if(SERIAL):\n",
" clusters_cpu = cf_cpu.steal_clusters(realloc_same_capacity=False)\n",
" n_clusters_cpu = clusters_cpu.size\n",
" \n",
" hist_cpu = make_hist(clusters_cpu)\n",
"else:\n",
" cf_cpu.stop()\n",
" sink.stop()\n",
" \n",
" clusters_cpu = sink.steal_clusters() #cf_cpu.steal_clusters(realloc_same_capacity=False)\n",
" \n",
" hist_cpu = bh.Histogram(bh.axis.Regular(100, -2, 4000))\n",
" n_clusters_cpu = 0\n",
" for cv in clusters_cpu:\n",
" hist_cpu.fill(cv.sum())\n",
" n_clusters_cpu += cv.size\n",
" \n",
"print(f'CPU clustering: {t_cpu:.3f}s ({N/t_cpu:.0f} FPS, '\n",
" f'{n_clusters_cpu} clusters, {n_clusters_cpu/N:.2f}/frame)')"
]
},
{
"cell_type": "markdown",
"id": "8cfd7091-8020-49ff-b033-28bf773983d8",
"metadata": {},
"source": [
"## CUDA clustering"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4b8df93b-9a1b-41a5-9fed-9cda295fb523",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CUDA clustering: 3.880s (10310 FPS, 55810704 clusters, 1395.27/frame)\n",
"Speedup (CPU / CUDA): 19.07×\n"
]
}
],
"source": [
"# Warmup: first kernel launch pays CUDA context + pedestal H2D upload cost\n",
"cf_cuda.find_clusters(data[0])\n",
"_ = cf_cuda.steal_clusters(realloc_same_capacity=False)\n",
"\n",
"t0 = time.perf_counter()\n",
"for frame in data:\n",
" cf_cuda.find_clusters(frame)\n",
"t_cuda = time.perf_counter() - t0\n",
"clusters_cuda = cf_cuda.steal_clusters(realloc_same_capacity=False)\n",
"n_clusters_cuda = clusters_cuda.size\n",
"print(f'CUDA clustering: {t_cuda:.3f}s ({N/t_cuda:.0f} FPS, '\n",
" f'{n_clusters_cuda} clusters, {n_clusters_cuda/N:.2f}/frame)')\n",
"print(f'Speedup (CPU / CUDA): {t_cpu / t_cuda:.2f}×')\n",
"\n",
"hist_cuda = make_hist(clusters_cuda)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1fda99ee-db54-40c1-8e5b-51168a33fb96",
"metadata": {},
"outputs": [],
"source": [
"# BATCH_SIZE = 500\n",
"\n",
"# # warmup\n",
"# _ = cf_cuda.find_clusters_batched(data[:1], first_frame=0)\n",
"\n",
"# t0 = time.perf_counter()\n",
"# clusters_cuda_per_frame = []\n",
"# for start in range(0, N, BATCH_SIZE):\n",
"# stop = min(start + BATCH_SIZE, N)\n",
"# clusters_cuda_per_frame.extend(\n",
"# cf_cuda.find_clusters_batched(data[start:stop], first_frame=start)\n",
"# )\n",
"# t_cuda = time.perf_counter() - t0\n",
"\n",
"# n_clusters_cuda = sum(cv.size for cv in clusters_cuda_per_frame)\n",
"\n",
"# print(f'CUDA clustering: {t_cuda:.3f}s ({N/t_cuda:.0f} FPS, '\n",
"# f'{n_clusters_cuda} clusters, {n_clusters_cuda/N:.2f}/frame)')\n",
"# print(f'Speedup (CPU / CUDA): {t_cpu / t_cuda:.2f}×')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b7df7b9d-6873-46c4-b6bc-7e4238a5bdf7",
"metadata": {},
"outputs": [],
"source": [
"# def make_hist_from_batch(result_list):\n",
"# h = bh.Histogram(bh.axis.Regular(100, -2, 4000))\n",
"# energies = [np.asarray(cv.sum()).ravel() for cv in result_list if cv.size > 0]\n",
"# if energies:\n",
"# h.fill(np.concatenate(energies))\n",
"# return h\n",
"\n",
"# hist_cuda = make_hist_from_batch(clusters_cuda_per_frame)"
]
},
{
"cell_type": "markdown",
"id": "b966cce1-0f73-4565-a707-1aec2a69f75e",
"metadata": {},
"source": [
"## Agreement check: \n",
"- Cluster counts should match closely.\n",
"- However, as the CUDA CF updates the pedestal once per frame rather than per-pixel, a small divergence after the first few frames is expected."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "d3a850df-7df0-485b-971d-381cfdc6be81",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cluster count diff: 361108 (0.65%)\n"
]
}
],
"source": [
"diff = abs(n_clusters_cpu - n_clusters_cuda)\n",
"rel = diff / max(n_clusters_cpu, 1)\n",
"print(f'Cluster count diff: {diff} ({rel:.2%})')"
]
},
{
"cell_type": "markdown",
"id": "81638ad3-6112-4bd7-a93a-f4d97f1f94cf",
"metadata": {},
"source": [
"## Plots"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9adeea2f-9309-4c0a-905b-7d1203ba1f4e",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3kA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6AYveNFguq2uwk7Y1zRTrDwkl28rxfsfU1GtS2mZ1KIi2C70IvbPdarvSCZO+//77UjcNZumyCQSugM7kVUT/g9e+/PqLnbNrhM68pr/y6X8c+mWrU3Zr9wn91dDZPeTuu+82M6npLFwAao92u9KTbQ0KdAY8vZ6VdtHSEy8do6XTnms2vuQ4NCftcqcnZRMnTjRj4DQzpF0A9ZIAxelJt54U6o8+bdu2NV3O9POu6zUb4KQBhgYy2jVMfwzSk01dpzM76q/vejFp/b7QcWw6fkwzCnpyrMFNZU/8itOTP53dUWfU0+8m/UFKX4dm0XRmTZ3d0nlJg8rSSxJoTwadlVC7pmu3N+1ept+LehKpM29qUFYRfb16oqkBkZZPMxd6cqrd25XOJlkyY6Jj3HQWSC2zzl55LJV9T5R+n2uXPs3yaWZKZ8bUrI/20nBmpir7HmnXUp2pUcd2aT1rcKgBnbY3PZaOO6uoLVRE61y7ETpn7dQAXrselnWJieOl/4/pmEbtwniscXlaxxroaWCh76GLine truncated
"text/plain": [
"<Figure size 900x600 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, (ax_spec, ax_ratio) = plt.subplots(\n",
" 2, 1, figsize=(9, 6), sharex=True,\n",
" gridspec_kw={'height_ratios': [3, 1]}\n",
")\n",
"\n",
"edges = hist_cpu.axes[0].edges\n",
"cpu_vals = hist_cpu.values()\n",
"cuda_vals = hist_cuda.values()\n",
"\n",
"ax_spec.stairs(cpu_vals, edges, label=f'CPU ({n_clusters_cpu} clusters)')\n",
"ax_spec.stairs(cuda_vals, edges, label=f'CUDA ({n_clusters_cuda} clusters)', linestyle='--')\n",
"ax_spec.set_ylabel('Counts')\n",
"ax_spec.set_title('Cluster energy spectrum: CPU vs CUDA')\n",
"ax_spec.legend()\n",
"ax_spec.grid(alpha=0.3)\n",
"\n",
"with np.errstate(divide='ignore', invalid='ignore'):\n",
" ratio = np.where(cpu_vals > 0, cuda_vals / cpu_vals, np.nan)\n",
"\n",
"ax_ratio.stairs(ratio, edges, color='k')\n",
"ax_ratio.axhline(1.0, color='gray', linewidth=0.5)\n",
"ax_ratio.set_ylabel('CUDA / CPU')\n",
"ax_ratio.set_xlabel('Energy [ADU]')\n",
"ax_ratio.set_ylim(0.5, 3.5)\n",
"ax_ratio.grid(alpha=0.3)\n",
"\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6ca4581-bb29-4c74-a34a-bd427551a39b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}