Actual source code: ex14.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: static char help[] = "Tests sequential and parallel MatGetRow() and MatRestoreRow().\n";

  4: #include <petscmat.h>

  6: int main(int argc,char **args)
  7: {
  8:   Mat               C;
  9:   PetscInt          i,j,m = 5,n = 5,Ii,J,nz,rstart,rend;
 10:   PetscErrorCode    ierr;
 11:   PetscMPIInt       rank;
 12:   const PetscInt    *idx;
 13:   PetscScalar       v;
 14:   const PetscScalar *values;

 16:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 17:   /* Create the matrix for the five point stencil, YET AGAIN */
 18:   MatCreate(PETSC_COMM_WORLD,&C);
 19:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 20:   MatSetFromOptions(C);
 21:   MatSetUp(C);
 22:   for (i=0; i<m; i++) {
 23:     for (j=0; j<n; j++) {
 24:       v = -1.0;  Ii = j + n*i;
 25:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 26:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 27:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 28:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 29:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 30:     }
 31:   }
 32:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 33:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 34:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 36:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 37:   MatGetOwnershipRange(C,&rstart,&rend);
 38:   for (i=rstart; i<rend; i++) {
 39:     MatGetRow(C,i,&nz,&idx,&values);
 40:     if (!rank) {
 41:       for (j=0; j<nz; j++) {
 42:         PetscPrintf(PETSC_COMM_SELF,"%D %g ",idx[j],(double)PetscRealPart(values[j]));
 43:       }
 44:       PetscPrintf(PETSC_COMM_SELF,"\n");
 45:     }
 46:     MatRestoreRow(C,i,&nz,&idx,&values);
 47:   }

 49:   MatDestroy(&C);
 50:   PetscFinalize();
 51:   return ierr;
 52: }


 55: /*TEST

 57:    test:

 59: TEST*/