Actual source code: ex238.c
1: static char help[] = "Creates MatSeqBAIJ matrix of given BS for timing tests of MatMult().\n";
3: #include <petscmat.h>
5: int main(int argc,char **args)
6: {
7: Mat A;
8: Vec x,y;
10: PetscInt m=50000,bs=12,i,j,k,l,row,col,M, its = 25;
11: PetscScalar rval,*vals;
12: PetscRandom rdm;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);
16: PetscOptionsGetInt(NULL,NULL,"-its",&its,NULL);
17: PetscOptionsGetInt(NULL,NULL,"-mat_size",&m,NULL);
18: M = m*bs;
19: MatCreateSeqBAIJ(PETSC_COMM_SELF,bs,M,M,27,NULL,&A);
20: MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
22: PetscRandomCreate(PETSC_COMM_SELF,&rdm);
23: PetscRandomSetFromOptions(rdm);
24: VecCreateSeq(PETSC_COMM_SELF,M,&x);
25: VecDuplicate(x,&y);
27: /* For each block row insert at most 27 blocks */
28: PetscMalloc1(bs*bs,&vals);
29: for (i=0; i<m; i++) {
30: row = i;
31: for (j=0; j<27; j++) {
32: PetscRandomGetValue(rdm,&rval);
33: col = (PetscInt)(PetscRealPart(rval)*m);
34: for (k=0; k<bs; k++) {
35: for (l=0; l<bs; l++) {
36: PetscRandomGetValue(rdm,&rval);
37: vals[k*bs + l] = rval;
38: }
39: }
40: MatSetValuesBlocked(A,1,&row,1,&col,vals,INSERT_VALUES);
41: }
42: }
43: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
44: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
45: PetscFree(vals);
47: /* Time MatMult(), MatMultAdd() */
48: for (i=0; i<its; i++) {
49: VecSetRandom(x,rdm);
50: MatMult(A,x,y);
51: VecSetRandom(x,rdm);
52: VecSetRandom(y,rdm);
53: MatMultAdd(A,x,y,y);
54: }
56: MatDestroy(&A);
57: VecDestroy(&x);
58: VecDestroy(&y);
59: PetscRandomDestroy(&rdm);
60: PetscFinalize();
61: return ierr;
62: }
64: /*TEST
66: testset:
67: requires: defined(PETSC_USING_64BIT_PTR)
68: output_file: output/ex238_1.out
69: test:
70: suffix: 1
71: args: -mat_block_size 1 -mat_size 1000 -its 2
72: test:
73: suffix: 2
74: args: -mat_block_size 2 -mat_size 1000 -its 2
75: test:
76: suffix: 4
77: args: -mat_block_size 4 -mat_size 1000 -its 2
78: test:
79: suffix: 5
80: args: -mat_block_size 5 -mat_size 1000 -its 2
81: test:
82: suffix: 6
83: args: -mat_block_size 6 -mat_size 1000 -its 2
84: test:
85: suffix: 8
86: args: -mat_block_size 8 -mat_size 1000 -its 2
87: test:
88: suffix: 12
89: args: -mat_block_size 12 -mat_size 1000 -its 2
90: test:
91: suffix: 15
92: args: -mat_block_size 15 -mat_size 1000 -its 2
94: TEST*/