Actual source code: ex14.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests sequential and parallel MatGetRow() and MatRestoreRow().\n";
4: #include <petscmat.h>
8: int main(int argc,char **args)
9: {
10: Mat C;
11: PetscInt i,j,m = 5,n = 5,Ii,J,nz,rstart,rend;
12: PetscErrorCode ierr;
13: PetscMPIInt rank;
14: const PetscInt *idx;
15: PetscScalar v;
16: const PetscScalar *values;
18: PetscInitialize(&argc,&args,(char*)0,help);
20: /* Create the matrix for the five point stencil, YET AGAIN */
21: MatCreate(PETSC_COMM_WORLD,&C);
22: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
23: MatSetFromOptions(C);
24: MatSetUp(C);
25: for (i=0; i<m; i++) {
26: for (j=0; j<n; j++) {
27: v = -1.0; Ii = j + n*i;
28: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
29: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
30: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
31: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
32: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
33: }
34: }
35: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
36: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
37: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
39: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
40: MatGetOwnershipRange(C,&rstart,&rend);
41: for (i=rstart; i<rend; i++) {
42: MatGetRow(C,i,&nz,&idx,&values);
43: if (!rank) {
44: for (j=0; j<nz; j++) {
45: PetscPrintf(PETSC_COMM_SELF,"%D %g ",idx[j],(double)PetscRealPart(values[j]));
46: }
47: PetscPrintf(PETSC_COMM_SELF,"\n");
48: }
49: MatRestoreRow(C,i,&nz,&idx,&values);
50: }
52: MatDestroy(&C);
53: PetscFinalize();
54: return 0;
55: }