Skip to content

Commit

Permalink
Try refactoring some CUDA code out of cuCIM
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Dec 6, 2022
1 parent 6c50a4e commit e6da017
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 9 additions & 9 deletions python/cucim/src/cucim/skimage/measure/_moments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
import os

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


def _get_inertia_tensor_2x2_kernel():
kernel_directory = os.path.join(
os.path.normpath(os.path.dirname(__file__)), 'cuda'
)
with open(os.path.join(kernel_directory, "moments.h"), 'rt') as f:
preamble = f.read()

operation = """
F mu0, mxx, mxy, myy;
mu0 = mu[0];
mxx = mu[6];
myy = mu[2];
mxy = mu[4];
result[0] = myy / mu0;
result[1] = result[2] = -mxy / mu0;
result[3] = mxx / mu0;
inertia_tensor_2x2(&mu[0], &result[0]);
"""
return cp.ElementwiseKernel(
in_params='raw F mu',
out_params='raw F result',
preamble=preamble,
operation=operation,
name='cucim_skimage_measure_inertia_tensor_2x2'
)
Expand Down
12 changes: 12 additions & 0 deletions python/cucim/src/cucim/skimage/measure/cuda/moments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
template<typename T>
__device__ void inertia_tensor_2x2(const T* mu, T* result){
T mu0, mxx, mxy, myy;
mu0 = mu[0];
mxx = mu[6];
myy = mu[2];
mxy = mu[4];

result[0] = myy / mu0;
result[1] = result[2] = -mxy / mu0;
result[3] = mxx / mu0;
}

0 comments on commit e6da017

Please sign in to comment.