Actual source code: ex16.c
1: /*$Id: ex16.c,v 1.17 2001/08/07 03:03:07 balay Exp $*/
3: static char help[] = "Tests MatGetArray().nn";
5: #include petscmat.h
7: int main(int argc,char **args)
8: {
9: Mat A;
10: int i,j,m = 3,n = 2,ierr,rstart,rend;
11: PetscScalar v,*array;
13: PetscInitialize(&argc,&args,(char *)0,help);
15: /*
16: Create a parallel dense matrix shared by all processors
17: */
18: MatCreateMPIDense(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m,n,PETSC_NULL,&A);
19:
21: /*
22: Set values into the matrix
23: */
24: for (i=0; i<m; i++) {
25: for (j=0; j<n; j++) {
26: v = 9.0/(i+j+1); MatSetValues(A,1,&i,1,&j,&v,INSERT_VALUES);
27: }
28: }
29: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
30: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
32: /*
33: Print the matrix to the screen
34: */
35: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
38: /*
39: Print the local portion of the matrix to the screen
40: */
41: MatGetArray(A,&array);
42: MatGetOwnershipRange(A,&rstart,&rend);
43: for (i=rstart; i<rend; i++) {
44: for (j=0; j<n; j++) {
45: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"%6.4e ",PetscRealPart(array[j*(rend-rstart)+i-rstart]));
46: }
47: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"n");
48: }
49: PetscSynchronizedFlush(PETSC_COMM_WORLD);
50: MatRestoreArray(A,&array);
52: /*
53: Free the space used by the matrix
54: */
55: MatDestroy(A);
56: PetscFinalize();
57: return 0;
58: }