Actual source code: power.c

  1: #include <petsc/private/matimpl.h>

  3: static PetscErrorCode MatColoringApply_Power(MatColoring mc, ISColoring *iscoloring)
  4: {
  5:   Mat         m = mc->mat, mp, ms;
  6:   MatColoring imc;
  7:   const char *optionsprefix;

  9:   PetscFunctionBegin;
 10:   /* square the matrix repeatedly if necessary */
 11:   if (mc->dist == 1) {
 12:     mp = m;
 13:   } else {
 14:     PetscCall(MatMatMult(m, m, MAT_INITIAL_MATRIX, 2.0, &mp));
 15:     for (PetscInt i = 2; i < mc->dist; i++) {
 16:       ms = mp;
 17:       PetscCall(MatMatMult(m, ms, MAT_INITIAL_MATRIX, 2.0, &mp));
 18:       PetscCall(MatDestroy(&ms));
 19:     }
 20:   }
 21:   PetscCall(MatColoringCreate(mp, &imc));
 22:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)mc, &optionsprefix));
 23:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)imc, optionsprefix));
 24:   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)imc, "power_"));
 25:   PetscCall(MatColoringSetType(imc, MATCOLORINGGREEDY));
 26:   PetscCall(MatColoringSetDistance(imc, 1));
 27:   PetscCall(MatColoringSetWeightType(imc, mc->weight_type));
 28:   PetscCall(MatColoringSetFromOptions(imc));
 29:   PetscCall(MatColoringApply(imc, iscoloring));
 30:   PetscCall(MatColoringDestroy(&imc));
 31:   if (mp != m) PetscCall(MatDestroy(&mp));
 32:   PetscFunctionReturn(PETSC_SUCCESS);
 33: }

 35: /*MC
 36:   MATCOLORINGPOWER - Take the matrix's nth power, then do one-coloring on it.

 38:    Level: beginner

 40:    Notes:
 41:    This is merely a trivial test algorithm.

 43:    Supports any distance coloring.

 45: .seealso: [](sec_fdmatrix), [](sec_matfactor), `MatColoring`, `MatColoringType`, `MatColoringCreate()`, `MatColoringSetType()`
 46: M*/
 47: PETSC_EXTERN PetscErrorCode MatColoringCreate_Power(MatColoring mc)
 48: {
 49:   PetscFunctionBegin;
 50:   mc->ops->apply          = MatColoringApply_Power;
 51:   mc->ops->view           = NULL;
 52:   mc->ops->destroy        = NULL;
 53:   mc->ops->setfromoptions = NULL;
 54:   PetscFunctionReturn(PETSC_SUCCESS);
 55: }