-
Notifications
You must be signed in to change notification settings - Fork 4
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
Increase maximum tree depth #13
Comments
Just check the VTK CellTreeLocator which uses a C++ std::stack, which I guess is heap allocated. Would be worthwhile to compare with a numba list, and do some benchmarking. The stack allocated stack might be a premature optimization on my part. Generating a largish mesh, 2.6 million triangles: import geopandas as gpd
import numpy as np
import shapely.geometry as sg
import pandamesh as pm
outer_coords = np.array([(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)]) * 1e4
inner_coords = np.array([(3.0, 3.0), (7.0, 3.0), (7.0, 7.0), (3.0, 7.0)]) * 1e4
line_coords = np.array([(2.0, 8.0), (8.0, 2.0)]) * 1e4
inner = sg.LinearRing(inner_coords)
outer = sg.LinearRing(outer_coords)
line = sg.LineString(line_coords)
donut = sg.Polygon(outer, holes=[inner])
gdf = gpd.GeoDataFrame(data={"cellsize": [100.0]}, geometry=[donut])
mesher = pm.TriangleMesher(gdf)
vertices, triangles = mesher.generate()
vertices.astype(np.float64).tofile("vertices.bin")
triangles.astype(np.int64).tofile("triangles.bin") Benchmarking: import numpy as np
import numba_celltree
# %%
triangles = np.fromfile("c:/src/pandamesh/triangles.bin", dtype=np.int64).reshape((-1, 3))
vertices = np.fromfile("c:/src/pandamesh/vertices.bin", dtype=np.float64).reshape((-1, 2))
# %%
tree = numba_celltree.CellTree2d(vertices, triangles, -1)
# %%
xmin = vertices[:, 0].min()
ymin = vertices[:, 1].min()
xmax = vertices[:, 0].max()
ymax = vertices[:, 1].max()
points = np.random.rand(int(1e6), 2)
points[:, 0] = points[:, 0] * (xmax - xmin) + xmin
points[:, 1] = points[:, 1] * (ymax - ymin) + ymin
# %%
%timeit result = tree.locate_points(points) Static stack-memory stack, size 32:
Static stack, size 128 gives basically the same result. A list based stack (which allows us to just append and pop), is at least twice slower:
No clue why the timings are so inconsistent. A ordinary heap-memory stack, size 32:
Heap memory, size 128:
It's still a pretty tiny allocation all things together; it does allocate once per query, since the queries are parallellized and we don't want to share pre-allocated memory. I'll keep the stack-memory, and bump up the stack size for now. |
Alternatively, we could implement bounds-checking. Ideally, I guess it reallocates a larger stack. If someone runs into an issue in the future, they should enable bounds checking for numba. A resulting index error is then almost certainly due to the fixed stack size. |
The current max depth is set to 32. I took this smallish value, but currently working on some smalll, but somewhat perverse data (an earcut triangulation from polygons), it creates out-of-bounds errors. Setting the depth to 128 fixes it.
The text was updated successfully, but these errors were encountered: