Python PETSc Example BP1#1806
Conversation
| 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() |
There was a problem hiding this comment.
You'll want a MatShell, not a MatAIJ
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Thank you for all the suggestions! Definitely, keeping the data in place would mean less overhead and better efficiency
|
@tylercollins5737 , thank you for your work. Are you able to address the reviewer's comments? |
|
@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. |
| with self.v.array_read() as va: | ||
| y.setValues(range(lo, hi), va[lo:hi]) | ||
| y.assemble() |
There was a problem hiding this comment.
Ideally, you'd set v to use y's array before you apply the libCEED operator
There was a problem hiding this comment.
That makes more sense, thank you!
| 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) |
There was a problem hiding this comment.
If this just wraps another function call, I think its clearer to use the function call itself
There was a problem hiding this comment.
Totally agree, I have another CreateRestriction function that reflects C but its a WIP
|
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! |
|
Yeah, since there are QFunctions that are not available via the gallery, you'd have to use the technique shown in this folder for |
|
@jeremylt Cool, yeah that should do the trick. |
|
Hi @tylercollins5737, I'm hoping to have this ready in case next semester's students want to build on this for BP 2-6. This is good work so far and will be very helpful to future users. Is there anything I can do to help here? I'm happy to help out on any pieces that have you stuck so we can get this moving again. |
Cool, thank you for the support. I have the development environment setup again with everything updated, and I'm making progress on the example! I will reach out if I get stumped. |
|
@tylercollins5737 are you planning on coming back to this? I think there's a lot of good work here! If not, it may be time to close this PR. |
@zatkins-dev Yes! I should have have a pretty complete update soon, ideally by the end of the weekend. |
|
Awesome! Happy to keep it open then, thanks! |
The gallery has Mass3DBuild and MassApply, but nothing like SetupMassRhs or Error, which depend on the exact solution the benchmark solves for. These follow the ex1-ex3 pattern: C sources next to the Python examples, built into the QFunctions library by qfunctions.c.
Port examples/petsc/bpsraw.c to Python with petsc4py and the libCEED Python bindings, solving BP1. A python PETSc Mat takes the place of the MatShell; its mult() runs the libCEED operator. MatMult hands the PETSc Vec arrays straight to libCEED with CEED_USE_POINTER instead of copying them, on the host and on the device. petsc4py has no VecGetArrayAndMemType, so on the device the pointer comes from getCUDAHandle and is passed through the CUDA array interface.
The example picks its iteration limit from how long a first solve takes, so the tests pin it with ksp_max_it_clip to keep the iteration count the same every run, as the C test does.
c387450 to
f833966
Compare
Two notes:
|
| import ex1_volume | ||
| import ex2_surface | ||
| import ex3_volume | ||
| import bpsraw |
There was a problem hiding this comment.
since this file relies upon PETSc4Py, we'll want to test it separately from ex1-3, which only need libCEED
| # Apply libCEED operator | ||
| self.op_apply.apply(self.x_ceed, self.y_ceed) | ||
|
|
||
| # Restore arrays; the C also detaches them with CeedVectorTakeArray, which the Python bindings do not |
There was a problem hiding this comment.
We really should just add CeedVectorTakeArarry to the Python bindings - @zatkins-dev or I can hit that, though dunno how long it'll take to publish that
| if isinstance(value, int)} | ||
|
|
||
|
|
||
| def Split3(size, reverse=False): |
There was a problem hiding this comment.
we should use the same function naming, style throughout. Also a brief docstring for each would be handy (though BPsRaw probably omitted that? If so, then no worries here)
|
I have just a handful of comments, but this looks great! |
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: