Skip to content

rtree bulk query - #5247

Open
leo-collins wants to merge 22 commits into
mainfrom
leo/bulk-query
Open

rtree bulk query#5247
leo-collins wants to merge 22 commits into
mainfrom
leo/bulk-query

Conversation

@leo-collins

@leo-collins leo-collins commented Jul 14, 2026

Copy link
Copy Markdown
Contributor
  • Use new bulk query routine to query the rtree. We now do 3 FFI calls instead of (potentially) 100,000s.
  • firedrake-rtree IDs now use int64 instead of size_t.
  • Refactored locate.c to reduce code duplication. We now also check the cells_ignore array before computing the reference coordinates.
  • Other various tidying, e.g. using bools, tidying headers, checking bounds when casting to PetscInt

Requires a new release of firedrake-rtree.

Comment thread firedrake/cython/rtree.pyx

@connorjward connorjward left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems pretty much fine

Comment thread firedrake/function.py
@@ -651,6 +651,8 @@ def single_eval(x, buf):
buf.ctypes.data_as(c_void_p))
if err == -1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still right? Don't we want to check cell_out?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the old Function.at code which calls a different C function defined in pointeval_utils. I suppose the rtree error path shouldn't return -1 which is what the "cell not found" error path returns.

Comment thread firedrake/locate.c
*cell_out = -1;
for (size_t i = 0; i < nids; ++i) {
if (ids[i] > (int64_t)PETSC_MAX_INT) {
return PETSC_ERR_ARG_OUTOFRANGE;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment explaining why this might happen. It isn't obvious to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if this is some sort of indicator that things aren't found wouldn't -1 be a better choice?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just checking that casting the ids from int64 to PetscInt is safe (for 32 bit petsc builds). Since the ids are mesh cell ids it should always be safe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Definitely needs a comment but makes sense that it's there.

Comment thread firedrake/mesh.py
return cells, Xs, ref_cell_dists_l1

@PETSc.Log.EventDecorator()
def _c_locator(self, tolerance=None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly out of scope but could you add a docstring/comments? This is a really complicated piece of code.

Comment thread firedrake/mesh.py
Comment on lines +2788 to +2792
if (locate_err != PETSC_SUCCESS) {{
rtree_free_ids(candidate_ids, candidate_offsets[npoints]);
rtree_free_offsets(candidate_offsets, npoints + 1);
return locate_err;
}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More natural to

Suggested change
if (locate_err != PETSC_SUCCESS) {{
rtree_free_ids(candidate_ids, candidate_offsets[npoints]);
rtree_free_offsets(candidate_offsets, npoints + 1);
return locate_err;
}}
if (locate_err != PETSC_SUCCESS) {{
break;
}}

?

Generally in C I think it is bad style to exit early because cleanup is hard

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just break then this function will return 0 and silently drop the error? The block as it is has all the cleanup we need to do in it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean to change the return 0 at the end of the function to return locate_err. This isn't critical, but I think it's tidier/better practice.

@connorjward

Copy link
Copy Markdown
Contributor

I assume this is no longer a draft?

@leo-collins
leo-collins marked this pull request as ready for review July 29, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:int64 Do a 64 bit integer build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants