Actual source code: ex208.c

  1: static char help[] = "Test MatCreateRedundantMatrix for rectangular matrix.\n\
  2:                       Contributed by Jose E. Roman, July 2017\n\n";

  4: #include <petscmat.h>
  5: int main(int argc,char **args)
  6: {
  7:   Mat               A,B;
  8:   PetscInt          m=3,n=4,i,nsubcomm;
  9:   PetscMPIInt       size,rank;

 11:   PetscInitialize(&argc,&args,(char*)0,help);
 12:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 13:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);

 15:   nsubcomm = size;
 16:   PetscOptionsGetInt(NULL,NULL,"-nsubcomm",&nsubcomm,NULL);

 18:   MatCreate(PETSC_COMM_WORLD, &A);
 19:   MatSetSizes(A, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
 20:   MatSetType(A, MATAIJ);
 21:   MatSetFromOptions(A);
 22:   MatSetUp(A);

 24:   if (rank == 0) {
 25:     for (i=0;i<size*PetscMin(m,n);i++) {
 26:       MatSetValue(A, i, i, 1.0, INSERT_VALUES);
 27:     }
 28:   }
 29:   MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
 30:   MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
 31:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

 33:   MatCreateRedundantMatrix(A, nsubcomm, MPI_COMM_NULL, MAT_INITIAL_MATRIX, &B);
 34:   if (nsubcomm==size) { /* B is a sequential matrix */
 35:     if (rank == 0) {
 36:       MatView(B,PETSC_VIEWER_STDOUT_SELF);
 37:     }
 38:   } else {
 39:     MPI_Comm comm;
 40:     PetscObjectGetComm((PetscObject)B,&comm);
 41:     MatView(B,PETSC_VIEWER_STDOUT_(comm));
 42:   }

 44:   MatDestroy(&A);
 45:   MatDestroy(&B);
 46:   PetscFinalize();
 47:   return 0;
 48: }

 50: /*TEST

 52:    test:

 54:    test:
 55:       suffix: 2
 56:       nsize: 3

 58:    test:
 59:       suffix: baij
 60:       args: -mat_type baij

 62:    test:
 63:       suffix: baij_2
 64:       nsize: 3
 65:       args: -mat_type baij

 67:    test:
 68:       suffix: dense
 69:       args: -mat_type dense

 71:    test:
 72:       suffix: dense_2
 73:       nsize: 3
 74:       args: -mat_type dense

 76: TEST*/