Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cubic numba proper compilation #4488

Merged
merged 34 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0eb8eef
is it overhead..
zeffii May 13, 2022
36fb17b
cast to nparray
zeffii May 13, 2022
01c47d2
alternative to newaxis
zeffii May 13, 2022
6795475
that's faster..
zeffii May 14, 2022
079c40e
try this
zeffii May 14, 2022
6a1d364
fully njit
zeffii May 14, 2022
1a6a189
remove commented out code, and typo
zeffii May 14, 2022
05c82d1
rm whitespace
zeffii May 14, 2022
540c44d
comment
zeffii May 14, 2022
aaaedfc
adds cyclic mode
zeffii May 14, 2022
36fb7aa
does this introduce slight overhead?
zeffii May 15, 2022
5cfcce2
gofaster works!
zeffii May 15, 2022
fc486c4
make decorators_warpspeed
zeffii May 16, 2022
55ac000
add file
zeffii May 16, 2022
306489c
use cleaner names
zeffii May 16, 2022
32bec4e
rename file
zeffii May 16, 2022
39582b1
rm whitespace
zeffii May 16, 2022
6caf21e
had nothing to do with x or y, so renamed var
zeffii May 16, 2022
36c018b
slightly faster
zeffii May 16, 2022
c7fbcd3
adds cyclic version to numba modification
zeffii May 21, 2022
8633152
add decorator parameters
zeffii May 22, 2022
d8ee16d
literal signature evaluation fails
zeffii May 22, 2022
ad12df0
it's not going to work this way
zeffii May 22, 2022
8a00af3
is fastest
zeffii May 22, 2022
a1e516e
clearup comment, print statements
zeffii May 22, 2022
452fa52
show autogenerated signature
zeffii May 22, 2022
1e5ba74
Merge branch 'master' into cubic_numba_proper_compilation
zeffii May 22, 2022
34904af
fast fast fast
zeffii May 23, 2022
60b21ba
Merge branch 'cubic_numba_proper_compilation' of https://github.com/n…
zeffii May 23, 2022
426d842
compiled node name
zeffii May 23, 2022
10e3ca5
remove unusable code
zeffii May 23, 2022
f2065fe
this is the fastest impl
zeffii May 23, 2022
9626343
makes more sense
zeffii Jun 7, 2022
370733b
newline
zeffii Jun 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def app_handler_ops(append=None, remove=None):
"snlite_utils", "snlite_importhelper", "context_managers", "sv_node_utils", "sv_noise_utils",
"profile", "logging", "testing", "sv_requests", "sv_shader_sources", "tree_structure",
"avl_tree", "sv_nodeview_draw_helper", "sv_font_xml_parser", "modules.edge_utils", "modules.polygon_utils",
"wfc_algorithm", "handle_blender_data", "nodes_mixins.generating_objects",
"wfc_algorithm", "handle_blender_data", "nodes_mixins.generating_objects", "decorators_compilation",
"nodes_mixins.show_3d_properties", "modules_inspection", "sv_json_export", "sv_json_import",
"meshes", "tree_walk", "mesh_functions", 'mesh.inset_faces', 'mesh.extrude_edges', "sv_json_struct",
"nodeview_time_graph_drawing", "modules.shader_utils", "dummy_nodes",
Expand Down
31 changes: 31 additions & 0 deletions utils/decorators_compilation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is part of project Sverchok. It's copyrighted by the contributors
# recorded in the version control history of the file, available from
# its original location https://github.com/nortikin/sverchok/commit/master
#
# SPDX-License-Identifier: GPL3
# License-Filename: LICENSE

from sverchok.dependencies import numba


local_numba_storage = {}

# # further reading
# # https://stackoverflow.com/a/54024922/1243487

def njit(**kwargs):
if numba:

def wrapper(function_to_compile):
function_name = function_to_compile.__name__
if function_name not in local_numba_storage:
jitted_func = numba.njit(**kwargs)(function_to_compile)
local_numba_storage[function_name] = jitted_func
return local_numba_storage[function_name]

else:

def wrapper(function_to_compile):
return function_to_compile

return wrapper
92 changes: 54 additions & 38 deletions utils/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
from sverchok.utils.math import np_mixed_product
from sverchok.utils.logging import debug, info

# njit is a light-wrapper aroudn numba.njit, if found
from sverchok.dependencies import numba # not strictly needed i think...
from sverchok.utils.decorators_compilation import njit


identity_matrix = Matrix()

