Actual source code: ex5.c
1: /*$Id: ex5.c,v 1.27 2001/08/07 21:30:08 bsmith Exp $*/
2:
3: static char help[] = "Tests MatMult(), MatMultAdd(), MatMultTranspose().n
4: Also MatMultTransposeAdd(), MatScale(), MatGetDiagonal(), and MatDiagonalScale().nn";
6: #include petscmat.h
8: int main(int argc,char **args)
9: {
10: Mat C;
11: Vec s,u,w,x,y,z;
12: int ierr,i,j,m = 8,n,rstart,rend,vstart,vend;
13: PetscScalar one = 1.0,negone = -1.0,v,alpha=0.1;
14: PetscReal norm;
15: PetscTruth flg;
17: PetscInitialize(&argc,&args,(char *)0,help);
18: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
19: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
20: n = m;
21: PetscOptionsHasName(PETSC_NULL,"-rectA",&flg);
22: if (flg) n += 2;
23: PetscOptionsHasName(PETSC_NULL,"-rectB",&flg);
24: if (flg) n -= 2;
26: /* ---------- Assemble matrix and vectors ----------- */
28: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m,n,&C);
29: MatSetFromOptions(C);
30: MatGetOwnershipRange(C,&rstart,&rend);
31: VecCreate(PETSC_COMM_WORLD,&x);
32: VecSetSizes(x,PETSC_DECIDE,m);
33: VecSetFromOptions(x);
34: VecDuplicate(x,&z);
35: VecDuplicate(x,&w);
36: VecCreate(PETSC_COMM_WORLD,&y);
37: VecSetSizes(y,PETSC_DECIDE,n);
38: VecSetFromOptions(y);
39: VecDuplicate(y,&u);
40: VecDuplicate(y,&s);
41: VecGetOwnershipRange(y,&vstart,&vend);
43: /* Assembly */
44: for (i=rstart; i<rend; i++) {
45: v = 100*(i+1);
46: VecSetValues(z,1,&i,&v,INSERT_VALUES);
47: for (j=0; j<n; j++) {
48: v=10*(i+1)+j+1;
49: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
50: }
51: }
53: /* Flush off proc Vec values and do more assembly */
54: VecAssemblyBegin(z);
55: for (i=vstart; i<vend; i++) {
56: v = one*((PetscReal)i);
57: VecSetValues(y,1,&i,&v,INSERT_VALUES);
58: v = 100.0*i;
59: VecSetValues(u,1,&i,&v,INSERT_VALUES);
60: }
62: /* Flush off proc Mat values and do more assembly */
63: MatAssemblyBegin(C,MAT_FLUSH_ASSEMBLY);
64: for (i=rstart; i<rend; i++) {
65: for (j=0; j<n; j++) {
66: v=10*(i+1)+j+1;
67: MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
68: }
69: }
70: /* Try overlap Coomunication with the next stage XXXSetValues */
71: VecAssemblyEnd(z);
72: MatAssemblyEnd(C,MAT_FLUSH_ASSEMBLY);
74: /* The Assembly for the second Stage */
75: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
76: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
77: VecAssemblyBegin(y);
78: VecAssemblyEnd(y);
79: MatScale(&alpha,C);
80: VecAssemblyBegin(u);
81: VecAssemblyEnd(u);
83: /* ------------ Test MatMult(), MatMultAdd() ---------- */
85: PetscPrintf(PETSC_COMM_WORLD,"testing MatMult()n");
86: MatMult(C,y,x);
87: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
88: PetscPrintf(PETSC_COMM_WORLD,"testing MatMultAdd()n");
89: MatMultAdd(C,y,z,w);
90: VecAXPY(&one,z,x);
91: VecAXPY(&negone,w,x);
92: VecNorm(x,NORM_2,&norm);
93: if (norm > 1.e-8){
94: PetscPrintf(PETSC_COMM_WORLD,"Norm of error difference = %gn",norm);
95: }
97: /* ------- Test MatMultTranspose(), MatMultTransposeAdd() ------- */
99: for (i=rstart; i<rend; i++) {
100: v = one*((PetscReal)i);
101: VecSetValues(x,1,&i,&v,INSERT_VALUES);
102: }
103: VecAssemblyBegin(x);
104: VecAssemblyEnd(x);
105: PetscPrintf(PETSC_COMM_WORLD,"testing MatMultTranspose()n");
106: MatMultTranspose(C,x,y);
107: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
109: PetscPrintf(PETSC_COMM_WORLD,"testing MatMultTransposeAdd()n");
110: MatMultTransposeAdd(C,x,u,s);
111: VecAXPY(&one,u,y);
112: VecAXPY(&negone,s,y);
113: VecNorm(y,NORM_2,&norm);
114: if (norm > 1.e-8){
115: PetscPrintf(PETSC_COMM_WORLD,"Norm of error difference = %gn",norm);
116: }
118: /* -------------------- Test MatGetDiagonal() ------------------ */
120: PetscPrintf(PETSC_COMM_WORLD,"testing MatGetDiagonal(), MatDiagonalScale()n");
121: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
122: VecSet(&one,x);
123: MatGetDiagonal(C,x);
124: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
125: for (i=vstart; i<vend; i++) {
126: v = one*((PetscReal)(i+1));
127: VecSetValues(y,1,&i,&v,INSERT_VALUES);
128: }
130: /* -------------------- Test () MatDiagonalScale ------------------ */
131: PetscOptionsHasName(PETSC_NULL,"-test_diagonalscale",&flg);
132: if (flg) {
133: MatDiagonalScale(C,x,y);
134: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
135: }
136: /* Free data structures */
137: VecDestroy(u); VecDestroy(s);
138: VecDestroy(w); VecDestroy(x);
139: VecDestroy(y); VecDestroy(z);
140: MatDestroy(C);
142: PetscFinalize();
143: return 0;
144: }