Skip to content

Commit a4a6f04

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

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,20 +519,19 @@ def centroid(image, *, spacing=None):
519519

520520

521521
def _get_inertia_tensor_2x2_kernel():
522+
kernel_directory = os.path.join(
523+
os.path.normpath(os.path.dirname(__file__)), 'cuda'
524+
)
525+
with open(os.path.join(kernel_directory, "moments.h"), 'rt') as f:
526+
preamble = f.read()
527+
522528
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;
529+
inertia_tensor_2x2(mu, result);
532530
"""
533531
return cp.ElementwiseKernel(
534532
in_params='raw F mu',
535533
out_params='raw F result',
534+
preamble=preamble,
536535
operation=operation,
537536
name='cucim_skimage_measure_inertia_tensor_2x2'
538537
)
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)