Actual source code: ex122.c
petsc-3.7.3 2016-08-01
1: static char help[] = "Test MatMatMult() for AIJ and Dense matrices.\n\n";
3: #include <petscmat.h>
7: int main(int argc,char **argv)
8: {
9: Mat A,B,C,D;
10: PetscInt i,M=10,N=5;
12: PetscRandom r;
13: PetscBool equal=PETSC_FALSE;
14: PetscReal fill = 1.0;
15: PetscInt nza,am,an;
17: PetscInitialize(&argc,&argv,(char*)0,help);
18: PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
19: PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
20: PetscOptionsGetReal(NULL,NULL,"-fill",&fill,NULL);
22: PetscRandomCreate(PETSC_COMM_WORLD,&r);
23: PetscRandomSetFromOptions(r);
25: /* create a aij matrix A */
26: MatCreate(PETSC_COMM_WORLD,&A);
27: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,M);
28: MatSetType(A,MATAIJ);
29: nza = (PetscInt)(.3*M); /* num of nozeros in each row of A */
30: MatSeqAIJSetPreallocation(A,nza,NULL);
31: MatMPIAIJSetPreallocation(A,nza,NULL,nza,NULL);
32: MatSetRandom(A,r);
34: /* create a dense matrix B */
35: MatGetLocalSize(A,&am,&an);
36: MatCreate(PETSC_COMM_WORLD,&B);
37: MatSetSizes(B,PETSC_DECIDE,am,N,PETSC_DECIDE);
38: MatSetType(B,MATDENSE);
39: MatSeqDenseSetPreallocation(B,NULL);
40: MatMPIDenseSetPreallocation(B,NULL);
41: MatSetRandom(B,r);
42: PetscRandomDestroy(&r);
43: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
44: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
47: /* Test MatMatMult() */
48: MatMatMult(B,A,MAT_INITIAL_MATRIX,fill,&C);
50: MatMatMultSymbolic(B,A,fill,&D);
51: for (i=0; i<2; i++) {
52: MatMatMultNumeric(B,A,D);
53: }
54: MatEqual(C,D,&equal);
55: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"C != D");
57: MatDestroy(&D);
58: MatDestroy(&C);
59: MatDestroy(&B);
60: MatDestroy(&A);
61: PetscFinalize();
62: return(0);
63: }