Actual source code: ex51.c
2: static char help[] = "Tests MatIncreaseOverlap(), MatCreateSubMatrices() for MatBAIJ format.\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat A,B,*submatA,*submatB;
9: PetscInt bs=1,m=43,ov=1,i,j,k,*rows,*cols,M,nd=5,*idx,mm,nn,lsize;
11: PetscScalar *vals,rval;
12: IS *is1,*is2;
13: PetscRandom rdm;
14: Vec xx,s1,s2;
15: PetscReal s1norm,s2norm,rnorm,tol = PETSC_SQRT_MACHINE_EPSILON;
16: PetscBool flg;
18: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
19: PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);
20: PetscOptionsGetInt(NULL,NULL,"-mat_size",&m,NULL);
21: PetscOptionsGetInt(NULL,NULL,"-ov",&ov,NULL);
22: PetscOptionsGetInt(NULL,NULL,"-nd",&nd,NULL);
23: M = m*bs;
25: MatCreateSeqBAIJ(PETSC_COMM_SELF,bs,M,M,1,NULL,&A);
26: MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
27: MatCreateSeqAIJ(PETSC_COMM_SELF,M,M,15,NULL,&B);
28: MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
29: PetscRandomCreate(PETSC_COMM_SELF,&rdm);
30: PetscRandomSetFromOptions(rdm);
32: PetscMalloc1(bs,&rows);
33: PetscMalloc1(bs,&cols);
34: PetscMalloc1(bs*bs,&vals);
35: PetscMalloc1(M,&idx);
37: /* Now set blocks of values */
38: for (i=0; i<20*bs; i++) {
39: PetscRandomGetValue(rdm,&rval);
40: cols[0] = bs*(int)(PetscRealPart(rval)*m);
41: PetscRandomGetValue(rdm,&rval);
42: rows[0] = bs*(int)(PetscRealPart(rval)*m);
43: for (j=1; j<bs; j++) {
44: rows[j] = rows[j-1]+1;
45: cols[j] = cols[j-1]+1;
46: }
48: for (j=0; j<bs*bs; j++) {
49: PetscRandomGetValue(rdm,&rval);
50: vals[j] = rval;
51: }
52: MatSetValues(A,bs,rows,bs,cols,vals,ADD_VALUES);
53: MatSetValues(B,bs,rows,bs,cols,vals,ADD_VALUES);
54: }
56: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
57: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
58: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
59: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
61: /* Test MatIncreaseOverlap() */
62: PetscMalloc1(nd,&is1);
63: PetscMalloc1(nd,&is2);
65: for (i=0; i<nd; i++) {
66: PetscRandomGetValue(rdm,&rval);
67: lsize = (int)(PetscRealPart(rval)*m);
68: for (j=0; j<lsize; j++) {
69: PetscRandomGetValue(rdm,&rval);
70: idx[j*bs] = bs*(int)(PetscRealPart(rval)*m);
71: for (k=1; k<bs; k++) idx[j*bs+k] = idx[j*bs]+k;
72: }
73: ISCreateGeneral(PETSC_COMM_SELF,lsize*bs,idx,PETSC_COPY_VALUES,is1+i);
74: ISCreateGeneral(PETSC_COMM_SELF,lsize*bs,idx,PETSC_COPY_VALUES,is2+i);
75: }
76: MatIncreaseOverlap(A,nd,is1,ov);
77: MatIncreaseOverlap(B,nd,is2,ov);
79: for (i=0; i<nd; ++i) {
80: ISEqual(is1[i],is2[i],&flg);
81: if (!flg) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"i=%D, flg =%d\n",i,(int)flg);
82: }
84: for (i=0; i<nd; ++i) {
85: ISSort(is1[i]);
86: ISSort(is2[i]);
87: }
89: MatCreateSubMatrices(A,nd,is1,is1,MAT_INITIAL_MATRIX,&submatA);
90: MatCreateSubMatrices(B,nd,is2,is2,MAT_INITIAL_MATRIX,&submatB);
92: /* Test MatMult() */
93: for (i=0; i<nd; i++) {
94: MatGetSize(submatA[i],&mm,&nn);
95: VecCreateSeq(PETSC_COMM_SELF,mm,&xx);
96: VecDuplicate(xx,&s1);
97: VecDuplicate(xx,&s2);
98: for (j=0; j<3; j++) {
99: VecSetRandom(xx,rdm);
100: MatMult(submatA[i],xx,s1);
101: MatMult(submatB[i],xx,s2);
102: VecNorm(s1,NORM_2,&s1norm);
103: VecNorm(s2,NORM_2,&s2norm);
104: rnorm = s2norm-s1norm;
105: if (rnorm<-tol || rnorm>tol) {
106: PetscPrintf(PETSC_COMM_SELF,"Error:MatMult - Norm1=%16.14e Norm2=%16.14e\n",s1norm,s2norm);
107: }
108: }
109: VecDestroy(&xx);
110: VecDestroy(&s1);
111: VecDestroy(&s2);
112: }
113: /* Now test MatCreateSubmatrices with MAT_REUSE_MATRIX option */
114: MatCreateSubMatrices(A,nd,is1,is1,MAT_REUSE_MATRIX,&submatA);
115: MatCreateSubMatrices(B,nd,is2,is2,MAT_REUSE_MATRIX,&submatB);
117: /* Test MatMult() */
118: for (i=0; i<nd; i++) {
119: MatGetSize(submatA[i],&mm,&nn);
120: VecCreateSeq(PETSC_COMM_SELF,mm,&xx);
121: VecDuplicate(xx,&s1);
122: VecDuplicate(xx,&s2);
123: for (j=0; j<3; j++) {
124: VecSetRandom(xx,rdm);
125: MatMult(submatA[i],xx,s1);
126: MatMult(submatB[i],xx,s2);
127: VecNorm(s1,NORM_2,&s1norm);
128: VecNorm(s2,NORM_2,&s2norm);
129: rnorm = s2norm-s1norm;
130: if (rnorm<-tol || rnorm>tol) {
131: PetscPrintf(PETSC_COMM_SELF,"Error:MatMult - Norm1=%16.14e Norm2=%16.14e\n",s1norm,s2norm);
132: }
133: }
134: VecDestroy(&xx);
135: VecDestroy(&s1);
136: VecDestroy(&s2);
137: }
139: /* Free allocated memory */
140: for (i=0; i<nd; ++i) {
141: ISDestroy(&is1[i]);
142: ISDestroy(&is2[i]);
143: }
144: MatDestroySubMatrices(nd,&submatA);
145: MatDestroySubMatrices(nd,&submatB);
146: PetscFree(is1);
147: PetscFree(is2);
148: PetscFree(idx);
149: PetscFree(rows);
150: PetscFree(cols);
151: PetscFree(vals);
152: MatDestroy(&A);
153: MatDestroy(&B);
154: PetscRandomDestroy(&rdm);
155: PetscFinalize();
156: return ierr;
157: }
159: /*TEST
161: test:
162: args: -mat_block_size {{1 2 5 7 8}} -ov {{1 3}} -mat_size {{11 13}} -nd {{7}}
164: TEST*/