Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Tests user interface for TRLANCZOS with GSVD.\n\n"
12 : "The command line options are:\n"
13 : " -m <m>, where <m> = number of rows of A.\n"
14 : " -n <n>, where <n> = number of columns of A.\n"
15 : " -p <p>, where <p> = number of rows of B.\n"
16 : " -s <s>, where <s> = scale parameter.\n\n";
17 :
18 : #include <slepcsvd.h>
19 :
20 6 : int main(int argc,char **argv)
21 : {
22 6 : Mat A,B;
23 6 : SVD svd;
24 6 : KSP ksp;
25 6 : PC pc;
26 6 : PetscInt m=15,n=20,p=21,i,j,d,Istart,Iend;
27 6 : PetscReal keep,scale=1.0;
28 6 : PetscBool flg,lock,oneside;
29 6 : SVDTRLanczosGBidiag bidiag;
30 :
31 6 : PetscFunctionBeginUser;
32 6 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
33 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
34 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
35 6 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL));
36 6 : PetscCall(PetscOptionsGetReal(NULL,NULL,"-s",&scale,NULL));
37 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized singular value decomposition, (%" PetscInt_FMT "+%" PetscInt_FMT ")x%" PetscInt_FMT "\n\n",m,p,n));
38 :
39 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
40 : Generate the matrices
41 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
42 :
43 6 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
44 6 : PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
45 6 : PetscCall(MatSetFromOptions(A));
46 :
47 6 : PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
48 87 : for (i=Istart;i<Iend;i++) {
49 81 : if (i>0 && i-1<n) PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
50 81 : if (i+1<n) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
51 81 : if (i<n) PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
52 81 : if (i>n) PetscCall(MatSetValue(A,i,n-1,1.0,INSERT_VALUES));
53 : }
54 6 : PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
55 6 : PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
56 :
57 6 : PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
58 6 : PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,p,n));
59 6 : PetscCall(MatSetFromOptions(B));
60 :
61 6 : PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
62 6 : d = PetscMax(0,n-p);
63 123 : for (i=Istart;i<Iend;i++) {
64 1345 : for (j=0;j<=PetscMin(i,n-1);j++) PetscCall(MatSetValue(B,i,j+d,1.0,INSERT_VALUES));
65 : }
66 6 : PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
67 6 : PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
68 :
69 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70 : Create the singular value solver, set options and solve
71 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
72 :
73 6 : PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
74 6 : PetscCall(SVDSetOperators(svd,A,B));
75 6 : PetscCall(SVDSetProblemType(svd,SVD_GENERALIZED));
76 6 : PetscCall(SVDSetDimensions(svd,4,PETSC_DETERMINE,PETSC_DETERMINE));
77 6 : PetscCall(SVDSetConvergenceTest(svd,SVD_CONV_NORM));
78 :
79 6 : PetscCall(SVDSetType(svd,SVDTRLANCZOS));
80 6 : PetscCall(SVDTRLanczosSetGBidiag(svd,SVD_TRLANCZOS_GBIDIAG_UPPER));
81 6 : PetscCall(SVDTRLanczosSetScale(svd,scale));
82 :
83 : /* create a standalone KSP with appropriate settings */
84 6 : PetscCall(KSPCreate(PETSC_COMM_WORLD,&ksp));
85 6 : PetscCall(KSPSetType(ksp,KSPLSQR));
86 6 : PetscCall(KSPGetPC(ksp,&pc));
87 6 : PetscCall(PCSetType(pc,PCNONE));
88 6 : PetscCall(KSPSetTolerances(ksp,PETSC_SMALL,PETSC_CURRENT,PETSC_CURRENT,PETSC_CURRENT));
89 6 : PetscCall(KSPSetFromOptions(ksp));
90 6 : PetscCall(SVDTRLanczosSetKSP(svd,ksp));
91 6 : PetscCall(SVDTRLanczosSetRestart(svd,0.4));
92 6 : PetscCall(SVDTRLanczosSetLocking(svd,PETSC_TRUE));
93 :
94 6 : PetscCall(SVDSetFromOptions(svd));
95 :
96 6 : PetscCall(PetscObjectTypeCompare((PetscObject)svd,SVDTRLANCZOS,&flg));
97 6 : if (flg) {
98 6 : PetscCall(SVDTRLanczosGetGBidiag(svd,&bidiag));
99 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"TRLANCZOS: using %s bidiagonalization\n",SVDTRLanczosGBidiags[bidiag]));
100 6 : PetscCall(SVDTRLanczosGetRestart(svd,&keep));
101 6 : PetscCall(SVDTRLanczosGetLocking(svd,&lock));
102 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"TRLANCZOS: restarting parameter %.2f %s\n",(double)keep,lock?"(locking)":""));
103 6 : PetscCall(SVDTRLanczosGetScale(svd,&scale));
104 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"TRLANCZOS: scale parameter %g\n",(double)scale));
105 6 : PetscCall(SVDTRLanczosGetOneSide(svd,&oneside));
106 6 : if (oneside) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"TRLANCZOS: using one-sided orthogonalization\n"));
107 : }
108 :
109 6 : PetscCall(SVDSolve(svd));
110 6 : PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,NULL));
111 :
112 : /* Free work space */
113 6 : PetscCall(SVDDestroy(&svd));
114 6 : PetscCall(KSPDestroy(&ksp));
115 6 : PetscCall(MatDestroy(&A));
116 6 : PetscCall(MatDestroy(&B));
117 6 : PetscCall(SlepcFinalize());
118 : return 0;
119 : }
120 :
121 : /*TEST
122 :
123 : test:
124 : suffix: 1
125 : args: -svd_trlanczos_gbidiag {{single upper lower}}
126 : filter: grep -v "TRLANCZOS: using"
127 : requires: !single
128 :
129 : test:
130 : suffix: 2
131 : args: -m 6 -n 12 -p 12 -svd_trlanczos_restart .7
132 : requires: !single
133 :
134 : test:
135 : suffix: 3
136 : args: -s 8 -svd_trlanczos_gbidiag lower
137 : requires: !single
138 :
139 : test:
140 : suffix: 4
141 : args: -s -5 -svd_trlanczos_gbidiag lower
142 : requires: !single
143 :
144 : TEST*/
|