Actual source code: ex24.c

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

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

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

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

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

 32:   PetscFunctionBeginUser;
 33:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 34:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

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

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

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

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

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

 62: /*TEST

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

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

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

 82: TEST*/