Skip to content

Conversation

@majosm
Copy link
Collaborator

@majosm majosm commented Apr 17, 2025

Reimplements generate_box_mesh using numpy, and rearranges some of the boundary processing code to make it faster.

Before (shock1d):
Screenshot 2025-04-17 at 12 51 27 PM

After:
Screenshot 2025-04-17 at 12 51 50 PM

Fixes #456.

To-do:

  • Test against mirgecom examples/tests after find_point_permutation refactor
  • Re-do shock1d profile after find_point_permutation refactor

@majosm majosm force-pushed the optimize-box-mesh-gen branch from a1e9116 to 15f51ca Compare April 17, 2025 20:27
@majosm majosm marked this pull request as ready for review April 18, 2025 00:52
@majosm majosm marked this pull request as draft April 28, 2025 02:11
@majosm majosm force-pushed the optimize-box-mesh-gen branch 2 times, most recently from ee58bb5 to 5bfbe52 Compare April 30, 2025 21:02
@majosm
Copy link
Collaborator Author

majosm commented May 2, 2025

@MTCam was still seeing some slowness after the box mesh changes, which turned out to be due to inefficiencies in the periodic boundary processing (specifically, the vertex matching). My measurements above are for a nonperiodic mesh, so the issue isn't present there. I modified the vertex matching code to use a hybrid of the two existing algorithms; it does spatial partitioning until the number of vertices to compare is sufficiently small, then it switches to a direct pairwise distance calculation.

Before:
Screenshot 2025-05-02 at 10 48 36 AM

After:
Screenshot 2025-05-02 at 10 48 47 AM

@majosm
Copy link
Collaborator Author

majosm commented May 2, 2025

Also, I just remembered today that #352 exists, which would eliminate the last remaining non-numpy loop here. I think I'll try to resurrect that in a separate PR rather than dragging this one out any further.

@majosm majosm marked this pull request as ready for review May 2, 2025 15:57
@majosm majosm requested review from alexfikl and inducer May 2, 2025 15:57
Copy link
Collaborator

@alexfikl alexfikl left a comment

Choose a reason for hiding this comment

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

Took a look over the mesh/generation.py changes and it mostly looks good to me! I left some "thinking out loud" comments to maybe reduce some memory allocations, but otherwise just small nitpicks.

Very impressive performance improvements though! 😁

Comment on lines 1055 to 1061
# Compute an approximate median vertex coordinate
hist, bin_edges = np.histogram(
both_vertices[idim_largest, :],
both_vertices.shape[1])
nvertices_up_to_bin = np.cumsum(hist)
median_bin = np.where(nvertices_up_to_bin > both_vertices.shape[1]/2)[0][0]
median_coord = bin_edges[median_bin]
Copy link
Owner

Choose a reason for hiding this comment

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

Can we just use np.median?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, turns out np.median performs about the same as the np.histogram approximation. I switched it over.

@majosm majosm requested a review from inducer May 6, 2025 13:57

# {{{ vertex matching

def _rec_match(
Copy link
Owner

Choose a reason for hiding this comment

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

There is meshmode.mesh.find_point_permutation, which does the same thing, minus all the algorithmic bells and whistles. I feel that this could just become that routine. The interface is a bit simpler, but I think that's probably a plus. I think the "indices" handling could probably be done separately.

Coincidentally, find_point_permutation is also untested, so we should fix that.

@majosm majosm force-pushed the optimize-box-mesh-gen branch 2 times, most recently from 7f01e4e to b16df22 Compare May 9, 2025 23:22
@inducer
Copy link
Owner

inducer commented May 13, 2025

Maybe squash into two commits?

majosm added 2 commits May 13, 2025 10:10
* optimize generate_box_mesh

* optimize boundary processing loop

* fix type annotation

* parameterize dtypes

* simplify definition of a0/a1 for 1D

* use np.column_stack() instead of np.array([...]).T

* avoid potentially creating temporary arrays in vertex assignment

* *_crit -> *_face
…ations (inducer#458, part 2)

* optimize vertex matching in periodicity processing

* use np.median instead of approximating with np.histogram

    turns out to be just as fast

* combine _match_vertices and mesh.tools.find_point_permutation into find_point_to_point_mapping

* add tests for find_point_to_point_mapping

* use find_point_to_point_mapping instead of find_point_permutation
@majosm majosm force-pushed the optimize-box-mesh-gen branch from b16df22 to 9f9016f Compare May 13, 2025 15:21
@majosm
Copy link
Collaborator Author

majosm commented May 13, 2025

Rebased and mirgecom CI passed, should be good to go @inducer.

@inducer inducer merged commit 80627bf into inducer:main May 13, 2025
13 checks passed
inducer pushed a commit that referenced this pull request May 13, 2025
* optimize generate_box_mesh

* optimize boundary processing loop

* fix type annotation

* parameterize dtypes

* simplify definition of a0/a1 for 1D

* use np.column_stack() instead of np.array([...]).T

* avoid potentially creating temporary arrays in vertex assignment

* *_crit -> *_face
@inducer
Copy link
Owner

inducer commented May 13, 2025

Awesome, thanks! In it goes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Boxy mesh generation is slow

3 participants