Skip to content

Conversation

@tylercollins5737
Copy link

@tylercollins5737 tylercollins5737 commented Apr 19, 2025

Hello!

This is a Python implementation of bpsraw.c for BP1 and was initially discussed in #1774 . I'm aiming to recreate the benchmark behavior using libceed and petsc4py. I appreciate any feedback or indications I'm headed in the right direction!

It does run, but this is a work in progress. Remaining tasks:

  • fix the matrix operations to work like the MatShell C implementation
  • get PETSc Vec scatter working
  • QFunction
  • Error function
  • add meaningful comments
  • improve the printed output metrics
  • create a test

Comment on lines 102 to 115
matrix = PETSc.Mat().createAIJ([num_dofs, num_dofs], comm=PETSc.COMM_WORLD)
matrix.setFromOptions()
matrix.setUp()
for i in range(num_dofs):
e_vec = ceed.Vector(num_dofs)
r_vec = ceed.Vector(num_dofs)
e_vec.set_value(0.0)
r_vec.set_value(0.0)
with e_vec.array_write() as arr:
arr[i] = 1.0
op.apply(e_vec, r_vec)
with r_vec.array_read() as r_arr:
matrix.setValues(range(num_dofs), [i], r_arr)
matrix.assemble()
Copy link
Member

Choose a reason for hiding this comment

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

You'll want a MatShell, not a MatAIJ

Copy link
Author

Choose a reason for hiding this comment

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

Apologies for leaving this a bit stale. Addressing this led to a lot of other changes. I had trouble setting the matrix type to "shell" and applying context, kept getting segmentation faults. So, instead of using the "shell" type, I used the "python" type and set the context through a python class. Pulled that implementation from the petsc4py 2DPoisson example (about half way down) --> link. I'll tidy up what I have a submit an update soon.

Copy link
Member

Choose a reason for hiding this comment

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

Overall this is looking like the right approach. The big thing is to try to avoid copying around data during frequent operations, like the matrix application

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for all the suggestions! Definitely, keeping the data in place would mean less overhead and better efficiency

@valeriabarra
Copy link
Contributor

@tylercollins5737 , thank you for your work. Are you able to address the reviewer's comments?

@tylercollins5737
Copy link
Author

@valeriabarra I was able to get it working using the "python" matrix type, MatAIJ was not the best. I believe the "python" type and MatCreateShell can achieve the same thing.

Comment on lines +93 to +95
with self.v.array_read() as va:
y.setValues(range(lo, hi), va[lo:hi])
y.assemble()
Copy link
Member

Choose a reason for hiding this comment

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

Ideally, you'd set v to use y's array before you apply the libCEED operator

Copy link
Author

Choose a reason for hiding this comment

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

That makes more sense, thank you!

Comment on lines +39 to +40
def CreateRestriction(ceed, mesh_elem, P, num_comp, ndofs, offsets):
return ceed.ElemRestriction(mesh_elem, P, num_comp, 1, ndofs, offsets, cmode=libceed.USE_POINTER)
Copy link
Member

Choose a reason for hiding this comment

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

If this just wraps another function call, I think its clearer to use the function call itself

Copy link
Author

Choose a reason for hiding this comment

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

Totally agree, I have another CreateRestriction function that reflects C but its a WIP

Co-authored-by: Jeremy L Thompson <[email protected]>
@valeriabarra
Copy link
Contributor

Hey @tylercollins5737 , how are you? I was wondering if you intended to complete this PR or if there were any impeding issues that you encountered.

There are a couple of other potential contributors that are eyeing this project and I was wondering if there was a missing interface for which this BP could not be completed or if it was just for personal reasons. Thank you!

@tylercollins5737
Copy link
Author

tylercollins5737 commented Oct 9, 2025

Hey @tylercollins5737 , how are you? I was wondering if you intended to complete this PR or if there were any impeding issues that you encountered.

There are a couple of other potential contributors that are eyeing this project and I was wondering if there was a missing interface for which this BP could not be completed or if it was just for personal reasons. Thank you!

Hey @valeriabarra hope you are doing well! In short, progress didn't stop because of any major limitations in the libceed or petsc libraries. But the qfunction for building the RHS "SetupMassRHS" and "Error" I believe aren't available so a workaround would have to be implemented which could be very slow, or an addition to the gallery of qfunctions, or someway to use the qfunctions used by the BPs, or a way to define a custom one in python. If there are interested contributors they're more than welcome to work on it. If no one picks it up, I'll start on it again and get the development environment setup on a computer I actually own. Cheers!

@jeremylt
Copy link
Member

Yeah, since there are QFunctions that are not available via the gallery, you'd have to use the technique shown in this folder for ex3_volume.py (and associated files) to use a QFunction written in C.

@tylercollins5737
Copy link
Author

@jeremylt Cool, yeah that should do the trick.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants