Actual source code: ex21.c
2: static char help[] = "Tests converting a parallel AIJ formatted matrix to the parallel Row format.\n\
3: This also tests MatGetRow() and MatRestoreRow() for the parallel case.\n\n";
5: #include petscmat.h
9: int main(int argc,char **args)
10: {
11: Mat C,A;
12: PetscInt i,j,m = 3,n = 2,Ii,J,rstart,rend,nz;
13: PetscMPIInt rank,size;
14: PetscErrorCode ierr;
15: const PetscInt *idx;
16: PetscScalar v;
17: const PetscScalar *values;
19: PetscInitialize(&argc,&args,(char *)0,help);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
21: MPI_Comm_size(PETSC_COMM_WORLD,&size);
22: n = 2*size;
24: /* create the matrix for the five point stencil, YET AGAIN*/
25: MatCreate(PETSC_COMM_WORLD,&C);
26: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
27: MatSetFromOptions(C);
28: MatMPIAIJSetPreallocation(C,5,PETSC_NULL,5,PETSC_NULL);
29: MatSeqAIJSetPreallocation(C,5,PETSC_NULL);
30: for (i=0; i<m; i++) {
31: for (j=2*rank; j<2*rank+2; j++) {
32: v = -1.0; Ii = j + n*i;
33: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
34: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
35: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
36: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
37: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
38: }
39: }
40: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
41: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
42: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
43: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
44: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
45: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
47: MatGetOwnershipRange(C,&rstart,&rend);
48: for (i=rstart; i<rend; i++) {
49: MatGetRow(C,i,&nz,&idx,&values);
50: PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"[%d] get row %D: ",rank,i);
51: for (j=0; j<nz; j++) {
52: #if defined(PETSC_USE_COMPLEX)
53: PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"%D %G ",idx[j],PetscRealPart(values[j]));
54: #else
55: PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"%D %G ",idx[j],values[j]);
56: #endif
57: }
58: PetscSynchronizedFPrintf(PETSC_COMM_WORLD,stdout,"\n");
59: MatRestoreRow(C,i,&nz,&idx,&values);
60: }
61: PetscSynchronizedFlush(PETSC_COMM_WORLD);
63: MatConvert(C,MATSAME,MAT_INITIAL_MATRIX,&A);
64: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
65: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
66: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
67: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
69: MatDestroy(A);
70: MatDestroy(C);
71: PetscFinalize();
72: return 0;
73: }