Initialization of matrix A
55: /* When using MatCreate(), the matrix format can be specified at runtime.
Also, the parallel partitioning of the matrix is determined by PETSc at runtime.
Performance tuning note: For problems of substantial size, preallocation of matrix memory is crucial for
attaining good performance. Since preallocation is not possible via the generic matrix creation routine
MatCreate(), we recommend for practical problems instead to use the creation routine for a particular
matrix format, e.g., MatCreateMPIAIJ() – parallel AIJ (compressed sparse row)
MatCreateMPIBAIJ() – parallel block AIJ
See the matrix chapter of the users manual for details. */
69: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&A);
70: MatSetFromOptions(A);
73: /* Currently, all PETSc parallel matrix formats are partitioned by contiguous chunks of rows across the processors. Determine which rows of the matrix are locally owned. */
77: MatGetOwnershipRange(A,&Istart,&Iend);
92: for (I=Istart; I<Iend; I++) {
93: v = –1.0; i = I/n; j = I – i*n;
94: if (iɬ) {J = I – n; MatSetValues(A, 1,&I, 1,&J, &v, INSERT_VALUES);}
95: if (i<m–1) {J = I + n; MatSetValues(A, 1,&I, 1,&J, &v, INSERT_VALUES);}
96: if (jɬ) {J = I – 1; MatSetValues(A, 1,&I, 1,&J, &v, INSERT_VALUES);}
97: if (j<n–1) {J = I + 1; MatSetValues(A, 1,&I, 1,&J, &v, INSERT_VALUES);}
98: v = 4.0; MatSetValues(A,1,&I,1,&I,&v,INSERT_VALUES);
107: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
108: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);