Skip to content

Cache VertexOnlyMesh for cross-mesh interpolation#4860

Draft
leo-collins wants to merge 3 commits intomainfrom
leo/cache-vom-crossmesh
Draft

Cache VertexOnlyMesh for cross-mesh interpolation#4860
leo-collins wants to merge 3 commits intomainfrom
leo/cache-vom-crossmesh

Conversation

@leo-collins
Copy link
Contributor

Cache the vom used for cross-mesh interpolation. Consider the following

V = FunctionSpace(mesh1, "CG", 1)
W = FunctionSpace(mesh2, "CG", 1)

f1 = Function(V)
f2 = Function(V)

h1 = assemble(interpolate(f1, W))  # ~30s at 500,000 dofs
h2 = assemble(interpolate(f2, W))

The assembly of h2 previously took ~5s on my machine, with caching it now takes ~0.012s

Copy link
Contributor

@connorjward connorjward left a comment

Choose a reason for hiding this comment

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

Sorry for the negativity, but caching correctly is really hard.

"""Return the symbolic ``Interpolate`` expressions for point evaluation and
re-ordering into the input-ordering VertexOnlyMesh.
@staticmethod
def _vom_cache_key(target_space, source_mesh, allow_missing_dofs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this just be a cached property?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

self.allow_missing_dofs,
)
try:
return _vom_cache[key]
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really like this. The communicator is only implicitly part of this cache key so I think deadlocks could well happen.
Also you are using source_mesh as the key, creating a reference to it and meaning that it will never be cleared from the memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the correct way to do this then?

Copy link
Contributor

Choose a reason for hiding this comment

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

Step 1 is choosing an object to tie the lifetime of the VoM to. It's probably best to use source_mesh here.

I have implemented a fair amount of caching support that would easily handle this in pyop3, so you may want to wait for that or just port the (small) amount of code over that you need.

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.

2 participants