#include #include "petsc.h" int main(int argc, char** argv) { PetscCall(PetscInitialize(&argc, &argv, nullptr, nullptr)); PetscInt m = 2; PetscInt n = 2; PetscInt i[] = {0, 2, 4}; PetscInt j[] = {1, 0, 0, 1}; PetscScalar v[] = {2, 1, 3, 4}; Mat mat; PetscCall(MatCreateMPIAIJWithArrays(PETSC_COMM_SELF, m, n, m, n, i, j, v, &mat)); std::cout << "=== Before update ===" << std::endl; for (PetscInt row = 0; row < m; ++row) { for (PetscInt col = 0; col < n; ++col) { PetscScalar x; PetscCall(MatGetValue(mat, row, col, &x)); std::cout << ' ' << x; } std::cout << std::endl; } PetscCall(MatUpdateMPIAIJWithArray(mat, v)); std::cout << "=== After update ===" << std::endl; for (PetscInt row = 0; row < m; ++row) { for (PetscInt col = 0; col < n; ++col) { PetscScalar x; PetscCall(MatGetValue(mat, row, col, &x)); std::cout << ' ' << x; } std::cout << std::endl; } PetscCall(MatDestroy(&mat)); PetscCall(PetscFinalize()); }