21 #ifndef THUNDEREGG_PETSC_MATSHELLCREATOR_H
22 #define THUNDEREGG_PETSC_MATSHELLCREATOR_H
43 std::shared_ptr<const Operator<D>> op;
48 std::function<Vector<D>()> getNewVector;
56 const std::function<
Vector<D>()>& vector_allocator)
58 , getNewVector(vector_allocator)
68 static int applyMat(Mat A, Vec x, Vec b)
71 MatShellGetContext(A, &msc);
78 VecGetArrayRead(x, &x_view);
81 for (
int c = 0; c < te_x.getNumComponents(); c++) {
83 Loop::Nested<D>(ld.
getStart(), ld.
getEnd(), [&](
const std::array<int, D>& coord) {
84 ld[coord] = x_view[index];
90 VecRestoreArrayRead(x, &x_view);
92 msc->op->apply(te_x, te_b);
95 VecGetArray(b, &b_view);
98 for (
int c = 0; c < te_b.getNumComponents(); c++) {
100 Loop::Nested<D>(ld.
getStart(), ld.
getEnd(), [&](
const std::array<int, D>& coord) {
101 b_view[index] = ld[coord];
106 VecRestoreArray(b, &b_view);
116 static int destroyMat(Mat A)
119 MatShellGetContext(A, &msc);
133 const std::function<
Vector<D>()>& vector_allocator)
139 MatCreateShell(MPI_COMM_WORLD, m, m, PETSC_DETERMINE, PETSC_DETERMINE, msc, &A);
140 MatShellSetOperation(A, MATOP_MULT, (
void (*)(
void))applyMat);
141 MatShellSetOperation(A, MATOP_DESTROY, (
void (*)(
void))destroyMat);