Actual source code: ex213.c


  2: static char help[] = "Tests MatMPIBAIJSetPreallocationCSR()\n\n";

  4: /*
  5:   Include "petscmat.h" so that we can use matrices.  Note that this file
  6:   automatically includes:
  7:      petscsys.h       - base PETSc routines   petscvec.h - vectors
  8:      petscmat.h - matrices
  9:      petscis.h     - index sets
 10:      petscviewer.h - viewers
 11: */
 12: #include <petscmat.h>

 14: int main(int argc,char **args)
 15: {
 16:   Mat            A;
 17:   PetscInt       *ia,*ja, bs = 2;
 18:   PetscInt       N = 9, n;
 19:   PetscInt       rstart, rend, row, col;
 20:   PetscInt       i;
 21:   PetscMPIInt    rank,size;
 22:   Vec            v;

 24:   PetscInitialize(&argc,&args,(char*)0,help);
 25:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 27:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 29:   /* Get a partition range based on the vector size */
 30:   VecCreateMPI(PETSC_COMM_WORLD, PETSC_DECIDE, N, &v);
 31:   VecGetLocalSize(v, &n);
 32:   VecGetOwnershipRange(v, &rstart, &rend);
 33:   VecDestroy(&v);

 35:   PetscMalloc1(n+1,&ia);
 36:   PetscMalloc1(3*n,&ja);

 38:   /* Construct a tri-diagonal CSR indexing */
 39:   i = 1;
 40:   ia[0] = 0;
 41:   for (row = rstart; row < rend; row++)
 42:   {
 43:     ia[i] = ia[i-1];

 45:     /* diagonal */
 46:     col = row;
 47:     {
 48:       ja[ia[i]] = col;
 49:       ia[i]++;
 50:     }

 52:     /* lower diagonal */
 53:     col = row-1;
 54:     if (col >= 0)
 55:     {
 56:       ja[ia[i]] = col;
 57:       ia[i]++;
 58:     }

 60:     /* upper diagonal */
 61:     col = row+1;
 62:     if (col < N)
 63:     {
 64:       ja[ia[i]] = col;
 65:       ia[i]++;
 66:     }
 67:     i++;
 68:   }

 70:   MatCreate(PETSC_COMM_WORLD, &A);
 71:   MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE);
 72:   MatSetType(A,MATMPIAIJ);
 73:   MatMPIAIJSetPreallocationCSR(A, ia, ja, NULL);
 74:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 75:   MatDestroy(&A);

 77:   MatCreate(PETSC_COMM_WORLD, &A);
 78:   MatSetSizes(A, bs*n, bs*n, PETSC_DETERMINE, PETSC_DETERMINE);
 79:   MatSetType(A,MATMPIBAIJ);
 80:   MatMPIBAIJSetPreallocationCSR(A, bs, ia, ja, NULL);
 81:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 82:   MatDestroy(&A);

 84:   PetscFree(ia);
 85:   PetscFree(ja);
 86:   PetscFinalize();
 87:   return 0;
 88: }

 90: /*TEST

 92:     test:
 93:       nsize: 4

 95: TEST*/