Actual source code: ex114.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests MatGetRowMax(), MatGetRowMin(), MatGetRowMaxAbs()\n";
4: #include <petscmat.h>
6: #define M 5
7: #define N 6
11: int main(int argc,char **args)
12: {
13: Mat A;
14: Vec min,max,maxabs;
15: PetscInt m,n;
16: PetscInt imin[M],imax[M],imaxabs[M],indices[N],row;
17: PetscScalar values[N];
19: MatType type;
20: PetscMPIInt size;
21: PetscBool doTest=PETSC_TRUE;
23: PetscInitialize(&argc,&args,(char*)0,help);
24: MPI_Comm_size(PETSC_COMM_WORLD,&size);
26: MatCreate(PETSC_COMM_WORLD,&A);
27: MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,M,N);
28: MatSetFromOptions(A);
29: MatSetUp(A);
30: row = 0;
32: indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 3; indices[4] = 4; indices[5] = 5;
33: values[0] = -1.0; values[1] = 0.0; values[2] = 1.0; values[3] = 3.0; values[4] = 4.0; values[5] = -5.0;
35: MatSetValues(A,1,&row,6,indices,values,INSERT_VALUES);
36: row = 1;
37: MatSetValues(A,1,&row,3,indices,values,INSERT_VALUES);
38: row = 4;
39: MatSetValues(A,1,&row,1,indices+4,values+4,INSERT_VALUES);
40: row = 4;
41: MatSetValues(A,1,&row,2,indices+4,values+4,INSERT_VALUES);
42: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
43: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
44: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
46: MatGetLocalSize(A, &m,&n);
47: VecCreate(PETSC_COMM_WORLD,&min);
48: VecSetSizes(min,m,PETSC_DECIDE);
49: VecSetFromOptions(min);
50: VecDuplicate(min,&max);
51: VecDuplicate(min,&maxabs);
53: /* Test MatGetRowMin, MatGetRowMax and MatGetRowMaxAbs */
54: if (size == 1) {
55: MatGetRowMin(A,min,imin);
56: MatGetRowMax(A,max,imax);
57: MatGetRowMaxAbs(A,maxabs,imaxabs);
59: PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");
60: VecView(min,PETSC_VIEWER_STDOUT_WORLD);
61: PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);
62: PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");
63: VecView(max,PETSC_VIEWER_STDOUT_WORLD);
64: PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);
65: PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");
66: VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
67: PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);
69: } else {
70: MatGetType(A,&type);
71: PetscPrintf(PETSC_COMM_WORLD,"\nMatrix type: %s\n",type);
72: /* AIJ */
73: PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&doTest);
74: if (doTest) {
75: MatGetRowMaxAbs(A,maxabs,NULL);
76: MatGetRowMaxAbs(A,maxabs,imaxabs);
77: PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");
78: VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
79: }
80: /* BAIJ */
81: PetscObjectTypeCompare((PetscObject)A,MATMPIBAIJ,&doTest);
82: if (doTest) {
83: MatGetRowMaxAbs(A,maxabs,NULL);
84: MatGetRowMaxAbs(A,maxabs,imaxabs);
85: PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");
86: VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
87: }
88: }
90: if (size == 1) {
91: MatConvert(A,MATDENSE,MAT_INPLACE_MATRIX,&A);
93: MatGetRowMin(A,min,imin);
94: MatGetRowMax(A,max,imax);
95: MatGetRowMaxAbs(A,maxabs,imaxabs);
97: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
98: PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");
99: VecView(min,PETSC_VIEWER_STDOUT_WORLD);
100: PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);
101: PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");
102: VecView(max,PETSC_VIEWER_STDOUT_WORLD);
103: PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);
104: PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");
105: VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
106: PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);
107: }
109: VecDestroy(&min);
110: VecDestroy(&max);
111: VecDestroy(&maxabs);
112: MatDestroy(&A);
113: PetscFinalize();
114: return 0;
115: }