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,Ii,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,&C);
 22:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 23:   MatSetFromOptions(C);
 24:   for (i=0; i<m; i++) {
 25:     for (j=0; j<n; j++) {
 26:       v = -1.0;  Ii = j + n*i;
 27:       if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 28:       if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 29:       if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 30:       if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 31:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 32:     }
 33:   }
 34:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 35:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 36:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);

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

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