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;
9: PetscInt m=50000,bs=12,i,j,k,l,row,col,M, its = 25;
10: PetscScalar rval,*vals;
11: PetscRandom rdm;
13: PetscInitialize(&argc,&args,(char*)0,help);
14: PetscOptionsGetInt(NULL,NULL,"-mat_block_size",&bs,NULL);
15: PetscOptionsGetInt(NULL,NULL,"-its",&its,NULL);
16: PetscOptionsGetInt(NULL,NULL,"-mat_size",&m,NULL);
17: M = m*bs;
18: MatCreateSeqBAIJ(PETSC_COMM_SELF,bs,M,M,27,NULL,&A);
19: MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
21: PetscRandomCreate(PETSC_COMM_SELF,&rdm);
22: PetscRandomSetFromOptions(rdm);
23: VecCreateSeq(PETSC_COMM_SELF,M,&x);
24: VecDuplicate(x,&y);
26: /* For each block row insert at most 27 blocks */
27: PetscMalloc1(bs*bs,&vals);
28: for (i=0; i<m; i++) {
29: row = i;
30: for (j=0; j<27; j++) {
31: PetscRandomGetValue(rdm,&rval);
32: col = (PetscInt)(PetscRealPart(rval)*m);
33: for (k=0; k<bs; k++) {
34: for (l=0; l<bs; l++) {
35: PetscRandomGetValue(rdm,&rval);
36: vals[k*bs + l] = rval;
37: }
38: }
39: MatSetValuesBlocked(A,1,&row,1,&col,vals,INSERT_VALUES);
40: }
41: }
42: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
43: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
44: PetscFree(vals);
46: /* Time MatMult(), MatMultAdd() */
47: for (i=0; i<its; i++) {
48: VecSetRandom(x,rdm);
49: MatMult(A,x,y);
50: VecSetRandom(x,rdm);
51: VecSetRandom(y,rdm);
52: MatMultAdd(A,x,y,y);
53: }
55: MatDestroy(&A);
56: VecDestroy(&x);
57: VecDestroy(&y);
58: PetscRandomDestroy(&rdm);
59: PetscFinalize();
60: return 0;
61: }
63: /*TEST
65: testset:
66: requires: defined(PETSC_USING_64BIT_PTR)
67: output_file: output/ex238_1.out
68: test:
69: suffix: 1
70: args: -mat_block_size 1 -mat_size 1000 -its 2
71: test:
72: suffix: 2
73: args: -mat_block_size 2 -mat_size 1000 -its 2
74: test:
75: suffix: 4
76: args: -mat_block_size 4 -mat_size 1000 -its 2
77: test:
78: suffix: 5
79: args: -mat_block_size 5 -mat_size 1000 -its 2
80: test:
81: suffix: 6
82: args: -mat_block_size 6 -mat_size 1000 -its 2
83: test:
84: suffix: 8
85: args: -mat_block_size 8 -mat_size 1000 -its 2
86: test:
87: suffix: 12
88: args: -mat_block_size 12 -mat_size 1000 -its 2
89: test:
90: suffix: 15
91: args: -mat_block_size 15 -mat_size 1000 -its 2
93: TEST*/