Actual source code: ex300.c

petsc-3.9.4 2018-09-11
Report Typos and Errors

  2: static char help[] = "Show MatShift BUG happening after copying a matrix with no rows on a process";
  3: /*
  4:    Contributed by: Eric Chamberland
  5: */
  6:  #include <petscmat.h>


  9: /* DEFINE this to turn on/off the bug: */
 10: #define SET_2nd_PROC_TO_HAVE_NO_LOCAL_LINES

 12: int main(int argc,char **args)
 13: {
 14:   Mat               C;
 15:   PetscInt          i,m = 3;
 16:   PetscMPIInt       rank,size;
 17:   PetscErrorCode    ierr;
 18:   PetscScalar       v;
 19:   Mat               lMatA;
 20:   PetscInt          locallines;
 21:   PetscInt          d_nnz[3] = {0,0,0};
 22:   PetscInt          o_nnz[3] = {0,0,0};

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

 28:   if (2 != size) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_INCOMP,"Relevant with 2 processes only");
 29:   MatCreate(PETSC_COMM_WORLD,&C);

 31: #ifdef SET_2nd_PROC_TO_HAVE_NO_LOCAL_LINES
 32:   if (0 == rank) {
 33:     locallines = m;
 34:     d_nnz[0] = 1;
 35:     d_nnz[1] = 1;
 36:     d_nnz[2] = 1;
 37:   } else {
 38:    locallines = 0;
 39:   }
 40: #else
 41:   if (0 == rank) {
 42:     locallines = m-1;
 43:     d_nnz[0] = 1;
 44:     d_nnz[1] = 1;
 45:   } else {
 46:     locallines = 1;
 47:     d_nnz[0] = 1;
 48:   }
 49: #endif

 51:   MatSetSizes(C,locallines,locallines,m,m);
 52:   MatSetFromOptions(C);
 53:   MatXAIJSetPreallocation(C,1,d_nnz,o_nnz,NULL,NULL);

 55:   v = 2;
 56:   /* Assembly on the diagonal: */
 57:   for (i=0; i<m; i++) {
 58:      MatSetValues(C,1,&i,1,&i,&v,ADD_VALUES);
 59:   }
 60:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 61:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 62:   MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
 63:   MatSetOption(C, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
 64:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 65:   MatConvert(C,MATSAME, MAT_INITIAL_MATRIX, &lMatA);
 66:   MatView(lMatA,PETSC_VIEWER_STDOUT_WORLD);

 68:   MatShift(lMatA,-1.0);

 70:   MatDestroy(&lMatA);
 71:   MatDestroy(&C);
 72:   PetscFinalize();
 73:   return ierr;
 74: }


 77: /*TEST

 79:    test:
 80:       nsize: 2

 82: TEST*/