|
| 1 | +#include <cuda_runtime.h> |
| 2 | +#include <nanobind/nanobind.h> |
| 3 | +#include <cstdint> |
| 4 | + |
| 5 | +#include "kernels_ligrec.cuh" |
| 6 | + |
| 7 | +namespace nb = nanobind; |
| 8 | + |
| 9 | +template <typename T> |
| 10 | +static inline void launch_sum_count_dense(std::uintptr_t data, std::uintptr_t clusters, |
| 11 | + std::uintptr_t sum, std::uintptr_t count, int rows, |
| 12 | + int cols, int ncls) { |
| 13 | + dim3 block(32, 32); |
| 14 | + dim3 grid((rows + block.x - 1) / block.x, (cols + block.y - 1) / block.y); |
| 15 | + sum_and_count_dense_kernel<T> |
| 16 | + <<<grid, block>>>(reinterpret_cast<const T*>(data), reinterpret_cast<const int*>(clusters), |
| 17 | + reinterpret_cast<T*>(sum), reinterpret_cast<int*>(count), rows, cols, ncls); |
| 18 | +} |
| 19 | + |
| 20 | +template <typename T> |
| 21 | +static inline void launch_sum_count_sparse(std::uintptr_t indptr, std::uintptr_t index, |
| 22 | + std::uintptr_t data, std::uintptr_t clusters, |
| 23 | + std::uintptr_t sum, std::uintptr_t count, int rows, |
| 24 | + int ncls) { |
| 25 | + dim3 block(32); |
| 26 | + dim3 grid((rows + block.x - 1) / block.x); |
| 27 | + sum_and_count_sparse_kernel<T> |
| 28 | + <<<grid, block>>>(reinterpret_cast<const int*>(indptr), reinterpret_cast<const int*>(index), |
| 29 | + reinterpret_cast<const T*>(data), reinterpret_cast<const int*>(clusters), |
| 30 | + reinterpret_cast<T*>(sum), reinterpret_cast<int*>(count), rows, ncls); |
| 31 | +} |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +static inline void launch_mean_dense(std::uintptr_t data, std::uintptr_t clusters, std::uintptr_t g, |
| 35 | + int rows, int cols, int ncls) { |
| 36 | + dim3 block(32, 32); |
| 37 | + dim3 grid((rows + block.x - 1) / block.x, (cols + block.y - 1) / block.y); |
| 38 | + mean_dense_kernel<T><<<grid, block>>>(reinterpret_cast<const T*>(data), |
| 39 | + reinterpret_cast<const int*>(clusters), |
| 40 | + reinterpret_cast<T*>(g), rows, cols, ncls); |
| 41 | +} |
| 42 | + |
| 43 | +template <typename T> |
| 44 | +static inline void launch_mean_sparse(std::uintptr_t indptr, std::uintptr_t index, |
| 45 | + std::uintptr_t data, std::uintptr_t clusters, |
| 46 | + std::uintptr_t g, int rows, int ncls) { |
| 47 | + dim3 block(32); |
| 48 | + dim3 grid((rows + block.x - 1) / block.x); |
| 49 | + mean_sparse_kernel<T> |
| 50 | + <<<grid, block>>>(reinterpret_cast<const int*>(indptr), reinterpret_cast<const int*>(index), |
| 51 | + reinterpret_cast<const T*>(data), reinterpret_cast<const int*>(clusters), |
| 52 | + reinterpret_cast<T*>(g), rows, ncls); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename T> |
| 56 | +static inline void launch_elementwise_diff(std::uintptr_t g, std::uintptr_t total_counts, |
| 57 | + int n_genes, int n_clusters) { |
| 58 | + dim3 block(32, 32); |
| 59 | + dim3 grid((n_genes + block.x - 1) / block.x, (n_clusters + block.y - 1) / block.y); |
| 60 | + elementwise_diff_kernel<T><<<grid, block>>>( |
| 61 | + reinterpret_cast<T*>(g), reinterpret_cast<const T*>(total_counts), n_genes, n_clusters); |
| 62 | +} |
| 63 | + |
| 64 | +template <typename T> |
| 65 | +static inline void launch_interaction(std::uintptr_t interactions, |
| 66 | + std::uintptr_t interaction_clusters, std::uintptr_t mean, |
| 67 | + std::uintptr_t res, std::uintptr_t mask, std::uintptr_t g, |
| 68 | + int n_iter, int n_inter_clust, int ncls) { |
| 69 | + dim3 block(32, 32); |
| 70 | + dim3 grid((n_iter + block.x - 1) / block.x, (n_inter_clust + block.y - 1) / block.y); |
| 71 | + interaction_kernel<T><<<grid, block>>>( |
| 72 | + reinterpret_cast<const int*>(interactions), |
| 73 | + reinterpret_cast<const int*>(interaction_clusters), reinterpret_cast<const T*>(mean), |
| 74 | + reinterpret_cast<T*>(res), reinterpret_cast<const bool*>(mask), reinterpret_cast<const T*>(g), |
| 75 | + n_iter, n_inter_clust, ncls); |
| 76 | +} |
| 77 | + |
| 78 | +template <typename T> |
| 79 | +static inline void launch_res_mean(std::uintptr_t interactions, std::uintptr_t interaction_clusters, |
| 80 | + std::uintptr_t mean, std::uintptr_t res_mean, int n_inter, |
| 81 | + int n_inter_clust, int ncls) { |
| 82 | + dim3 block(32, 32); |
| 83 | + dim3 grid((n_inter + block.x - 1) / block.x, (n_inter_clust + block.y - 1) / block.y); |
| 84 | + res_mean_kernel<T><<<grid, block>>>(reinterpret_cast<const int*>(interactions), |
| 85 | + reinterpret_cast<const int*>(interaction_clusters), |
| 86 | + reinterpret_cast<const T*>(mean), |
| 87 | + reinterpret_cast<T*>(res_mean), n_inter, n_inter_clust, ncls); |
| 88 | +} |
| 89 | + |
| 90 | +NB_MODULE(_ligrec_cuda, m) { |
| 91 | + m.def("sum_count_dense", [](std::uintptr_t data, std::uintptr_t clusters, std::uintptr_t sum, |
| 92 | + std::uintptr_t count, int rows, int cols, int ncls, int itemsize) { |
| 93 | + if (itemsize == 4) { |
| 94 | + launch_sum_count_dense<float>(data, clusters, sum, count, rows, cols, ncls); |
| 95 | + } else if (itemsize == 8) { |
| 96 | + launch_sum_count_dense<double>(data, clusters, sum, count, rows, cols, ncls); |
| 97 | + } else { |
| 98 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + m.def("sum_count_sparse", [](std::uintptr_t indptr, std::uintptr_t index, std::uintptr_t data, |
| 103 | + std::uintptr_t clusters, std::uintptr_t sum, std::uintptr_t count, |
| 104 | + int rows, int ncls, int itemsize) { |
| 105 | + if (itemsize == 4) { |
| 106 | + launch_sum_count_sparse<float>(indptr, index, data, clusters, sum, count, rows, ncls); |
| 107 | + } else if (itemsize == 8) { |
| 108 | + launch_sum_count_sparse<double>(indptr, index, data, clusters, sum, count, rows, ncls); |
| 109 | + } else { |
| 110 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + m.def("mean_dense", [](std::uintptr_t data, std::uintptr_t clusters, std::uintptr_t g, int rows, |
| 115 | + int cols, int ncls, int itemsize) { |
| 116 | + if (itemsize == 4) { |
| 117 | + launch_mean_dense<float>(data, clusters, g, rows, cols, ncls); |
| 118 | + } else if (itemsize == 8) { |
| 119 | + launch_mean_dense<double>(data, clusters, g, rows, cols, ncls); |
| 120 | + } else { |
| 121 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + m.def("mean_sparse", |
| 126 | + [](std::uintptr_t indptr, std::uintptr_t index, std::uintptr_t data, |
| 127 | + std::uintptr_t clusters, std::uintptr_t g, int rows, int ncls, int itemsize) { |
| 128 | + if (itemsize == 4) { |
| 129 | + launch_mean_sparse<float>(indptr, index, data, clusters, g, rows, ncls); |
| 130 | + } else if (itemsize == 8) { |
| 131 | + launch_mean_sparse<double>(indptr, index, data, clusters, g, rows, ncls); |
| 132 | + } else { |
| 133 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + m.def("elementwise_diff", [](std::uintptr_t g, std::uintptr_t total_counts, int n_genes, |
| 138 | + int n_clusters, int itemsize) { |
| 139 | + if (itemsize == 4) { |
| 140 | + launch_elementwise_diff<float>(g, total_counts, n_genes, n_clusters); |
| 141 | + } else if (itemsize == 8) { |
| 142 | + launch_elementwise_diff<double>(g, total_counts, n_genes, n_clusters); |
| 143 | + } else { |
| 144 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + m.def("interaction", [](std::uintptr_t interactions, std::uintptr_t interaction_clusters, |
| 149 | + std::uintptr_t mean, std::uintptr_t res, std::uintptr_t mask, |
| 150 | + std::uintptr_t g, int n_iter, int n_inter_clust, int ncls, int itemsize) { |
| 151 | + if (itemsize == 4) { |
| 152 | + launch_interaction<float>(interactions, interaction_clusters, mean, res, mask, g, n_iter, |
| 153 | + n_inter_clust, ncls); |
| 154 | + } else if (itemsize == 8) { |
| 155 | + launch_interaction<double>(interactions, interaction_clusters, mean, res, mask, g, n_iter, |
| 156 | + n_inter_clust, ncls); |
| 157 | + } else { |
| 158 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + m.def("res_mean", |
| 163 | + [](std::uintptr_t interactions, std::uintptr_t interaction_clusters, std::uintptr_t mean, |
| 164 | + std::uintptr_t res_mean, int n_inter, int n_inter_clust, int ncls, int itemsize) { |
| 165 | + if (itemsize == 4) { |
| 166 | + launch_res_mean<float>(interactions, interaction_clusters, mean, res_mean, n_inter, |
| 167 | + n_inter_clust, ncls); |
| 168 | + } else if (itemsize == 8) { |
| 169 | + launch_res_mean<double>(interactions, interaction_clusters, mean, res_mean, n_inter, |
| 170 | + n_inter_clust, ncls); |
| 171 | + } else { |
| 172 | + throw nb::value_error("Unsupported itemsize (expected 4 or 8)"); |
| 173 | + } |
| 174 | + }); |
| 175 | +} |
0 commit comments