# constants
Expand Down Expand Up @@ -203,14 +208,15 @@ def __init__(self, vertices, tknots = None, metric = None, is_cyclic = False):

super().__init__()


if is_cyclic:

#print(describe_data_shape(vertices))
if len(vertices) == 3:
va, vb, vc = vertices[0], vertices[1], vertices[2]
locs = np.array([vc, va, vb, vc, va, vb, vc, va, vb, vc, va])
else:
locs = np.array(vertices[-4:] + vertices + vertices[:4])
locs = np.concatenate((vertices[-4:], vertices, vertices[:4]), axis=0)

if tknots is None:
if metric is None:
Expand All @@ -233,44 +239,54 @@ def __init__(self, vertices, tknots = None, metric = None, is_cyclic = False):

n = len(locs)
if n < 2:
raise Exception("Cubic spline can't be build from less than 3 vertices")

# a = locs
h = tknots[1:] - tknots[:-1]
h[h == 0] = 1e-8
q = np.zeros((n - 1, 3))
q[1:] = 3 / h[1:, np.newaxis] * (locs[2:] - locs[1:-1]) - 3 / \
h[:-1, np.newaxis] * (locs[1:-1] - locs[:-2])

l = np.zeros((n, 3))
l[0, :] = 1.0
u = np.zeros((n - 1, 3))
z = np.zeros((n, 3))

for i in range(1, n - 1):
l[i] = 2 * (tknots[i + 1] - tknots[i - 1]) - h[i - 1] * u[i - 1]
l[i, l[i] == 0] = 1e-8
u[i] = h[i] / l[i]
z[i] = (q[i] - h[i - 1] * z[i - 1]) / l[i]
l[-1, :] = 1.0
z[-1] = 0.0

b = np.zeros((n - 1, 3))
c = np.zeros((n, 3))

for i in range(n - 2, -1, -1):
c[i] = z[i] - u[i] * c[i + 1]
b = (locs[1:] - locs[:-1]) / h[:, np.newaxis] - h[:, np.newaxis] * (c[1:] + 2 * c[:-1]) / 3
d = (c[1:] - c[:-1]) / (3 * h[:, np.newaxis])

splines = np.zeros((n - 1, 5, 3))
splines[:, 0] = locs[:-1]
splines[:, 1] = b
splines[:, 2] = c[:-1]
splines[:, 3] = d
splines[:, 4] = tknots[:-1, np.newaxis]
raise Exception("Cubic spline can't be built from less than 3 vertices")

@njit(cache=True)
def calc_cubic_splines(tknots, n, locs):
"""
returns splines
"""
h = tknots[1:] - tknots[:-1]
h[h == 0] = 1e-8

delta_i = (locs[2:] - locs[1:-1])
delta_j = (locs[1:-1] - locs[:-2])
nn = (3 / h[1:].reshape((-1, 1)) * delta_i) - (3 / h[:-1].reshape((-1, 1)) * delta_j)
q = np.vstack((np.array([[0.0, 0.0, 0.0]]), nn))
l = np.zeros((n, 3))
l[0, :] = 1.0
u = np.zeros((n - 1, 3))
z = np.zeros((n, 3))

for i in range(1, n - 1):
l[i] = 2 * (tknots[i + 1] - tknots[i - 1]) - h[i - 1] * u[i - 1]
for idx in range(len(l[i])): # range(l[i].shape[0]):
if l[i][idx] == 0:
l[i][idx] = 1e-8
u[i] = h[i] / l[i]
z[i] = (q[i] - h[i - 1] * z[i - 1]) / l[i]

l[-1, :] = 1.0
z[-1] = 0.0

b = np.zeros((n - 1, 3))
c = np.zeros((n, 3))
for i in range(n - 2, -1, -1):
c[i] = z[i] - u[i] * c[i + 1]

h_flat = h.reshape((-1, 1))
b = (locs[1:] - locs[:-1]) / h_flat - h_flat * (c[1:] + 2 * c[:-1]) / 3
d = (c[1:] - c[:-1]) / (3 * h_flat)

splines = np.zeros((n - 1, 5, 3))
splines[:, 0] = locs[:-1]
splines[:, 1] = b
splines[:, 2] = c[:-1]
splines[:, 3] = d
splines[:, 4] = tknots[:-1].reshape((-1, 1))
return splines

self.splines = splines
self.splines = calc_cubic_splines(tknots, n, locs)

def eval(self, t_in, tknots = None):
"""
Expand Down