xref: /petsc/src/mat/tests/ex90.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
1 
2 static char help[] ="Tests MatPtAP() \n";
3 
4 #include <petscmat.h>
5 
6 /*
7  * This is an extremely simple example to test MatPtAP. It is very useful when developing and debugging the code.
8  *
9  * A =
10 
11    1   2   0   4
12    0   1   2   0
13    2   0   4   0
14    0   1   2   1
15  *
16  *
17  *
18  * P =
19 
20    1.00000   0.00000
21    0.30000   0.50000
22    0.00000   0.80000
23    0.90000   0.00000
24  *
25  *
26  *AP =
27  *
28  * 5.20000   1.00000
29    0.30000   2.10000
30    2.00000   3.20000
31    1.20000   2.10000
32  *
33  * PT =
34 
35    1.00000   0.30000   0.00000   0.90000
36    0.00000   0.50000   0.80000   0.00000
37 
38  *
39  * C =
40 
41    6.3700   3.5200
42    1.7500   3.6100
43  *
44  * */
45 
46 int main(int argc,char **argv)
47 {
48   PetscErrorCode ierr;
49   Mat            A,P,PtAP;
50   PetscInt       i1[] = {0, 3, 5}, i2[] = {0,2,5};
51   PetscInt       j1[] = {0, 1, 3, 1, 2}, j2[] = {0, 2, 1, 2, 3};
52   PetscScalar    a1[] = {1, 2, 4, 1, 2}, a2[] = {2, 4, 1, 2, 1};
53   PetscInt       pi1[] = {0,1,3}, pi2[] = {0,1,2};
54   PetscInt       pj1[] = {0, 0, 1}, pj2[] = {1,0};
55   PetscScalar    pa1[] = {1, 0.3, 0.5}, pa2[] = {0.8, 0.9};
56   MPI_Comm       comm;
57   PetscMPIInt    rank,size;
58 
59   ierr = PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
60   comm = PETSC_COMM_WORLD;
61   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
62   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
63   if (size != 2) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"You have to use two processor cores to run this example \n");
64   ierr = MatCreateMPIAIJWithArrays(comm,2,2,PETSC_DETERMINE,PETSC_DETERMINE,rank? i2:i1,rank? j2:j1,rank? a2:a1,&A);CHKERRQ(ierr);
65   ierr = MatCreateMPIAIJWithArrays(comm,2,1,PETSC_DETERMINE,PETSC_DETERMINE,rank? pi2:pi1,rank? pj2:pj1,rank? pa2:pa1,&P);CHKERRQ(ierr);
66   ierr = MatPtAP(A,P,MAT_INITIAL_MATRIX,1.1,&PtAP);CHKERRQ(ierr);
67   ierr = MatView(A,NULL);CHKERRQ(ierr);
68   ierr = MatView(P,NULL);CHKERRQ(ierr);
69   ierr = MatView(PtAP,NULL);CHKERRQ(ierr);
70   ierr = MatPtAP(A,P,MAT_REUSE_MATRIX,1.1,&PtAP);CHKERRQ(ierr);
71   ierr = MatView(A,NULL);CHKERRQ(ierr);
72   ierr = MatView(P,NULL);CHKERRQ(ierr);
73   ierr = MatView(PtAP,NULL);CHKERRQ(ierr);
74   ierr = MatDestroy(&A);CHKERRQ(ierr);
75   ierr = MatDestroy(&P);CHKERRQ(ierr);
76   ierr = MatDestroy(&PtAP);CHKERRQ(ierr);
77   ierr = PetscFinalize();
78   return ierr;
79 }
80 
81 /*TEST
82    test:
83      nsize: 2
84      args:   -matptap_via allatonce
85      output_file: output/ex90_1.out
86 
87    test:
88      nsize: 2
89      suffix: merged
90      args:   -matptap_via allatonce_merged
91      output_file: output/ex90_1.out
92 
93 TEST*/
94