Skip to content

Commit e6da017

Browse files
committed
Try refactoring some CUDA code out of cuCIM
1 parent 6c50a4e commit e6da017

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

python/cucim/src/cucim/skimage/measure/_moments.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import os
23

34
import cupy as cp
45
import numpy as np
@@ -519,20 +520,19 @@ def centroid(image, *, spacing=None):
519520

520521

521522
def _get_inertia_tensor_2x2_kernel():
523+
kernel_directory = os.path.join(
524+
os.path.normpath(os.path.dirname(__file__)), 'cuda'
525+
)
526+
with open(os.path.join(kernel_directory, "moments.h"), 'rt') as f:
527+
preamble = f.read()
528+
522529
operation = """
523-
F mu0, mxx, mxy, myy;
524-
mu0 = mu[0];
525-
mxx = mu[6];
526-
myy = mu[2];
527-
mxy = mu[4];
528-
529-
result[0] = myy / mu0;
530-
result[1] = result[2] = -mxy / mu0;
531-
result[3] = mxx / mu0;
530+
inertia_tensor_2x2(&mu[0], &result[0]);
532531
"""
533532
return cp.ElementwiseKernel(
534533
in_params='raw F mu',
535534
out_params='raw F result',
535+
preamble=preamble,
536536
operation=operation,
537537
name='cucim_skimage_measure_inertia_tensor_2x2'
538538
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
template<typename T>
2+
__device__ void inertia_tensor_2x2(const T* mu, T* result){
3+
T mu0, mxx, mxy, myy;
4+
mu0 = mu[0];
5+
mxx = mu[6];
6+
myy = mu[2];
7+
mxy = mu[4];
8+
9+
result[0] = myy / mu0;
10+
result[1] = result[2] = -mxy / mu0;
11+
result[3] = mxx / mu0;
12+
}

0 commit comments

Comments
 (0)