Skip to content

Commit

Permalink
Maybe speed up trim (#287)
Browse files Browse the repository at this point in the history
* Faster?

* fmt

* update kernel

---------

Co-authored-by: Severin Dicks <[email protected]>
Co-authored-by: Intron7 <[email protected]>
  • Loading branch information
3 people authored Nov 4, 2024
1 parent 8486c2d commit 0f131aa
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/rapids_singlecell/preprocessing/_kernels/_bbknn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,23 @@
}
// Process each element in the row
int min_index = 0;
for (int idx = start; idx < end; ++idx) {
float num = data[idx];
// Find the smallest value in top_k
int min_index = 0;
float min_value = top_k[0];
for (int i = 1; i < trim; ++i) {
if (top_k[i] < min_value) {
min_value = top_k[i];
if (data[idx] <= top_k[min_index]) continue;
// If current value is larger than the smallest in top_k, replace it
top_k[min_index] = data[idx];
// Find the new smallest value in top_k and set min_index
for (int i = 0; i < trim; ++i) {
if (top_k[i] < top_k[min_index]) {
min_index = i;
}
}
// If current num is larger than the smallest in top_k, replace it
if (num > min_value) {
top_k[min_index] = num;
}
}
// After processing, find the smallest value in top_k, which is the kth largest
float kth_largest = top_k[0];
for (int i = 1; i < trim; ++i) {
if (top_k[i] < kth_largest) {
kth_largest = top_k[i];
}
}
float kth_largest = top_k[min_index];
vals[row] = kth_largest;
}
"""
Expand Down

0 comments on commit 0f131aa

Please sign in to comment.