Actual source code: ex2.c
petsc-3.10.5 2019-03-28
2: static char help[] = "Tests MatTranspose(), MatNorm(), MatAXPY() and MatAYPX().\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **argv)
7: {
8: Mat mat,tmat = 0;
9: PetscInt m = 7,n,i,j,rstart,rend,rect = 0;
11: PetscMPIInt size,rank;
12: PetscBool flg;
13: PetscScalar v, alpha;
14: PetscReal normf,normi,norm1;
16: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
17: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
18: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
19: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
20: MPI_Comm_size(PETSC_COMM_WORLD,&size);
21: n = m;
22: PetscOptionsHasName(NULL,NULL,"-rectA",&flg);
23: if (flg) {n += 2; rect = 1;}
24: PetscOptionsHasName(NULL,NULL,"-rectB",&flg);
25: if (flg) {n -= 2; rect = 1;}
27: /* ------- Assemble matrix, test MatValid() --------- */
28: MatCreate(PETSC_COMM_WORLD,&mat);
29: MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,m,n);
30: MatSetFromOptions(mat);
31: MatSetUp(mat);
32: MatGetOwnershipRange(mat,&rstart,&rend);
33: for (i=rstart; i<rend; i++) {
34: for (j=0; j<n; j++) {
35: v = 10.0*i+j;
36: MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
37: }
38: }
39: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
42: /* ----------------- Test MatNorm() ----------------- */
43: MatNorm(mat,NORM_FROBENIUS,&normf);
44: MatNorm(mat,NORM_1,&norm1);
45: MatNorm(mat,NORM_INFINITY,&normi);
46: PetscPrintf(PETSC_COMM_WORLD,"original A: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
47: MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
49: /* --------------- Test MatTranspose() -------------- */
50: PetscOptionsHasName(NULL,NULL,"-in_place",&flg);
51: if (!rect && flg) {
52: MatTranspose(mat,MAT_REUSE_MATRIX,&mat); /* in-place transpose */
53: tmat = mat; mat = 0;
54: } else { /* out-of-place transpose */
55: MatTranspose(mat,MAT_INITIAL_MATRIX,&tmat);
56: }
58: /* ----------------- Test MatNorm() ----------------- */
59: /* Print info about transpose matrix */
60: MatNorm(tmat,NORM_FROBENIUS,&normf);
61: MatNorm(tmat,NORM_1,&norm1);
62: MatNorm(tmat,NORM_INFINITY,&normi);
63: PetscPrintf(PETSC_COMM_WORLD,"B = A^T: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi);
64: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
66: /* ----------------- Test MatAXPY(), MatAYPX() ----------------- */
67: if (mat && !rect) {
68: alpha = 1.0;
69: PetscOptionsGetScalar(NULL,NULL,"-alpha",&alpha,NULL);
70: PetscPrintf(PETSC_COMM_WORLD,"MatAXPY: B = B + alpha * A\n");
71: MatAXPY(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN);
72: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
74: PetscPrintf(PETSC_COMM_WORLD,"MatAYPX: B = alpha*B + A\n");
75: MatAYPX(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN);
76: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
77: }
79: {
80: Mat C;
81: alpha = 1.0;
82: PetscPrintf(PETSC_COMM_WORLD,"MatAXPY: C = C + alpha * A, C=A, SAME_NONZERO_PATTERN\n");
83: MatDuplicate(mat,MAT_COPY_VALUES,&C);
84: MatAXPY(C,alpha,mat,SAME_NONZERO_PATTERN);
85: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
86: MatDestroy(&C);
87: }
89: {
90: Mat matB;
91: /* get matB that has nonzeros of mat in all even numbers of row and col */
92: MatCreate(PETSC_COMM_WORLD,&matB);
93: MatSetSizes(matB,PETSC_DECIDE,PETSC_DECIDE,m,n);
94: MatSetFromOptions(matB);
95: MatSetUp(matB);
96: MatGetOwnershipRange(matB,&rstart,&rend);
97: if (rstart % 2 != 0) rstart++;
98: for (i=rstart; i<rend; i += 2) {
99: for (j=0; j<n; j += 2) {
100: v = 10.0*i+j;
101: MatSetValues(matB,1,&i,1,&j,&v,INSERT_VALUES);
102: }
103: }
104: MatAssemblyBegin(matB,MAT_FINAL_ASSEMBLY);
105: MatAssemblyEnd(matB,MAT_FINAL_ASSEMBLY);
106: PetscPrintf(PETSC_COMM_WORLD," A: original matrix:\n");
107: MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
108: PetscPrintf(PETSC_COMM_WORLD," B(a subset of A):\n");
109: MatView(matB,PETSC_VIEWER_STDOUT_WORLD);
110: PetscPrintf(PETSC_COMM_WORLD,"MatAXPY: B = B + alpha * A, SUBSET_NONZERO_PATTERN\n");
111: MatAXPY(mat,alpha,matB,SUBSET_NONZERO_PATTERN);
112: MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
113: MatDestroy(&matB);
114: }
116: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
117: /* Free data structures */
118: MatDestroy(&mat);
119: MatDestroy(&tmat);
120: PetscFinalize();
121: return ierr;
122: }
128: /*TEST
130: test:
131: suffix: 11_A
132: args: -mat_type seqaij -rectA
133: filter: grep -v "Mat Object"
135: test:
136: suffix: 12_A
137: args: -mat_type seqdense -rectA
138: filter: grep -v "Mat Object"
140: test:
141: suffix: 11_B
142: args: -mat_type seqaij -rectB
143: filter: grep -v "Mat Object"
145: test:
146: suffix: 12_B
147: args: -mat_type seqdense -rectB
148: filter: grep -v "Mat Object"
150: test:
151: suffix: 21
152: args: -mat_type mpiaij
154: test:
155: suffix: 22
156: args: -mat_type mpidense
158: test:
159: suffix: 23
160: nsize: 3
161: args: -mat_type mpiaij
162: filter: grep -v type | grep -v "MPI processes"
164: test:
165: suffix: 24
166: nsize: 3
167: args: -mat_type mpidense
169: test:
170: suffix: 2_aijcusparse_1
171: args: -mat_type mpiaijcusparse
172: output_file: output/ex2_23.out
173: requires: veccuda
174: filter: grep -v type | grep -v "MPI processes"
176: test:
177: suffix: 2_aijcusparse_2
178: nsize: 3
179: args: -mat_type mpiaijcusparse
180: output_file: output/ex2_23.out
181: requires: veccuda
182: filter: grep -v type | grep -v "MPI processes"
184: test:
185: suffix: 3
186: nsize: 2
187: args: -mat_type mpiaij -rectA
189: test:
190: suffix: 3_aijcusparse
191: nsize: 2
192: args: -mat_type mpiaijcusparse -rectA
193: requires: veccuda
195: test:
196: suffix: 4
197: nsize: 2
198: args: -mat_type mpidense -rectA
200: test:
201: suffix: aijcusparse_1
202: args: -mat_type seqaijcusparse -rectA
203: filter: grep -v "Mat Object"
204: output_file: output/ex2_11_A_aijcusparse.out
205: requires: veccuda
207: test:
208: suffix: aijcusparse_2
209: args: -mat_type seqaijcusparse -rectB
210: filter: grep -v "Mat Object"
211: output_file: output/ex2_11_B_aijcusparse.out
212: requires: veccuda
214: TEST*/