Skip to content

Commit

Permalink
Snlite templates (#4548)
Browse files Browse the repository at this point in the history
* add templates, numba uncache

* make it non craching

* fix unreported comment-bug in required socket mode

* correct comment

* parse only if socketline is correct
  • Loading branch information
zeffii authored Jun 26, 2022
1 parent a7eeda7 commit bd41a58
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
28 changes: 28 additions & 0 deletions node_scripts/SNLite_templates/templates/numba_VF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
>in verts v #
>in faces s # no computation until both input sockets are connected
out _verts v
out _faces s
"""

from sverchok.utils.decorators_compilation import njit, numba_uncache

# if other numba features are needed
# from sverchok.dependencies import numba

# there are two ways to clear sverchok's njit cache
# 1. numba_uncache(your_function_name)
# 2. rename the function, into something that does not yet exist in the cache

# this will compile, the first time it's run, then subsequent
# calls to your_function are from the cached compilation.

#@njit(cache=True)
def your_function(vlist, flist):
return nvlist, nflist


for vlist, flist in zip(verts, faces):
v, f = your_function(vlist, flist)
_verts.append(v)
_faces.append(f)
2 changes: 1 addition & 1 deletion nodes/script/script1_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

defaults = [0] * 32

template_categories = ['demo', 'bpy_stuff', 'bmesh', 'utils']
template_categories = ['demo', 'bpy_stuff', 'bmesh', 'utils', 'templates']



Expand Down
9 changes: 9 additions & 0 deletions utils/decorators_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def wrapper(function_to_compile):
if function_name not in local_numba_storage:
jitted_func = numba.njit(**kwargs)(function_to_compile)
local_numba_storage[function_name] = jitted_func

#elif function_name in local_numba_storage and function_str_hash doesn't match:
# # recache
# the dowside to this would be that it becomes whitespace/comment changes sensitive
# unless whitespace and comments are removed from functionstring before compilation..

return local_numba_storage[function_name]

else:
Expand All @@ -47,3 +53,6 @@ def wrapper(function_to_compile):
return function_to_compile

return wrapper

def numba_uncache(function_name):
del local_numba_storage[function_name]
16 changes: 14 additions & 2 deletions utils/snlite_importhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,31 @@ def parse_socket_line(node, line):
nested = processed(lsp[4])
return socket_type, socket_name, default, nested

def trim_comment(line):
idx = line.find("#")
if idx < 0:
return line
return line[:idx]

def parse_required_socket_line(node, line):
# receives a line like
# required input sockets do not accept defaults or nested info, what would be the point?
# receives a line like
# >in socketname sockettype

line = trim_comment(line)

lsp = line.strip().split()
if len(lsp) > 3:
lsp = lsp[:3]

if len(lsp) == 3:
socket_type = sock_dict.get(lsp[2])
socket_name = lsp[1]
if not socket_type:
return UNPARSABLE
return socket_type, socket_name, None, None

node.error(f'directive: (socket line) "{line}" -> is malformed, missing socket type?')
node.error(f'directive: (socket line) "{line}" -> is malformed, missing socket type? {lsp}')
return UNPARSABLE


Expand Down
1 change: 1 addition & 0 deletions utils/snlite_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def ddir(content, filter_str=None):
vals = [n for n in dir(content) if not n.startswith('__') and filter_str in n]
return vals

# deprecated, use decorators_compilation.njit instead

def sv_njit(function_to_njit, parameters):
fn_name = function_to_njit.__name__
Expand Down

0 comments on commit bd41a58

Please sign in to comment.