xref: /petsc/src/mat/tests/ex49.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests MatTranspose(), MatNorm(), and MatAXPY().\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **argv)
7 {
8   Mat            mat,tmat = 0;
9   PetscInt       m = 4,n,i,j;
10   PetscMPIInt    size,rank;
11   PetscInt       rstart,rend,rect = 0;
12   PetscBool      flg;
13   PetscScalar    v;
14   PetscReal      normf,normi,norm1;
15   MatInfo        info;
16 
17   PetscFunctionBeginUser;
18   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
19   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
20   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
21   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
22   n    = m;
23   PetscCall(PetscOptionsHasName(NULL,NULL,"-rect1",&flg));
24   if (flg) {n += 2; rect = 1;}
25   PetscCall(PetscOptionsHasName(NULL,NULL,"-rect2",&flg));
26   if (flg) {n -= 2; rect = 1;}
27 
28   /* Create and assemble matrix */
29   PetscCall(MatCreate(PETSC_COMM_WORLD,&mat));
30   PetscCall(MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,m,n));
31   PetscCall(MatSetFromOptions(mat));
32   PetscCall(MatSetUp(mat));
33   PetscCall(MatGetOwnershipRange(mat,&rstart,&rend));
34   for (i=rstart; i<rend; i++) {
35     for (j=0; j<n; j++) {
36       v    = 10*i+j;
37       PetscCall(MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES));
38     }
39   }
40   PetscCall(MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY));
41   PetscCall(MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY));
42 
43   /* Print info about original matrix */
44   PetscCall(MatGetInfo(mat,MAT_GLOBAL_SUM,&info));
45   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"original matrix nonzeros = %" PetscInt_FMT ", allocated nonzeros = %" PetscInt_FMT "\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated));
46   PetscCall(MatNorm(mat,NORM_FROBENIUS,&normf));
47   PetscCall(MatNorm(mat,NORM_1,&norm1));
48   PetscCall(MatNorm(mat,NORM_INFINITY,&normi));
49   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"original: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi));
50   PetscCall(MatView(mat,PETSC_VIEWER_STDOUT_WORLD));
51 
52   /* Form matrix transpose */
53   PetscCall(PetscOptionsHasName(NULL,NULL,"-in_place",&flg));
54   if (flg) {
55     PetscCall(MatTranspose(mat,MAT_INPLACE_MATRIX,&mat));   /* in-place transpose */
56     tmat = mat; mat = 0;
57   } else {      /* out-of-place transpose */
58     PetscCall(MatTranspose(mat,MAT_INITIAL_MATRIX,&tmat));
59   }
60 
61   /* Print info about transpose matrix */
62   PetscCall(MatGetInfo(tmat,MAT_GLOBAL_SUM,&info));
63   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"transpose matrix nonzeros = %" PetscInt_FMT ", allocated nonzeros = %" PetscInt_FMT "\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated));
64   PetscCall(MatNorm(tmat,NORM_FROBENIUS,&normf));
65   PetscCall(MatNorm(tmat,NORM_1,&norm1));
66   PetscCall(MatNorm(tmat,NORM_INFINITY,&normi));
67   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"transpose: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi));
68   PetscCall(MatView(tmat,PETSC_VIEWER_STDOUT_WORLD));
69 
70   /* Test MatAXPY */
71   if (mat && !rect) {
72     PetscScalar alpha = 1.0;
73     PetscCall(PetscOptionsGetScalar(NULL,NULL,"-alpha",&alpha,NULL));
74     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"matrix addition:  B = B + alpha * A\n"));
75     PetscCall(MatAXPY(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN));
76     PetscCall(MatView(tmat,PETSC_VIEWER_STDOUT_WORLD));
77   }
78 
79   /* Free data structures */
80   PetscCall(MatDestroy(&tmat));
81   if (mat) PetscCall(MatDestroy(&mat));
82 
83   PetscCall(PetscFinalize());
84   return 0;
85 }
86 
87 /*TEST
88 
89    test:
90 
91    testset:
92      args: -rect1
93      test:
94        suffix: r1
95        output_file: output/ex49_r1.out
96      test:
97        suffix: r1_inplace
98        args: -in_place
99        output_file: output/ex49_r1.out
100      test:
101        suffix: r1_par
102        nsize: 2
103        output_file: output/ex49_r1_par.out
104      test:
105        suffix: r1_par_inplace
106        args: -in_place
107        nsize: 2
108        output_file: output/ex49_r1_par.out
109 
110 TEST*/
111