xref: /petsc/src/mat/tests/ex301.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1 
2 static char help[] = "Tests for bugs in A->offloadmask consistency for GPU matrices\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            A;
9   PetscInt       i,j,rstart,rend,m = 3;
10   PetscScalar    one = 1.0,zero = 0.0,negativeone = -1.0;
11   PetscReal      norm;
12   Vec            x,y;
13 
14   CHKERRQ(PetscInitialize(&argc,&args,(char*)0,help));
15   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
16 
17   for (i=0; i<2; i++) {
18     /* Create the matrix and set it to contain explicit zero entries on the diagonal. */
19     CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A));
20     CHKERRQ(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*m,m*m));
21     CHKERRQ(MatSetFromOptions(A));
22     CHKERRQ(MatSetUp(A));
23     CHKERRQ(MatGetOwnershipRange(A,&rstart,&rend));
24     CHKERRQ(MatCreateVecs(A,&x,&y));
25     CHKERRQ(VecSet(x,one));
26     CHKERRQ(VecSet(y,zero));
27     CHKERRQ(MatDiagonalSet(A,y,INSERT_VALUES));
28 
29     /* Now set A to be the identity using various approaches.
30      * Note that there may be other approaches that should be added here. */
31     switch (i) {
32     case 0:
33       CHKERRQ(MatDiagonalSet(A,x,INSERT_VALUES));
34       break;
35     case 1:
36       for (j=rstart; j<rend; j++) {
37         CHKERRQ(MatSetValue(A,j,j,one,INSERT_VALUES));
38       }
39       CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
40       CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
41       break;
42     case 2:
43       for (j=rstart; j<rend; j++) {
44         CHKERRQ(MatSetValuesRow(A,j,&one));
45       }
46       CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
47       CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
48     default:
49       break;
50     }
51 
52     /* Compute y <- A*x and verify that the difference between y and x is negligible, as it should be since A is the identity. */
53     CHKERRQ(MatMult(A,x,y));
54     CHKERRQ(VecAXPY(y,negativeone,x));
55     CHKERRQ(VecNorm(y,NORM_2,&norm));
56     if (norm > PETSC_SQRT_MACHINE_EPSILON) {
57       CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Test %" PetscInt_FMT ": Norm of error is %g, but should be near 0.\n",i,(double)norm));
58     }
59 
60     CHKERRQ(MatDestroy(&A));
61     CHKERRQ(VecDestroy(&x));
62     CHKERRQ(VecDestroy(&y));
63   }
64 
65   CHKERRQ(PetscFinalize());
66   return 0;
67 }
68 
69 /*TEST
70 
71    test:
72       suffix: aijviennacl_1
73       nsize: 1
74       args: -mat_type aijviennacl
75       requires: viennacl
76 
77    test:
78       suffix: aijviennacl_2
79       nsize: 2
80       args: -mat_type aijviennacl
81       requires: viennacl
82 
83    test:
84       suffix: aijcusparse_1
85       nsize: 1
86       args: -mat_type aijcusparse
87       requires: cuda
88 
89    test:
90       suffix: aijcusparse_2
91       nsize: 2
92       args: -mat_type aijcusparse
93       requires: cuda
94 TEST*/
95