Actual source code: ex9.c
petsc-3.6.1 2015-08-06
1:
2: static char help[] = "Tests MPI parallel matrix creation. Test MatCreateRedundantMatrix() \n\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C,Credundant;
11: MatInfo info;
12: PetscMPIInt rank,size,subsize;
13: PetscInt i,j,m = 3,n = 2,low,high,iglobal;
14: PetscInt Ii,J,ldim,nsubcomms;
16: PetscBool flg_info,flg_mat;
17: PetscScalar v,one = 1.0;
18: Vec x,y;
19: MPI_Comm subcomm;
21: PetscInitialize(&argc,&args,(char*)0,help);
22: PetscOptionsGetInt(NULL,"-m",&m,NULL);
23: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
24: MPI_Comm_size(PETSC_COMM_WORLD,&size);
25: n = 2*size;
27: MatCreate(PETSC_COMM_WORLD,&C);
28: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
29: MatSetFromOptions(C);
30: MatSetUp(C);
32: /* Create the matrix for the five point stencil, YET AGAIN */
33: for (i=0; i<m; i++) {
34: for (j=2*rank; j<2*rank+2; j++) {
35: v = -1.0; Ii = j + n*i;
36: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
37: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
38: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
39: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
40: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
41: }
42: }
44: /* Add extra elements (to illustrate variants of MatGetInfo) */
45: Ii = n; J = n-2; v = 100.0;
46: MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
47: Ii = n-2; J = n; v = 100.0;
48: MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);
50: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
51: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
53: /* Form vectors */
54: MatCreateVecs(C,&x,&y);
55: VecGetLocalSize(x,&ldim);
56: VecGetOwnershipRange(x,&low,&high);
57: for (i=0; i<ldim; i++) {
58: iglobal = i + low;
59: v = one*((PetscReal)i) + 100.0*rank;
60: VecSetValues(x,1,&iglobal,&v,INSERT_VALUES);
61: }
62: VecAssemblyBegin(x);
63: VecAssemblyEnd(x);
65: MatMult(C,x,y);
66: /*
67: VecView(x,PETSC_VIEWER_STDOUT_WORLD);
68: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
69: */
70: PetscOptionsHasName(NULL,"-view_info",&flg_info);
71: if (flg_info) {
72: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
73: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
74:
75: MatGetInfo(C,MAT_GLOBAL_SUM,&info);
76: PetscPrintf(PETSC_COMM_WORLD,"matrix information (global sums):\n\
77: nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
78: MatGetInfo (C,MAT_GLOBAL_MAX,&info);
79: PetscPrintf(PETSC_COMM_WORLD,"matrix information (global max):\n\
80: nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
81: }
82:
83: PetscOptionsHasName(NULL,"-view_mat",&flg_mat);
84: if (flg_mat) {
85: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
86: }
88: /* Test MatCreateRedundantMatrix() */
89: nsubcomms = size;
90: PetscOptionsGetInt(NULL,"-nsubcomms",&nsubcomms,NULL);
91: MatCreateRedundantMatrix(C,nsubcomms,MPI_COMM_NULL,MAT_INITIAL_MATRIX,&Credundant);
92: MatCreateRedundantMatrix(C,nsubcomms,MPI_COMM_NULL,MAT_REUSE_MATRIX,&Credundant);
94: PetscObjectGetComm((PetscObject)Credundant,&subcomm);
95: MPI_Comm_size(subcomm,&subsize);
96:
97: if (subsize==1 && flg_mat) {
98: printf("\n[%d] Credundant:\n",rank);
99: MatView(Credundant,PETSC_VIEWER_STDOUT_SELF);
100: }
101: MatDestroy(&Credundant);
102:
103: /* Test MatCreateRedundantMatrix() with user-provided subcomm */
104: {
105: PetscSubcomm psubcomm;
107: PetscSubcommCreate(PETSC_COMM_WORLD,&psubcomm);
108: PetscSubcommSetNumber(psubcomm,nsubcomms);
109: PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
110: /* enable runtime switch of psubcomm type, e.g., '-psubcomm_type interlaced */
111: PetscSubcommSetFromOptions(psubcomm);
113: MatCreateRedundantMatrix(C,nsubcomms,PetscSubcommChild(psubcomm),MAT_INITIAL_MATRIX,&Credundant);
114: MatCreateRedundantMatrix(C,nsubcomms,PetscSubcommChild(psubcomm),MAT_REUSE_MATRIX,&Credundant);
116: PetscSubcommDestroy(&psubcomm);
117: MatDestroy(&Credundant);
118: }
120: VecDestroy(&x);
121: VecDestroy(&y);
122: MatDestroy(&C);
123: PetscFinalize();
124: return 0;
125: }