xref: /petsc/src/mat/tests/ex49.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
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   PetscErrorCode ierr;
11   PetscMPIInt    size,rank;
12   PetscInt       rstart,rend,rect = 0;
13   PetscBool      flg;
14   PetscScalar    v;
15   PetscReal      normf,normi,norm1;
16   MatInfo        info;
17 
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   ierr = PetscPrintf(PETSC_COMM_WORLD,"original matrix nonzeros = %" PetscInt_FMT ", allocated nonzeros = %" PetscInt_FMT "\n",
46                      (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);PetscCall(ierr);
47   PetscCall(MatNorm(mat,NORM_FROBENIUS,&normf));
48   PetscCall(MatNorm(mat,NORM_1,&norm1));
49   PetscCall(MatNorm(mat,NORM_INFINITY,&normi));
50   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"original: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi));
51   PetscCall(MatView(mat,PETSC_VIEWER_STDOUT_WORLD));
52 
53   /* Form matrix transpose */
54   PetscCall(PetscOptionsHasName(NULL,NULL,"-in_place",&flg));
55   if (flg) {
56     PetscCall(MatTranspose(mat,MAT_INPLACE_MATRIX,&mat));   /* in-place transpose */
57     tmat = mat; mat = 0;
58   } else {      /* out-of-place transpose */
59     PetscCall(MatTranspose(mat,MAT_INITIAL_MATRIX,&tmat));
60   }
61 
62   /* Print info about transpose matrix */
63   PetscCall(MatGetInfo(tmat,MAT_GLOBAL_SUM,&info));
64   ierr = PetscPrintf(PETSC_COMM_WORLD,"transpose matrix nonzeros = %" PetscInt_FMT ", allocated nonzeros = %" PetscInt_FMT "\n",
65                      (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);PetscCall(ierr);
66   PetscCall(MatNorm(tmat,NORM_FROBENIUS,&normf));
67   PetscCall(MatNorm(tmat,NORM_1,&norm1));
68   PetscCall(MatNorm(tmat,NORM_INFINITY,&normi));
69   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"transpose: Frobenious norm = %g, one norm = %g, infinity norm = %g\n",(double)normf,(double)norm1,(double)normi));
70   PetscCall(MatView(tmat,PETSC_VIEWER_STDOUT_WORLD));
71 
72   /* Test MatAXPY */
73   if (mat && !rect) {
74     PetscScalar alpha = 1.0;
75     PetscCall(PetscOptionsGetScalar(NULL,NULL,"-alpha",&alpha,NULL));
76     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"matrix addition:  B = B + alpha * A\n"));
77     PetscCall(MatAXPY(tmat,alpha,mat,DIFFERENT_NONZERO_PATTERN));
78     PetscCall(MatView(tmat,PETSC_VIEWER_STDOUT_WORLD));
79   }
80 
81   /* Free data structures */
82   PetscCall(MatDestroy(&tmat));
83   if (mat) PetscCall(MatDestroy(&mat));
84 
85   PetscCall(PetscFinalize());
86   return 0;
87 }
88 
89 /*TEST
90 
91    test:
92 
93    testset:
94      args: -rect1
95      test:
96        suffix: r1
97        output_file: output/ex49_r1.out
98      test:
99        suffix: r1_inplace
100        args: -in_place
101        output_file: output/ex49_r1.out
102      test:
103        suffix: r1_par
104        nsize: 2
105        output_file: output/ex49_r1_par.out
106      test:
107        suffix: r1_par_inplace
108        args: -in_place
109        nsize: 2
110        output_file: output/ex49_r1_par.out
111 
112 TEST*/
113