Hello, PETSc developers, I have a question regarding the usage of MatGetRow and
MatSetValues.
As shown in the piece of code below. I have a matrix A with n rows and N_Steps
columns, for each time step I_Steps, I want to set A[x, I_Steps+1] = A[x,
I_Steps] + someComputedValue;
As I understand, in order to read A[x, I_Steps], I have to get the x's rows
values in A by MatGetRow; In order to set the A[x, I_Steps+1], I have to use
the MatSetValues. And these operations require the MatAssemblyBegin and
MatAssemblyEnd as well.
The problem is when I run the code, it only works when the number of processors
equal to 1 or n. Otherwise, it's hanging there forever.
I feel like the MatAssemblyBegin and MatAssemblyEnd may be the source of the
problem.
How to fix the error? Or is there a better way to implement this?
Thanks,
Shuangshuang
Mat A;
ierr = MatGetOwnershipRange(A, &lo, &hi); CHKERRQ(ierr);
const PetscScalar *AVals;
for (I_Steps = 0; I_Steps < N_Steps; I_Steps++) {
for (k = lo; k < hi; k++) {
ierr = MatGetRow(A, k, &ncols, &cols, &AVals); CHKERRQ(ierr);
val[0] = AVals[cols[I_Steps]] + someComputedValue;
ierr = MatRestoreRow(A, k, &ncols, &cols, &AVals); CHKERRQ(ierr);
j = I_Steps+1;
ierr = MatSetValues(A, 1, &k, 1, &j, val, INSERT_VALUES); CHKERRQ(ierr);
ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
MatView(A, PETSC_VIEWER_STDOUT_WORLD);
}
}