Actual source code: ex14.c


  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:   PetscMPIInt       rank;
 11:   const PetscInt    *idx;
 12:   PetscScalar       v;
 13:   const PetscScalar *values;

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

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

 48:   MatDestroy(&C);
 49:   PetscFinalize();
 50:   return 0;
 51: }

 53: /*TEST

 55:    test:

 57: TEST*/