Actual source code: ex9.c
petsc-3.5.4 2015-05-23
1:
2: static char help[] = "Tests MPI parallel matrix creation. Test MatGetRedundantMatrix() \n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C;
11: MatInfo info;
12: PetscMPIInt rank,size;
13: PetscInt i,j,m = 3,n = 2,low,high,iglobal;
14: PetscInt Ii,J,ldim;
16: PetscBool flg_info,flg_mat;
17: PetscScalar v,one = 1.0;
18: Vec x,y;
20: PetscInitialize(&argc,&args,(char*)0,help);
21: PetscOptionsGetInt(NULL,"-m",&m,NULL);
22: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
23: MPI_Comm_size(PETSC_COMM_WORLD,&size);
24: n = 2*size;
26: MatCreate(PETSC_COMM_WORLD,&C);
27: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
28: MatSetFromOptions(C);
29: MatSetUp(C);
31: /* Create the matrix for the five point stencil, YET AGAIN */
32: for (i=0; i<m; i++) {
33: for (j=2*rank; j<2*rank+2; j++) {
34: v = -1.0; Ii = j + n*i;
35: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
36: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
37: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
38: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
39: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
40: }
41: }
43: /* Add extra elements (to illustrate variants of MatGetInfo) */
44: Ii = n; J = n-2; v = 100.0;
45: MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
46: Ii = n-2; J = n; v = 100.0;
47: MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
49: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
50: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
52: /* Form vectors */
53: MatGetVecs(C,&x,&y);
54: VecGetLocalSize(x,&ldim);
55: VecGetOwnershipRange(x,&low,&high);
56: for (i=0; i<ldim; i++) {
57: iglobal = i + low;
58: v = one*((PetscReal)i) + 100.0*rank;
59: VecSetValues(x,1,&iglobal,&v,INSERT_VALUES);
60: }
61: VecAssemblyBegin(x);
62: VecAssemblyEnd(x);
64: MatMult(C,x,y);
65: /*
66: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
67: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
68: */
69: PetscOptionsHasName(NULL,"-view_info",&flg_info);
70: if (flg_info) {
71: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
72: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
73:
74: MatGetInfo(C,MAT_GLOBAL_SUM,&info);
75: PetscPrintf(PETSC_COMM_WORLD,"matrix information (global sums):\n\
76: nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
77: MatGetInfo (C,MAT_GLOBAL_MAX,&info);
78: PetscPrintf(PETSC_COMM_WORLD,"matrix information (global max):\n\
79: nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
80: }
81:
82: PetscOptionsHasName(NULL,"-view_mat",&flg_mat);
83: if (flg_mat) {
84: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
85: }
87: /* Test MatGetRedundantMatrix() */
88: if (size > 1) {
89: MPI_Comm subcomm;
90: Mat Credundant;
91: PetscInt Nsubcomms = size;
92: PetscMPIInt nsubcomms,subsize;
94: PetscOptionsGetInt(NULL,"-nsubcomms",&Nsubcomms,NULL);
95: PetscMPIIntCast(Nsubcomms,&nsubcomms);
96: if (nsubcomms > size) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"nsubcomms %d cannot > ncomms %d",nsubcomms,size);
98: MatGetRedundantMatrix(C,nsubcomms,MPI_COMM_NULL,MAT_INITIAL_MATRIX,&Credundant);
99: MatGetRedundantMatrix(C,nsubcomms,MPI_COMM_NULL,MAT_REUSE_MATRIX,&Credundant);
101: PetscObjectGetComm((PetscObject)Credundant,&subcomm);
102: MPI_Comm_size(subcomm,&subsize);
103:
104: if (subsize==1 && flg_mat) {
105: printf("\n[%d] Credundant:\n",rank);
106: MatView(Credundant,PETSC_VIEWER_STDOUT_SELF);
107: }
108: MatDestroy(&Credundant);
109: }
111: VecDestroy(&x);
112: VecDestroy(&y);
113: MatDestroy(&C);
114: PetscFinalize();
115: return 0;
116: }