1 static char help[]= "Test MatSetRandom on MATMPIAIJ matrices\n\n"; 2 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]; 10 PetscReal nrm,tol=10*PETSC_SMALL; 11 PetscRandom rctx; 12 13 PetscFunctionBeginUser; 14 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 15 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD,&rctx)); 16 17 /* Call MatSetRandom on unassembled matrices */ 18 PetscCall(MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,20,20,3,NULL,3,NULL,&A[0])); 19 PetscCall(MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,20,20,3,NULL,3,NULL,&A[1])); 20 PetscCall(MatSetRandom(A[0],rctx)); 21 PetscCall(MatSetRandom(A[1],rctx)); 22 23 PetscCall(MatAXPY(A[0],1.0,A[1],DIFFERENT_NONZERO_PATTERN)); 24 PetscCall(MatAXPY(A[0],-1.0,A[0],SAME_NONZERO_PATTERN)); 25 PetscCall(MatNorm(A[0],NORM_1,&nrm)); 26 if (nrm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error: MatNorm(), norm1=: %g\n",(double)nrm)); 27 28 /* Call MatSetRandom on assembled matrices */ 29 PetscCall(MatSetRandom(A[0],rctx)); 30 PetscCall(MatSetRandom(A[1],rctx)); 31 32 PetscCall(MatAXPY(A[0],1.0,A[1],DIFFERENT_NONZERO_PATTERN)); 33 PetscCall(MatAXPY(A[0],-1.0,A[0],SAME_NONZERO_PATTERN)); 34 PetscCall(MatNorm(A[0],NORM_1,&nrm)); 35 if (nrm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Error: MatNorm(), norm1=: %g\n",(double)nrm)); 36 37 PetscCall(MatDestroy(&A[0])); 38 PetscCall(MatDestroy(&A[1])); 39 PetscCall(PetscRandomDestroy(&rctx)); 40 PetscCall(PetscFinalize()); 41 return 0; 42 } 43 44 /*TEST 45 test: 46 nsize: 3 47 TEST*/ 48