xref: /petsc/src/mat/tests/ex24.c (revision 21e3ffae2f3b73c0bd738cf6d0a809700fc04bb0)
1 
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>
5 
6 PetscErrorCode FormJacobian(Mat A)
7 {
8   PetscInt    M, ownbegin, ownend, i, j;
9   PetscScalar dummy = 0.0;
10 
11   PetscFunctionBeginUser;
12   PetscCall(MatGetSize(A, &M, NULL));
13   PetscCall(MatGetOwnershipRange(A, &ownbegin, &ownend));
14 
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 }
24 
25 int main(int argc, char *argv[])
26 {
27   Mat         J;
28   PetscMPIInt size;
29   PetscInt    M = 8;
30   ISColoring  iscoloring;
31   MatColoring coloring;
32 
33   PetscFunctionBeginUser;
34   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
35   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
36 
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));
41 
42   PetscCall(FormJacobian(J));
43   PetscCall(MatView(J, PETSC_VIEWER_STDOUT_WORLD));
44 
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));
53 
54   if (size == 1) PetscCall(MatISColoringTest(J, iscoloring));
55 
56   PetscCall(ISColoringDestroy(&iscoloring));
57   PetscCall(MatColoringDestroy(&coloring));
58   PetscCall(MatDestroy(&J));
59   PetscCall(PetscFinalize());
60   return 0;
61 }
62 
63 /*TEST
64 
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
70 
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
76 
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
82 
83 TEST*/
84