Actual source code: ex94.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests sequential and parallel MatMatMult() and MatPtAP(), MatTransposeMatMult(), sequential MatMatTransposeMult(), MatRARt()\n\
3: Input arguments are:\n\
4: -f0 <input_file> -f1 <input_file> -f2 <input_file> -f3 <input_file> : file to load\n\n";
5: /* Example of usage:
6: ./ex94 -f0 <A_binary> -f1 <B_binary> -matmatmult_mat_view ::ascii_info -matmatmulttr_mat_view
7: mpiexec -n 3 ./ex94 -f0 medium -f1 medium -f2 arco1 -f3 arco1 -matmatmult_mat_view
8: */
10: #include <petscmat.h>
14: int main(int argc,char **args)
15: {
16: Mat A,A_save,B,P,R,C,C1;
17: Vec x,v1,v2,v3,v4;
18: PetscViewer viewer;
20: PetscMPIInt size,rank;
21: PetscInt i,m,n,j,*idxn,M,N,nzp,rstart,rend;
22: PetscReal norm,norm_abs,norm_tmp,tol=1.e-8,fill=4.0;
23: PetscRandom rdm;
24: char file[4][128];
25: PetscBool flg,preload = PETSC_TRUE;
26: PetscScalar *a,rval,alpha,none = -1.0;
27: PetscBool Test_MatMatMult=PETSC_TRUE,Test_MatMatTr=PETSC_TRUE,Test_MatPtAP=PETSC_TRUE,Test_MatRARt=PETSC_TRUE,Test_MatMatMatMult=PETSC_TRUE;
28: PetscBool Test_MatAXPY=PETSC_FALSE;
29: PetscInt pm,pn,pM,pN;
30: MatInfo info;
32: PetscInitialize(&argc,&args,(char*)0,help);
33: MPI_Comm_size(PETSC_COMM_WORLD,&size);
34: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
36: PetscOptionsGetReal(NULL,NULL,"-fill",&fill,NULL);
38: /* Load the matrices A_save and B */
39: PetscOptionsGetString(NULL,NULL,"-f0",file[0],128,&flg);
40: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for small matrix A with the -f0 option.");
41: PetscOptionsGetString(NULL,NULL,"-f1",file[1],128,&flg);
42: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for small matrix B with the -f1 option.");
43: PetscOptionsGetString(NULL,NULL,"-f2",file[2],128,&flg);
44: if (!flg) {
45: preload = PETSC_FALSE;
46: } else {
47: PetscOptionsGetString(NULL,NULL,"-f3",file[3],128,&flg);
48: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for test matrix B with the -f3 option.");
49: }
51: PetscPreLoadBegin(preload,"Load system");
52: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[2*PetscPreLoadIt],FILE_MODE_READ,&viewer);
53: MatCreate(PETSC_COMM_WORLD,&A_save);
54: MatSetFromOptions(A_save);
55: MatLoad(A_save,viewer);
56: PetscViewerDestroy(&viewer);
58: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[2*PetscPreLoadIt+1],FILE_MODE_READ,&viewer);
59: MatCreate(PETSC_COMM_WORLD,&B);
60: MatSetFromOptions(B);
61: MatLoad(B,viewer);
62: PetscViewerDestroy(&viewer);
64: MatGetSize(B,&M,&N);
65: nzp = PetscMax((PetscInt)(0.1*M),5);
66: PetscMalloc((nzp+1)*(sizeof(PetscInt)+sizeof(PetscScalar)),&idxn);
67: a = (PetscScalar*)(idxn + nzp);
69: /* Create vectors v1 and v2 that are compatible with A_save */
70: VecCreate(PETSC_COMM_WORLD,&v1);
71: MatGetLocalSize(A_save,&m,NULL);
72: VecSetSizes(v1,m,PETSC_DECIDE);
73: VecSetFromOptions(v1);
74: VecDuplicate(v1,&v2);
76: PetscRandomCreate(PETSC_COMM_WORLD,&rdm);
77: PetscRandomSetFromOptions(rdm);
78: PetscOptionsGetReal(NULL,NULL,"-fill",&fill,NULL);
80: /* Test MatAXPY() */
81: /*-------------------*/
82: PetscOptionsHasName(NULL,NULL,"-test_MatAXPY",&Test_MatAXPY);
83: if (Test_MatAXPY) {
84: Mat Btmp;
85: /* if (!rank) printf(" Loading matrices is done...\n"); */
86: MatDuplicate(A_save,MAT_COPY_VALUES,&A);
87: MatDuplicate(B,MAT_COPY_VALUES,&Btmp);
88: MatAXPY(A,-1.0,B,DIFFERENT_NONZERO_PATTERN); /* A = -B + A_save */
90: /* if (!rank) printf(" Test_MatAXPY is done, now checking accuracy ...\n"); */
91: MatScale(A,-1.0); /* A = -A = B - A_save */
92: MatAXPY(Btmp,-1.0,A,DIFFERENT_NONZERO_PATTERN); /* Btmp = -A + B = A_save */
93: MatMultEqual(A_save,Btmp,10,&flg);
94: if (!flg) SETERRQ(PETSC_COMM_SELF,0,"MatAXPY() is incorrect\n");
95: MatDestroy(&A);
96: MatDestroy(&Btmp);
98: Test_MatMatMult = PETSC_FALSE;
99: Test_MatMatTr = PETSC_FALSE;
100: Test_MatPtAP = PETSC_FALSE;
101: Test_MatRARt = PETSC_FALSE;
102: Test_MatMatMatMult = PETSC_FALSE;
103: }
105: /* 1) Test MatMatMult() */
106: /* ---------------------*/
107: if (Test_MatMatMult) {
108: MatDuplicate(A_save,MAT_COPY_VALUES,&A);
109: MatMatMult(A,B,MAT_INITIAL_MATRIX,fill,&C);
110: MatSetOptionsPrefix(C,"matmatmult_"); /* enable option '-matmatmult_' for matrix C */
111: MatGetInfo(C,MAT_GLOBAL_SUM,&info);
112: /* PetscPrintf(PETSC_COMM_WORLD,"MatMatMult: nz_allocated = %g; nz_used = %g; nz_unneeded = %g\n",info.nz_allocated,info.nz_used, info.nz_unneeded); */
114: /* Test MAT_REUSE_MATRIX - reuse symbolic C */
115: alpha=1.0;
116: for (i=0; i<2; i++) {
117: alpha -=0.1;
118: MatScale(A,alpha);
119: MatMatMult(A,B,MAT_REUSE_MATRIX,fill,&C);
120: }
121: MatMatMultEqual(A,B,C,10,&flg);
122: if (!flg) {
123: PetscPrintf(PETSC_COMM_SELF,"Error: MatMatMult()\n");
124: }
125: MatDestroy(&A);
127: /* Test MatDuplicate() of C=A*B */
128: MatDuplicate(C,MAT_COPY_VALUES,&C1);
129: MatDestroy(&C1);
130: MatDestroy(&C);
131: } /* if (Test_MatMatMult) */
133: /* 2) Test MatTransposeMatMult() and MatMatTransposeMult() */
134: /* ------------------------------------------------------- */
135: if (Test_MatMatTr) {
136: /* Create P */
137: PetscInt PN,rstart,rend;
138: PN = M/2;
139: nzp = 5; /* num of nonzeros in each row of P */
140: MatCreate(PETSC_COMM_WORLD,&P);
141: MatSetSizes(P,PETSC_DECIDE,PETSC_DECIDE,M,PN);
142: MatSetType(P,MATAIJ);
143: MatSeqAIJSetPreallocation(P,nzp,NULL);
144: MatMPIAIJSetPreallocation(P,nzp,NULL,nzp,NULL);
145: MatGetOwnershipRange(P,&rstart,&rend);
146: for (i=0; i<nzp; i++) {
147: PetscRandomGetValue(rdm,&a[i]);
148: }
149: for (i=rstart; i<rend; i++) {
150: for (j=0; j<nzp; j++) {
151: PetscRandomGetValue(rdm,&rval);
152: idxn[j] = (PetscInt)(PetscRealPart(rval)*PN);
153: }
154: MatSetValues(P,1,&i,nzp,idxn,a,ADD_VALUES);
155: }
156: MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY);
157: MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY);
159: /* Create R = P^T */
160: MatTranspose(P,MAT_INITIAL_MATRIX,&R);
162: { /* Test R = P^T, C1 = R*B */
163: MatMatMult(R,B,MAT_INITIAL_MATRIX,fill,&C1);
164: MatTranspose(P,MAT_REUSE_MATRIX,&R);
165: MatMatMult(R,B,MAT_REUSE_MATRIX,fill,&C1);
166: MatDestroy(&C1);
167: }
169: /* C = P^T*B */
170: MatTransposeMatMult(P,B,MAT_INITIAL_MATRIX,fill,&C);
171: MatGetInfo(C,MAT_GLOBAL_SUM,&info);
173: /* Test MAT_REUSE_MATRIX - reuse symbolic C */
174: MatTransposeMatMult(P,B,MAT_REUSE_MATRIX,fill,&C);
176: /* Compare P^T*B and R*B */
177: MatMatMult(R,B,MAT_INITIAL_MATRIX,fill,&C1);
178: MatEqual(C,C1,&flg);
179: if (!flg) {
180: /* Check norm of C1 = (-1.0)*C + C1 */
181: PetscReal nrm;
182: MatAXPY(C1,-1.0,C,DIFFERENT_NONZERO_PATTERN);
183: MatNorm(C1,NORM_INFINITY,&nrm);
184: if (nrm > 1.e-14) {
185: PetscPrintf(PETSC_COMM_WORLD,"Error in MatTransposeMatMult(): %g\n",nrm);
186: }
187: }
188: MatDestroy(&C1);
190: /* Test MatDuplicate() of C=P^T*B */
191: MatDuplicate(C,MAT_COPY_VALUES,&C1);
192: MatDestroy(&C1);
193: MatDestroy(&C);
195: /* C = B*R^T */
196: if (size == 1) {
197: MatMatTransposeMult(B,R,MAT_INITIAL_MATRIX,fill,&C);
198: MatSetOptionsPrefix(C,"matmatmulttr_"); /* enable '-matmatmulttr_' for matrix C */
199: MatGetInfo(C,MAT_GLOBAL_SUM,&info);
201: /* Test MAT_REUSE_MATRIX - reuse symbolic C */
202: MatMatTransposeMult(B,R,MAT_REUSE_MATRIX,fill,&C);
204: /* Check */
205: MatMatMult(B,P,MAT_INITIAL_MATRIX,fill,&C1);
206: MatEqual(C,C1,&flg);
207: if (!flg) {
208: PetscPrintf(PETSC_COMM_WORLD,"Error in MatMatTransposeMult()\n");
209: }
210: MatDestroy(&C1);
211: MatDestroy(&C);
212: }
213: MatDestroy(&P);
214: MatDestroy(&R);
215: }
217: /* 3) Test MatPtAP() */
218: /*-------------------*/
219: if (Test_MatPtAP) {
220: PetscInt PN;
221: Mat Cdup;
223: MatDuplicate(A_save,MAT_COPY_VALUES,&A);
224: MatGetSize(A,&M,&N);
225: MatGetLocalSize(A,&m,&n);
226: /* PetscPrintf(PETSC_COMM_SELF,"[%d] A: %d,%d, %d,%d\n",rank,m,n,M,N); */
228: PN = M/2;
229: nzp = (PetscInt)(0.1*PN+1); /* num of nozeros in each row of P */
230: MatCreate(PETSC_COMM_WORLD,&P);
231: MatSetSizes(P,PETSC_DECIDE,PETSC_DECIDE,N,PN);
232: MatSetType(P,MATAIJ);
233: MatSeqAIJSetPreallocation(P,nzp,NULL);
234: MatMPIAIJSetPreallocation(P,nzp,NULL,nzp,NULL);
235: for (i=0; i<nzp; i++) {
236: PetscRandomGetValue(rdm,&a[i]);
237: }
238: MatGetOwnershipRange(P,&rstart,&rend);
239: for (i=rstart; i<rend; i++) {
240: for (j=0; j<nzp; j++) {
241: PetscRandomGetValue(rdm,&rval);
242: idxn[j] = (PetscInt)(PetscRealPart(rval)*PN);
243: }
244: MatSetValues(P,1,&i,nzp,idxn,a,ADD_VALUES);
245: }
246: MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY);
247: MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY);
249: /* MatView(P,PETSC_VIEWER_STDOUT_WORLD); */
250: MatGetSize(P,&pM,&pN);
251: MatGetLocalSize(P,&pm,&pn);
252: MatPtAP(A,P,MAT_INITIAL_MATRIX,fill,&C);
253: /* if (!rank) {PetscPrintf(PETSC_COMM_SELF," MatPtAP() is done, P, %d, %d, %d,%d\n",pm,pn,pM,pN);} */
255: /* Test MAT_REUSE_MATRIX - reuse symbolic C */
256: alpha=1.0;
257: for (i=0; i<2; i++) {
258: alpha -=0.1;
259: MatScale(A,alpha);
260: MatPtAP(A,P,MAT_REUSE_MATRIX,fill,&C);
261: }
263: /* Test PtAP ops with P SeqDense and A either SeqAIJ or SeqDense (it assumes MatPtAP_SeqAIJ_SeqAIJ is fine) */
264: if (size == 1) {
265: Mat Cdensetest,Pdense,Cdense,Adense;
266: PetscReal norm;
268: MatConvert(C,MATSEQDENSE,MAT_INITIAL_MATRIX,&Cdensetest);
269: MatConvert(P,MATSEQDENSE,MAT_INITIAL_MATRIX,&Pdense);
271: /* test with A SeqAIJ */
272: MatPtAP(A,Pdense,MAT_INITIAL_MATRIX,fill,&Cdense);
273: MatAXPY(Cdense,-1.0,Cdensetest,SAME_NONZERO_PATTERN);
274: MatNorm(Cdense,NORM_FROBENIUS,&norm);
275: if (norm > PETSC_SMALL) {
276: PetscPrintf(PETSC_COMM_SELF,"Error in MatPtAP with A SeqAIJ and P SeqDense: %g\n",norm);
277: }
278: MatScale(Cdense,-1.);
279: MatPtAP(A,Pdense,MAT_REUSE_MATRIX,fill,&Cdense);
280: MatAXPY(Cdense,-1.0,Cdensetest,SAME_NONZERO_PATTERN);
281: MatNorm(Cdense,NORM_FROBENIUS,&norm);
282: if (norm > PETSC_SMALL) {
283: PetscPrintf(PETSC_COMM_SELF,"Error in MatPtAP with A SeqAIJ and P SeqDense and MAT_REUSE_MATRIX: %g\n",norm);
284: }
285: MatDestroy(&Cdense);
287: /* test with A SeqDense */
288: MatConvert(A,MATSEQDENSE,MAT_INITIAL_MATRIX,&Adense);
289: MatPtAP(Adense,Pdense,MAT_INITIAL_MATRIX,fill,&Cdense);
290: MatAXPY(Cdense,-1.0,Cdensetest,SAME_NONZERO_PATTERN);
291: MatNorm(Cdense,NORM_FROBENIUS,&norm);
292: if (norm > PETSC_SMALL) {
293: PetscPrintf(PETSC_COMM_SELF,"Error in MatPtAP with A SeqDense and P SeqDense: %g\n",norm);
294: }
295: MatScale(Cdense,-1.);
296: MatPtAP(Adense,Pdense,MAT_REUSE_MATRIX,fill,&Cdense);
297: MatAXPY(Cdense,-1.0,Cdensetest,SAME_NONZERO_PATTERN);
298: MatNorm(Cdense,NORM_FROBENIUS,&norm);
299: if (norm > PETSC_SMALL) {
300: PetscPrintf(PETSC_COMM_SELF,"Error in MatPtAP with A SeqDense and P SeqDense and MAT_REUSE_MATRIX: %g\n",norm);
301: }
302: MatDestroy(&Cdense);
304: MatDestroy(&Cdensetest);
305: MatDestroy(&Pdense);
306: MatDestroy(&Adense);
307: }
309: /* Test MatDuplicate() of C=PtAP */
310: MatDuplicate(C,MAT_COPY_VALUES,&Cdup);
311: MatDestroy(&Cdup);
313: if (size>1) Test_MatRARt = PETSC_FALSE;
314: /* 4) Test MatRARt() */
315: /* ----------------- */
316: if (Test_MatRARt) {
317: Mat R, RARt;
318: PetscBool equal;
319: MatTranspose(P,MAT_INITIAL_MATRIX,&R);
320: MatRARt(A,R,MAT_INITIAL_MATRIX,2.0,&RARt);
321: MatEqual(C,RARt,&equal);
322: if (!equal) {
323: PetscReal norm;
324: MatAXPY(RARt,-1.0,C,DIFFERENT_NONZERO_PATTERN); /* RARt = -RARt + C */
325: MatNorm(RARt,NORM_FROBENIUS,&norm);
326: if (norm > 1.e-12) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"|PtAP - RARt| = %g",norm);
327: }
328: MatDestroy(&R);
329: MatDestroy(&RARt);
330: }
332: if (Test_MatMatMatMult && size == 1) {
333: Mat R, RAP;
334: PetscBool equal;
335: MatTranspose(P,MAT_INITIAL_MATRIX,&R);
336: MatMatMatMult(R,A,P,MAT_INITIAL_MATRIX,2.0,&RAP);
337: MatEqual(C,RAP,&equal);
338: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"PtAP != RAP");
339: MatDestroy(&R);
340: MatDestroy(&RAP);
341: }
343: /* Create vector x that is compatible with P */
344: VecCreate(PETSC_COMM_WORLD,&x);
345: MatGetLocalSize(P,&m,&n);
346: VecSetSizes(x,n,PETSC_DECIDE);
347: VecSetFromOptions(x);
349: VecCreate(PETSC_COMM_WORLD,&v3);
350: VecSetSizes(v3,n,PETSC_DECIDE);
351: VecSetFromOptions(v3);
352: VecDuplicate(v3,&v4);
354: norm = 0.0;
355: for (i=0; i<10; i++) {
356: VecSetRandom(x,rdm);
357: MatMult(P,x,v1);
358: MatMult(A,v1,v2); /* v2 = A*P*x */
360: MatMultTranspose(P,v2,v3); /* v3 = Pt*A*P*x */
361: MatMult(C,x,v4); /* v3 = C*x */
362: VecNorm(v4,NORM_2,&norm_abs);
363: VecAXPY(v4,none,v3);
364: VecNorm(v4,NORM_2,&norm_tmp);
366: norm_tmp /= norm_abs;
367: if (norm_tmp > norm) norm = norm_tmp;
368: }
369: if (norm >= tol) {
370: PetscPrintf(PETSC_COMM_SELF,"Error: MatPtAP(), |v1 - v2|: %g\n",(double)norm);
371: }
373: MatDestroy(&A);
374: MatDestroy(&P);
375: MatDestroy(&C);
376: VecDestroy(&v3);
377: VecDestroy(&v4);
378: VecDestroy(&x);
379: }
381: /* Destroy objects */
382: VecDestroy(&v1);
383: VecDestroy(&v2);
384: PetscRandomDestroy(&rdm);
385: PetscFree(idxn);
387: MatDestroy(&A_save);
388: MatDestroy(&B);
390: PetscPreLoadEnd();
391: PetscFinalize();
392: return 0;
393: }