Actual source code: ex24.c


  2: static char help[] = "Tests the different MatColoring implementations and ISColoringTestValid() \n\
  3:                       Modified from the code contributed by Ali Berk Kahraman. \n\n";
  4: #include <petscmat.h>

  6: PetscErrorCode FormJacobian(Mat A)
  7: {
  8:   PetscInt    M, ownbegin, ownend, i, j;
  9:   PetscScalar dummy = 0.0;

 11:   PetscFunctionBeginUser;
 12:   PetscCall(MatGetSize(A, &M, NULL));
 13:   PetscCall(MatGetOwnershipRange(A, &ownbegin, &ownend));

 15:   for (i = ownbegin; i < ownend; i++) {
 16:     for (j = i - 3; j < i + 3; j++) {
 17:       if (j >= 0 && j < M) PetscCall(MatSetValues(A, 1, &i, 1, &j, &dummy, INSERT_VALUES));
 18:     }
 19:   }
 20:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 21:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
 22:   PetscFunctionReturn(PETSC_SUCCESS);
 23: }

 25: int main(int argc, char *argv[])
 26: {
 27:   Mat         J;
 28:   PetscMPIInt size;
 29:   PetscInt    M = 8;
 30:   ISColoring  iscoloring;
 31:   MatColoring coloring;

 33:   PetscFunctionBeginUser;
 34:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
 35:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 37:   PetscCall(MatCreate(PETSC_COMM_WORLD, &J));
 38:   PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M));
 39:   PetscCall(MatSetFromOptions(J));
 40:   PetscCall(MatSetUp(J));

 42:   PetscCall(FormJacobian(J));
 43:   PetscCall(MatView(J, PETSC_VIEWER_STDOUT_WORLD));

 45:   /*
 46:     Color the matrix, i.e. determine groups of columns that share no common
 47:     rows. These columns in the Jacobian can all be computed simultaneously.
 48:    */
 49:   PetscCall(MatColoringCreate(J, &coloring));
 50:   PetscCall(MatColoringSetType(coloring, MATCOLORINGGREEDY));
 51:   PetscCall(MatColoringSetFromOptions(coloring));
 52:   PetscCall(MatColoringApply(coloring, &iscoloring));

 54:   if (size == 1) PetscCall(MatISColoringTest(J, iscoloring));

 56:   PetscCall(ISColoringDestroy(&iscoloring));
 57:   PetscCall(MatColoringDestroy(&coloring));
 58:   PetscCall(MatDestroy(&J));
 59:   PetscCall(PetscFinalize());
 60:   return 0;
 61: }

 63: /*TEST

 65:    test:
 66:       suffix: sl
 67:       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
 68:       args: -mat_coloring_type sl
 69:       output_file: output/ex24_1.out

 71:    test:
 72:       suffix: lf
 73:       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
 74:       args: -mat_coloring_type lf
 75:       output_file: output/ex24_1.out

 77:    test:
 78:       suffix: id
 79:       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
 80:       args: -mat_coloring_type id
 81:       output_file: output/ex24_1.out

 83: TEST*/