Actual source code: ex14.c

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

 4:  #include petscmat.h

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

 18:   PetscInitialize(&argc,&args,(char *)0,help);

 20:   /* Create the matrix for the five point stencil, YET AGAIN */
 21:   MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&C);
 22:   MatSetFromOptions(C);
 23:   for (i=0; i<m; i++) {
 24:     for (j=0; j<n; j++) {
 25:       v = -1.0;  I = j + n*i;
 26:       if (i>0)   {J = I - n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 27:       if (i<m-1) {J = I + n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 28:       if (j>0)   {J = I - 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 29:       if (j<n-1) {J = I + 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
 30:       v = 4.0; MatSetValues(C,1,&I,1,&I,&v,INSERT_VALUES);
 31:     }
 32:   }
 33:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 34:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 35:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

 37:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 38:   MatGetOwnershipRange(C,&rstart,&rend);
 39:   for (i=rstart; i<rend; i++){
 40:     MatGetRow(C,i,&nz,&idx,&values);
 41:     if (!rank){
 42: #if defined(PETSC_USE_COMPLEX)
 43:       for (j=0; j<nz; j++) {
 44:         PetscPrintf(PETSC_COMM_SELF,"%D %g ",idx[j],PetscRealPart(values[j]));
 45:       }
 46: #else
 47:       for (j=0; j<nz; j++) {
 48:         PetscPrintf(PETSC_COMM_SELF,"%D %g ",idx[j],values[j]);}
 49: #endif
 50:       PetscPrintf(PETSC_COMM_SELF,"\n");
 51:     }
 52:     MatRestoreRow(C,i,&nz,&idx,&values);
 53:   }

 55:   MatDestroy(C);
 56:   PetscFinalize();
 57:   return 0;
 58: }