Actual source code: ex27.c
petsc-3.13.6 2020-09-29
1: static char help[]= "Test MatSetRandom on MATMPIAIJ matrices\n\n";
3: /*
4: Adapted from an example Contributed-by: Jakub Kruzik <jakub.kruzik@vsb.cz>
5: */
6: #include <petscmat.h>
7: int main(int argc,char **args)
8: {
9: Mat A[2];
11: PetscReal nrm,tol=10*PETSC_SMALL;
12: PetscRandom rctx;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
17: /* Call MatSetRandom on unassembled matrices */
18: MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,20,20,3,NULL,3,NULL,&A[0]);
19: MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,20,20,3,NULL,3,NULL,&A[1]);
20: MatSetRandom(A[0],rctx);
21: MatSetRandom(A[1],rctx);
23: MatAXPY(A[0],1.0,A[1],DIFFERENT_NONZERO_PATTERN);
24: MatAXPY(A[0],-1.0,A[0],SAME_NONZERO_PATTERN);
25: MatNorm(A[0],NORM_1,&nrm);
26: if (nrm > tol) {PetscPrintf(PETSC_COMM_WORLD,"Error: MatNorm(), norm1=: %g\n",(double)nrm);}
28: /* Call MatSetRandom on assembled matrices */
29: MatSetRandom(A[0],rctx);
30: MatSetRandom(A[1],rctx);
32: MatAXPY(A[0],1.0,A[1],DIFFERENT_NONZERO_PATTERN);
33: MatAXPY(A[0],-1.0,A[0],SAME_NONZERO_PATTERN);
34: MatNorm(A[0],NORM_1,&nrm);
35: if (nrm > tol) {PetscPrintf(PETSC_COMM_WORLD,"Error: MatNorm(), norm1=: %g\n",(double)nrm);}
37: MatDestroy(&A[0]);
38: MatDestroy(&A[1]);
39: PetscRandomDestroy(&rctx);
40: PetscFinalize();
41: return ierr;
42: }
44: /*TEST
45: test:
46: nsize: 3
47: TEST*/