xref: /petsc/src/mat/tests/ex24.c (revision 2fa40bb9206b96114faa7cb222621ec184d31cd2)
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 {
8   PetscErrorCode ierr;
9   PetscInt       M,ownbegin,ownend,i,j;
10   PetscScalar    dummy=0.0;
11 
12   PetscFunctionBeginUser;
13   ierr = MatGetSize(A,&M,NULL);CHKERRQ(ierr);
14   ierr = MatGetOwnershipRange(A,&ownbegin,&ownend);CHKERRQ(ierr);
15 
16   for (i=ownbegin; i<ownend; i++) {
17     for (j=i-3; j<i+3; j++) {
18       if (j >= 0 && j < M) {
19         ierr = MatSetValues(A,1,&i,1,&j,&dummy,INSERT_VALUES);CHKERRQ(ierr);
20       }
21     }
22   }
23   ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24   ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25   PetscFunctionReturn(0);
26 }
27 
28 int main(int argc, char *argv[])
29 {
30   PetscErrorCode ierr;
31   Mat            J;
32   PetscMPIInt    size;
33   PetscInt       M=8;
34   ISColoring     iscoloring;
35   MatColoring    coloring;
36 
37   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
38   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr);
39 
40   ierr = MatCreate(PETSC_COMM_WORLD,&J);CHKERRQ(ierr);
41   ierr = MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M);CHKERRQ(ierr);
42   ierr = MatSetFromOptions(J);CHKERRQ(ierr);
43   ierr = MatSetUp(J);CHKERRQ(ierr);
44 
45   ierr = FormJacobian(J);CHKERRQ(ierr);
46   ierr = MatView(J,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
47 
48   /*
49     Color the matrix, i.e. determine groups of columns that share no common
50     rows. These columns in the Jacobian can all be computed simultaneously.
51    */
52   ierr = MatColoringCreate(J, &coloring);CHKERRQ(ierr);
53   ierr = MatColoringSetType(coloring,MATCOLORINGGREEDY);CHKERRQ(ierr);
54   ierr = MatColoringSetFromOptions(coloring);CHKERRQ(ierr);
55   ierr = MatColoringApply(coloring, &iscoloring);CHKERRQ(ierr);
56 
57   if (size == 1) {
58     ierr = MatISColoringTest(J,iscoloring);CHKERRQ(ierr);
59   }
60 
61   ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
62   ierr = MatColoringDestroy(&coloring);CHKERRQ(ierr);
63   ierr = MatDestroy(&J);CHKERRQ(ierr);
64   ierr = PetscFinalize();
65   return ierr;
66 }
67 
68 /*TEST
69 
70    test:
71       suffix: sl
72       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
73       args: -mat_coloring_type sl
74       output_file: output/ex24_1.out
75 
76    test:
77       suffix: lf
78       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
79       args: -mat_coloring_type lf
80       output_file: output/ex24_1.out
81 
82    test:
83       suffix: id
84       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
85       args: -mat_coloring_type id
86       output_file: output/ex24_1.out
87 
88 TEST*/
89