Actual source code: ex52.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  2: static char help[] = "Tests various routines in MatMPIBAIJ format.\n";


  5:  #include <petscmat.h>

  7: int main(int argc,char **args)
  8: {
  9:   Mat            A;
 10:   PetscInt       m=2,bs=1,M,row,col,start,end,i,j,k;
 12:   PetscMPIInt    rank,size;
 13:   PetscScalar    data=100;
 14:   PetscBool      flg;

 16:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 17:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 18:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 20:   /* Test MatSetValues() and MatGetValues() */
 21:   PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);
 22:   PetscOptionsGetInt(NULL,NULL,"-mat_size",&m,NULL);

 24:   M    = m*bs*size;
 25:   MatCreateBAIJ(PETSC_COMM_WORLD,bs,PETSC_DECIDE,PETSC_DECIDE,M,M,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL,&A);

 27:   MatGetOwnershipRange(A,&start,&end);
 28:   PetscOptionsHasName(NULL,NULL,"-column_oriented",&flg);
 29:   if (flg) {
 30:     MatSetOption(A,MAT_ROW_ORIENTED,PETSC_FALSE);
 31:   }

 33:   /* inproc assembly */
 34:   for (row=start; row<end; row++) {
 35:     for (col=start; col<end; col++,data+=1) {
 36:       MatSetValues(A,1,&row,1,&col,&data,INSERT_VALUES);
 37:     }
 38:   }
 39:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 40:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 42:   /* offproc assembly */
 43:   data = 5.0;
 44:   row  = (M+start-1)%M;
 45:   for (col=0; col<M; col++) {
 46:     MatSetValues(A,1,&row,1,&col,&data,ADD_VALUES);
 47:   }
 48:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 49:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 51:   /* Test MatSetValuesBlocked() */
 52:   PetscOptionsHasName(NULL,NULL,"-test_setvaluesblocked",&flg);
 53:   if (flg) {
 54:     PetscScalar *bval;
 55:     row /= bs;
 56:     col  = start/bs;
 57:     PetscMalloc1(bs*bs,&bval);
 58:     k = 1;
 59:     /* row oriented - defalt */
 60:     for (i=0; i<bs; i++) {
 61:       for (j=0; j<bs; j++) {
 62:         bval[i*bs+j] = (PetscScalar)k; k++;
 63:       }
 64:     }
 65:     MatSetValuesBlocked(A,1,&row,1,&col,bval,INSERT_VALUES);
 66:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 67:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 68:     PetscFree(bval);
 69:   }

 71:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 72:   MatDestroy(&A);
 73:   PetscFinalize();
 74:   return ierr;
 75: }



 79: /*TEST

 81:    test:
 82:       suffix: 1
 83:       nsize: 3
 84:       args: -mat_block_size 2 -test_setvaluesblocked

 86:    test:
 87:       suffix: 2
 88:       nsize: 3
 89:       args: -mat_block_size 2 -test_setvaluesblocked -column_oriented

 91:    test:
 92:       suffix: 3
 93:       nsize: 3
 94:       args: -mat_block_size 1 -test_setvaluesblocked

 96:    test:
 97:       suffix: 4
 98:       nsize: 3
 99:       args: -mat_block_size 1 -test_setvaluesblocked -column_oriented

101: TEST*/