Actual source code: test3.c
slepc-3.19.0 2023-03-31
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
11: static char help[] = "Test SVD with user-provided initial vectors.\n\n"
12: "The command line options are:\n"
13: " -n <n>, where <n> = row dimension.\n"
14: " -m <m>, where <m> = column dimension.\n\n";
16: #include <slepcsvd.h>
18: /*
19: This example computes the singular values of a rectangular nxm Grcar matrix:
21: | 1 1 1 1 |
22: | -1 1 1 1 1 |
23: | -1 1 1 1 1 |
24: A = | . . . . . |
25: | . . . . . |
26: | -1 1 1 1 1 |
27: | -1 1 1 1 |
29: */
31: int main(int argc,char **argv)
32: {
33: Mat A; /* Grcar matrix */
34: SVD svd; /* singular value solver context */
35: Vec v0,w0; /* initial vectors */
36: Vec *U,*V;
37: PetscInt N=35,M=30,Istart,Iend,i,col[5],nconv;
38: PetscScalar value[] = { -1, 1, 1, 1, 1 };
39: PetscReal lev1=0.0,lev2=0.0,tol=PETSC_SMALL;
40: PetscBool skiporth=PETSC_FALSE;
42: PetscFunctionBeginUser;
43: PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
44: PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL));
45: PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&M,NULL));
46: PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nSVD of a rectangular Grcar matrix, %" PetscInt_FMT "x%" PetscInt_FMT "\n\n",N,M));
47: PetscCall(PetscOptionsGetBool(NULL,NULL,"-skiporth",&skiporth,NULL));
49: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: Generate the matrix
51: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
53: PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
54: PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,M));
55: PetscCall(MatSetFromOptions(A));
56: PetscCall(MatSetUp(A));
58: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
59: for (i=Istart;i<Iend;i++) {
60: col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
61: if (i==0) PetscCall(MatSetValues(A,1,&i,PetscMin(4,M-i+1),col+1,value+1,INSERT_VALUES));
62: else PetscCall(MatSetValues(A,1,&i,PetscMin(5,M-i+1),col,value,INSERT_VALUES));
63: }
64: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
65: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
67: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68: Create the SVD context and solve the problem
69: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
71: PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
72: PetscCall(SVDSetOperators(svd,A,NULL));
73: PetscCall(SVDSetFromOptions(svd));
75: /*
76: Set the initial vectors. This is optional, if not done the initial
77: vectors are set to random values
78: */
79: PetscCall(MatCreateVecs(A,&v0,&w0));
80: PetscCall(VecSet(v0,1.0));
81: PetscCall(VecSet(w0,1.0));
82: PetscCall(SVDSetInitialSpaces(svd,1,&v0,1,&w0));
84: /*
85: Compute solution
86: */
87: PetscCall(SVDSolve(svd));
88: PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
90: /*
91: Check orthonormality of computed singular vectors
92: */
93: PetscCall(SVDGetConverged(svd,&nconv));
94: if (nconv>1) {
95: PetscCall(VecDuplicateVecs(w0,nconv,&U));
96: PetscCall(VecDuplicateVecs(v0,nconv,&V));
97: for (i=0;i<nconv;i++) PetscCall(SVDGetSingularTriplet(svd,i,NULL,U[i],V[i]));
98: if (!skiporth) {
99: PetscCall(VecCheckOrthonormality(U,nconv,NULL,nconv,NULL,NULL,&lev1));
100: PetscCall(VecCheckOrthonormality(V,nconv,NULL,nconv,NULL,NULL,&lev2));
101: }
102: if (lev1+lev2<20*tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality below the tolerance\n"));
103: else PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality: %g (U) %g (V)\n",(double)lev1,(double)lev2));
104: PetscCall(VecDestroyVecs(nconv,&U));
105: PetscCall(VecDestroyVecs(nconv,&V));
106: }
108: /*
109: Free work space
110: */
111: PetscCall(VecDestroy(&v0));
112: PetscCall(VecDestroy(&w0));
113: PetscCall(SVDDestroy(&svd));
114: PetscCall(MatDestroy(&A));
115: PetscCall(SlepcFinalize());
116: return 0;
117: }
119: /*TEST
121: testset:
122: args: -svd_nsv 4
123: output_file: output/test3_1.out
124: filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
125: test:
126: suffix: 1_lanczos
127: args: -svd_type lanczos
128: test:
129: suffix: 1_lanczos_one
130: args: -svd_type lanczos -svd_lanczos_oneside
131: test:
132: suffix: 1_trlanczos
133: args: -svd_type trlanczos -svd_trlanczos_locking {{0 1}}
134: test:
135: suffix: 1_trlanczos_one
136: args: -svd_type trlanczos -svd_trlanczos_oneside
137: test:
138: suffix: 1_trlanczos_one_mgs
139: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
140: test:
141: suffix: 1_trlanczos_one_always
142: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
143: test:
144: suffix: 1_cross
145: args: -svd_type cross
146: test:
147: suffix: 1_cross_exp
148: args: -svd_type cross -svd_cross_explicitmatrix
149: test:
150: suffix: 1_cyclic
151: args: -svd_type cyclic
152: requires: !__float128
153: test:
154: suffix: 1_cyclic_exp
155: args: -svd_type cyclic -svd_cyclic_explicitmatrix
156: requires: !__float128
157: test:
158: suffix: 1_lapack
159: args: -svd_type lapack
160: test:
161: suffix: 1_randomized
162: args: -svd_type randomized
163: test:
164: suffix: 1_primme
165: args: -svd_type primme
166: requires: primme
168: testset:
169: args: -svd_implicittranspose -svd_nsv 4 -svd_tol 1e-5
170: output_file: output/test3_1.out
171: filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
172: test:
173: suffix: 2_lanczos
174: args: -svd_type lanczos -svd_conv_norm
175: test:
176: suffix: 2_lanczos_one
177: args: -svd_type lanczos -svd_lanczos_oneside
178: test:
179: suffix: 2_trlanczos
180: args: -svd_type trlanczos
181: test:
182: suffix: 2_trlanczos_one
183: args: -svd_type trlanczos -svd_trlanczos_oneside
184: test:
185: suffix: 2_trlanczos_one_mgs
186: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
187: test:
188: suffix: 2_trlanczos_one_always
189: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
190: test:
191: suffix: 2_cross
192: args: -svd_type cross
193: test:
194: suffix: 2_cross_exp
195: args: -svd_type cross -svd_cross_explicitmatrix
196: requires: !complex
197: test:
198: suffix: 2_cyclic
199: args: -svd_type cyclic -svd_tol 1e-9
200: requires: double
201: test:
202: suffix: 2_lapack
203: args: -svd_type lapack
204: test:
205: suffix: 2_randomized
206: args: -svd_type randomized
208: testset:
209: args: -svd_nsv 4 -mat_type aijcusparse
210: requires: cuda
211: output_file: output/test3_1.out
212: filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
213: test:
214: suffix: 3_cuda_lanczos
215: args: -svd_type lanczos
216: test:
217: suffix: 3_cuda_lanczos_one
218: args: -svd_type lanczos -svd_lanczos_oneside
219: test:
220: suffix: 3_cuda_trlanczos
221: args: -svd_type trlanczos
222: test:
223: suffix: 3_cuda_trlanczos_one
224: args: -svd_type trlanczos -svd_trlanczos_oneside
225: test:
226: suffix: 3_cuda_trlanczos_one_mgs
227: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
228: test:
229: suffix: 3_cuda_trlanczos_one_always
230: args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
231: test:
232: suffix: 3_cuda_cross
233: args: -svd_type cross
234: test:
235: suffix: 3_cuda_cyclic
236: args: -svd_type cyclic
237: test:
238: suffix: 3_cuda_cyclic_exp
239: args: -svd_type cyclic -svd_cyclic_explicitmatrix
240: test:
241: suffix: 3_cuda_randomized
242: args: -svd_type randomized
244: test:
245: suffix: 4
246: args: -svd_type lapack -svd_nsv 4
247: output_file: output/test3_1.out
248: filter: sed -e "s/15129/15128/"
249: nsize: 2
251: test:
252: suffix: 5
253: args: -svd_nsv 4 -svd_view_values draw -svd_monitor draw::draw_lg
254: requires: x
255: output_file: output/test3_1.out
257: TEST*/