Actual source code: ex108.c
1: static char help[] = "Testing MatCreateSeqBAIJWithArrays() and MatCreateSeqSBAIJWithArrays().\n\n";
3: #include <petscmat.h>
5: int main(int argc,char **argv)
6: {
7: Mat A,B,As;
8: const PetscInt *ai,*aj;
9: PetscInt i,j,k,nz,n,asi[]={0,2,3,4,6,7};
10: PetscInt asj[]={0,4,1,2,3,4,4};
11: PetscScalar asa[7],*aa;
12: PetscRandom rctx;
13: PetscMPIInt size;
14: PetscBool flg;
16: PetscInitialize(&argc,&argv,(char*)0,help);
17: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: /* Create a aij matrix for checking */
21: MatCreateSeqAIJ(PETSC_COMM_SELF,5,5,2,NULL,&A);
22: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
23: PetscRandomSetFromOptions(rctx);
25: k = 0;
26: for (i=0; i<5; i++) {
27: nz = asi[i+1] - asi[i]; /* length of i_th row of A */
28: for (j=0; j<nz; j++) {
29: PetscRandomGetValue(rctx,&asa[k]);
30: MatSetValues(A,1,&i,1,&asj[k],&asa[k],INSERT_VALUES);
31: MatSetValues(A,1,&i,1,&asj[k],&asa[k],INSERT_VALUES);
32: if (i != asj[k]) { /* insert symmetric entry */
33: MatSetValues(A,1,&asj[k],1,&i,&asa[k],INSERT_VALUES);
34: }
35: k++;
36: }
37: }
38: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
39: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
41: /* Create a baij matrix using MatCreateSeqBAIJWithArrays() */
42: MatGetRowIJ(A,0,PETSC_FALSE,PETSC_FALSE,&n,&ai,&aj,&flg);
43: MatSeqAIJGetArray(A,&aa);
44: /* WARNING: This sharing is dangerous if either A or B is later assembled */
45: MatCreateSeqBAIJWithArrays(PETSC_COMM_SELF,1,5,5,(PetscInt*)ai,(PetscInt*)aj,aa,&B);
46: MatSeqAIJRestoreArray(A,&aa);
47: MatRestoreRowIJ(A,0,PETSC_FALSE,PETSC_FALSE,&n,&ai,&aj,&flg);
48: MatMultEqual(A,B,10,&flg);
51: /* Create a sbaij matrix using MatCreateSeqSBAIJWithArrays() */
52: MatCreateSeqSBAIJWithArrays(PETSC_COMM_SELF,1,5,5,asi,asj,asa,&As);
53: MatMultEqual(A,As,10,&flg);
56: /* Free spaces */
57: PetscRandomDestroy(&rctx);
58: MatDestroy(&A);
59: MatDestroy(&B);
60: MatDestroy(&As);
61: PetscFinalize();
62: return 0;
63: }