Actual source code: ex135.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: static const char help[] = "Test parallel assembly of SBAIJ matrices\n\n";

  3: #include <petscmat.h>

  7: PetscErrorCode Assemble(MPI_Comm comm,PetscInt n,MatType mtype)
  8: {
  9:   Mat            A;
 10:   PetscInt       first,last,i;
 12:   PetscMPIInt    rank,size;

 15:   MatCreate(PETSC_COMM_WORLD,&A);
 16:   MatSetSizes(A, PETSC_DECIDE,PETSC_DECIDE,n,n);
 17:   MatSetType(A,MATMPISBAIJ);
 18:   MatSetFromOptions(A);
 19:   MPI_Comm_size(comm,&size);
 20:   MPI_Comm_rank(comm,&rank);
 21:   if (rank < size-1) {
 22:     MatMPISBAIJSetPreallocation(A,1,1,NULL,1,NULL);
 23:   } else {
 24:     MatMPISBAIJSetPreallocation(A,1,2,NULL,0,NULL);
 25:   }
 26:   MatGetOwnershipRange(A,&first,&last);
 27:   MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
 28:   last--;
 29:   for (i=first; i<=last; i++) {
 30:     MatSetValue(A,i,i,2.,INSERT_VALUES);
 31:     if (i != n-1) {MatSetValue(A,i,n-1,-1.,INSERT_VALUES);}
 32:   }
 33:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 34:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 35:   MatDestroy(&A);
 36:   return(0);
 37: }

 41: int main(int argc,char *argv[])
 42: {
 44:   MPI_Comm       comm;
 45:   PetscInt       n = 6;

 47:   PetscInitialize(&argc,&argv,NULL,help);
 48:   comm = PETSC_COMM_WORLD;
 49:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 50:   Assemble(comm,n,MATMPISBAIJ);
 51:   PetscFinalize();
 52:   return 0;
 53: }