Actual source code: ex300.c

petsc-3.7.3 2016-08-01
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>


 11: /* DEFINE this to turn on/off the bug: */
 12: #define SET_2nd_PROC_TO_HAVE_NO_LOCAL_LINES

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

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

 30:   if (2 != size) {
 31:      printf("**** Relevant with 2 processes only.*****\n");
 32:      PetscFinalize();
 33:      return 1;
 34:   }
 35:   MatCreate(PETSC_COMM_WORLD,&C);

 37: #ifdef SET_2nd_PROC_TO_HAVE_NO_LOCAL_LINES
 38:   if (0 == rank) {
 39:     locallines = m;
 40:     d_nnz[0] = 1;
 41:     d_nnz[1] = 1;
 42:     d_nnz[2] = 1;
 43:   } else {
 44:    locallines = 0;
 45:   }
 46: #else
 47:   if (0 == rank) {
 48:     locallines = m-1;
 49:     d_nnz[0] = 1;
 50:     d_nnz[1] = 1;
 51:   } else {
 52:     locallines = 1;
 53:     d_nnz[0] = 1;
 54:   }
 55: #endif

 57:   MatSetSizes(C,locallines,locallines,m,m);
 58:   MatSetFromOptions(C);
 59:   MatXAIJSetPreallocation(C,1,d_nnz,o_nnz,NULL,NULL);

 61:   v = 2;
 62:   /* Assembly on the diagonal: */
 63:   for (i=0; i<m; i++) {
 64:      MatSetValues(C,1,&i,1,&i,&v,ADD_VALUES);
 65:   }
 66:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 67:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 68:   MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
 69:   MatSetOption(C, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
 70:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 71:   MatConvert(C,MATSAME, MAT_INITIAL_MATRIX, &lMatA);
 72:   MatView(lMatA,PETSC_VIEWER_STDOUT_WORLD);

 74:   v = -1.0;
 75:   MatShift(lMatA,v);

 77:   MatDestroy(&lMatA);
 78:   MatDestroy(&C);
 79:   PetscFinalize();
 80:   return 0;
 81: }