xref: /petsc/src/mat/tests/ex24.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 
2 static char help[] = "Tests the different MatColoring implementatons and ISColoringTestValid() \n\
3                       Modifed from the code contributed by Ali Berk Kahraman. \n\n";
4 #include <petscmat.h>
5 
6 PetscErrorCode FormJacobian(Mat A) {
7   PetscInt    M, ownbegin, ownend, i, j;
8   PetscScalar dummy = 0.0;
9 
10   PetscFunctionBeginUser;
11   PetscCall(MatGetSize(A, &M, NULL));
12   PetscCall(MatGetOwnershipRange(A, &ownbegin, &ownend));
13 
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(0);
22 }
23 
24 int main(int argc, char *argv[]) {
25   Mat         J;
26   PetscMPIInt size;
27   PetscInt    M = 8;
28   ISColoring  iscoloring;
29   MatColoring coloring;
30 
31   PetscFunctionBeginUser;
32   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
33   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
34 
35   PetscCall(MatCreate(PETSC_COMM_WORLD, &J));
36   PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M));
37   PetscCall(MatSetFromOptions(J));
38   PetscCall(MatSetUp(J));
39 
40   PetscCall(FormJacobian(J));
41   PetscCall(MatView(J, PETSC_VIEWER_STDOUT_WORLD));
42 
43   /*
44     Color the matrix, i.e. determine groups of columns that share no common
45     rows. These columns in the Jacobian can all be computed simultaneously.
46    */
47   PetscCall(MatColoringCreate(J, &coloring));
48   PetscCall(MatColoringSetType(coloring, MATCOLORINGGREEDY));
49   PetscCall(MatColoringSetFromOptions(coloring));
50   PetscCall(MatColoringApply(coloring, &iscoloring));
51 
52   if (size == 1) { PetscCall(MatISColoringTest(J, iscoloring)); }
53 
54   PetscCall(ISColoringDestroy(&iscoloring));
55   PetscCall(MatColoringDestroy(&coloring));
56   PetscCall(MatDestroy(&J));
57   PetscCall(PetscFinalize());
58   return 0;
59 }
60 
61 /*TEST
62 
63    test:
64       suffix: sl
65       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
66       args: -mat_coloring_type sl
67       output_file: output/ex24_1.out
68 
69    test:
70       suffix: lf
71       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
72       args: -mat_coloring_type lf
73       output_file: output/ex24_1.out
74 
75    test:
76       suffix: id
77       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
78       args: -mat_coloring_type id
79       output_file: output/ex24_1.out
80 
81 TEST*/
82