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[] = "Solves a singular value problem with the matrix loaded from a file.\n"
12 : "This example works for both real and complex numbers.\n\n"
13 : "The command line options are:\n"
14 : " -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";
15 :
16 : #include <slepcsvd.h>
17 :
18 13 : int main(int argc,char **argv)
19 : {
20 13 : Mat A; /* operator matrix */
21 13 : SVD svd; /* singular value problem solver context */
22 13 : SVDType type;
23 13 : SVDStop stop;
24 13 : PetscReal tol,thres;
25 13 : PetscInt nsv,maxit,its;
26 13 : char filename[PETSC_MAX_PATH_LEN];
27 13 : PetscViewer viewer;
28 13 : PetscBool flg,terse;
29 :
30 13 : PetscFunctionBeginUser;
31 13 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
32 :
33 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34 : Load the operator matrix that defines the singular value problem
35 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36 :
37 13 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n"));
38 13 : PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
39 13 : PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");
40 :
41 : #if defined(PETSC_USE_COMPLEX)
42 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n"));
43 : #else
44 13 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n"));
45 : #endif
46 13 : PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
47 13 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
48 13 : PetscCall(MatSetFromOptions(A));
49 13 : PetscCall(MatLoad(A,viewer));
50 13 : PetscCall(PetscViewerDestroy(&viewer));
51 :
52 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53 : Create the singular value solver and set various options
54 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55 :
56 : /*
57 : Create singular value solver context
58 : */
59 13 : PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
60 :
61 : /*
62 : Set operator
63 : */
64 13 : PetscCall(SVDSetOperators(svd,A,NULL));
65 :
66 : /*
67 : Set solver parameters at runtime
68 : */
69 13 : PetscCall(SVDSetFromOptions(svd));
70 :
71 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72 : Solve the singular value system
73 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74 :
75 13 : PetscCall(SVDSolve(svd));
76 13 : PetscCall(SVDGetIterationNumber(svd,&its));
77 13 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its));
78 :
79 : /*
80 : Optional: Get some information from the solver and display it
81 : */
82 13 : PetscCall(SVDGetType(svd,&type));
83 13 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type));
84 13 : PetscCall(SVDGetStoppingTest(svd,&stop));
85 13 : if (stop!=SVD_STOP_THRESHOLD) {
86 7 : PetscCall(SVDGetDimensions(svd,&nsv,NULL,NULL));
87 7 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %" PetscInt_FMT "\n",nsv));
88 : } else {
89 6 : PetscCall(SVDGetThreshold(svd,&thres,NULL));
90 6 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using threshold: %.4g\n",(double)thres));
91 : }
92 13 : PetscCall(SVDGetTolerances(svd,&tol,&maxit));
93 13 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit));
94 :
95 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 : Display solution and clean up
97 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98 :
99 : /* show detailed info unless -terse option is given by user */
100 13 : PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
101 13 : if (terse) PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
102 : else {
103 1 : PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
104 1 : PetscCall(SVDConvergedReasonView(svd,PETSC_VIEWER_STDOUT_WORLD));
105 1 : PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
106 1 : PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
107 : }
108 13 : PetscCall(SVDDestroy(&svd));
109 13 : PetscCall(MatDestroy(&A));
110 13 : PetscCall(SlepcFinalize());
111 : return 0;
112 : }
113 : /*TEST
114 :
115 : testset:
116 : requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
117 : args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
118 : test:
119 : suffix: 1
120 : args: -svd_nsv 4 -svd_standard -svd_ncv 12 -svd_type {{trlanczos lanczos randomized cross}}
121 : filter: grep -v method
122 : test:
123 : suffix: 1_scalapack
124 : nsize: {{1 2 3}}
125 : args: -svd_nsv 4 -svd_type scalapack
126 : requires: scalapack
127 : test:
128 : suffix: 1_elemental
129 : nsize: {{1 2 3}}
130 : args: -svd_nsv 4 -svd_type elemental
131 : output_file: output/ex14_1_scalapack.out
132 : filter: sed -e "s/elemental/scalapack/"
133 : requires: elemental
134 : test:
135 : suffix: 1_ksvd
136 : nsize: 6
137 : args: -svd_nsv 4 -svd_type ksvd -svd_ksvd_eigen_method {{mrrr elpa}} -svd_ksvd_polar_method {{qdwh zolopd}}
138 : output_file: output/ex14_1_scalapack.out
139 : filter: sed -e "s/ksvd/scalapack/"
140 : requires: elpa polar ksvd
141 : test:
142 : suffix: 2
143 : args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.5 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
144 : filter: grep -v tolerance
145 : test:
146 : suffix: 2_cross
147 : args: -svd_nsv 2 -svd_type cross -svd_cross_explicitmatrix -svd_cross_st_type sinvert -svd_cross_eps_target 100.0
148 : filter: grep -v tolerance
149 :
150 : testset:
151 : requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
152 : args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -terse
153 : test:
154 : suffix: 1_complex
155 : args: -svd_nsv 4
156 : test:
157 : suffix: 1_complex_scalapack
158 : nsize: {{1 2 3}}
159 : args: -svd_nsv 4 -svd_type scalapack
160 : requires: scalapack
161 : test:
162 : suffix: 1_complex_elemental
163 : nsize: {{1 2 3}}
164 : args: -svd_nsv 4 -svd_type elemental
165 : requires: elemental
166 : test:
167 : suffix: 2_complex
168 : args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.0 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu -svd_view
169 : filter: grep -v tolerance
170 :
171 : testset:
172 : args: -svd_nsv 5 -svd_type randomized -svd_max_it 1 -svd_conv_maxit
173 : test:
174 : suffix: 3
175 : args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc
176 : requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
177 : test:
178 : suffix: 3_complex
179 : args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc
180 : requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
181 : filter: sed -e 's/[0-9][0-9]$//'
182 :
183 : testset:
184 : args: -svd_type {{trlanczos lanczos cross}} -terse
185 : filter: grep -v method
186 : test:
187 : suffix: 4
188 : args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -svd_threshold_relative 0.9 -svd_ncv {{26 12}}
189 : requires: double !complex !defined(PETSC_USE_64BIT_INDICES)
190 : test:
191 : suffix: 4_complex
192 : args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -svd_threshold_relative 0.6 -svd_ncv {{18 10}}
193 : requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
194 :
195 : TEST*/
|