Actual source code: test3.c

slepc-3.16.1 2021-11-17
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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;

 43:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 44:   PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
 45:   PetscOptionsGetInt(NULL,NULL,"-m",&M,NULL);
 46:   PetscPrintf(PETSC_COMM_WORLD,"\nSVD of a rectangular Grcar matrix, %Dx%D\n\n",N,M);
 47:   PetscOptionsGetBool(NULL,NULL,"-skiporth",&skiporth,NULL);

 49:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 50:         Generate the matrix
 51:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 53:   MatCreate(PETSC_COMM_WORLD,&A);
 54:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,M);
 55:   MatSetFromOptions(A);
 56:   MatSetUp(A);

 58:   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) {
 62:       MatSetValues(A,1,&i,PetscMin(4,M-i+1),col+1,value+1,INSERT_VALUES);
 63:     } else {
 64:       MatSetValues(A,1,&i,PetscMin(5,M-i+1),col,value,INSERT_VALUES);
 65:     }
 66:   }
 67:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 68:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 71:              Create the SVD context and solve the problem
 72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 74:   SVDCreate(PETSC_COMM_WORLD,&svd);
 75:   SVDSetOperators(svd,A,NULL);
 76:   SVDSetFromOptions(svd);

 78:   /*
 79:      Set the initial vectors. This is optional, if not done the initial
 80:      vectors are set to random values
 81:   */
 82:   MatCreateVecs(A,&v0,&w0);
 83:   VecSet(v0,1.0);
 84:   VecSet(w0,1.0);
 85:   SVDSetInitialSpaces(svd,1,&v0,1,&w0);

 87:   /*
 88:      Compute solution
 89:   */
 90:   SVDSolve(svd);
 91:   SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);

 93:   /*
 94:      Check orthonormality of computed singular vectors
 95:   */
 96:   SVDGetConverged(svd,&nconv);
 97:   if (nconv>1) {
 98:     VecDuplicateVecs(w0,nconv,&U);
 99:     VecDuplicateVecs(v0,nconv,&V);
100:     for (i=0;i<nconv;i++) {
101:       SVDGetSingularTriplet(svd,i,NULL,U[i],V[i]);
102:     }
103:     if (!skiporth) {
104:       VecCheckOrthonormality(U,nconv,NULL,nconv,NULL,NULL,&lev1);
105:       VecCheckOrthonormality(V,nconv,NULL,nconv,NULL,NULL,&lev2);
106:     }
107:     if (lev1+lev2<20*tol) {
108:       PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality below the tolerance\n");
109:     } else {
110:       PetscPrintf(PETSC_COMM_WORLD,"Level of orthogonality: %g (U) %g (V)\n",(double)lev1,(double)lev2);
111:     }
112:     VecDestroyVecs(nconv,&U);
113:     VecDestroyVecs(nconv,&V);
114:   }

116:   /*
117:      Free work space
118:   */
119:   VecDestroy(&v0);
120:   VecDestroy(&w0);
121:   SVDDestroy(&svd);
122:   MatDestroy(&A);
123:   SlepcFinalize();
124:   return ierr;
125: }

127: /*TEST

129:    testset:
130:       args: -svd_nsv 4
131:       output_file: output/test3_1.out
132:       filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
133:       test:
134:          suffix: 1_lanczos
135:          args: -svd_type lanczos
136:       test:
137:          suffix: 1_lanczos_one
138:          args: -svd_type lanczos -svd_lanczos_oneside
139:       test:
140:          suffix: 1_trlanczos
141:          args: -svd_type trlanczos -svd_trlanczos_locking {{0 1}}
142:       test:
143:          suffix: 1_trlanczos_one
144:          args: -svd_type trlanczos -svd_trlanczos_oneside
145:       test:
146:          suffix: 1_trlanczos_one_mgs
147:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
148:       test:
149:          suffix: 1_trlanczos_one_always
150:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
151:       test:
152:          suffix: 1_cross
153:          args: -svd_type cross
154:       test:
155:          suffix: 1_cross_exp
156:          args: -svd_type cross -svd_cross_explicitmatrix
157:       test:
158:          suffix: 1_cyclic
159:          args: -svd_type cyclic
160:          requires: !__float128
161:       test:
162:          suffix: 1_cyclic_exp
163:          args: -svd_type cyclic -svd_cyclic_explicitmatrix
164:          requires: !__float128
165:       test:
166:          suffix: 1_lapack
167:          args: -svd_type lapack
168:       test:
169:          suffix: 1_randomized
170:          args: -svd_type randomized
171:       test:
172:          suffix: 1_primme
173:          args: -svd_type primme
174:          requires: primme

176:    testset:
177:       args: -svd_implicittranspose -svd_nsv 4 -svd_tol 1e-5
178:       output_file: output/test3_1.out
179:       filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
180:       test:
181:          suffix: 2_lanczos
182:          args: -svd_type lanczos -svd_conv_norm
183:       test:
184:          suffix: 2_lanczos_one
185:          args: -svd_type lanczos -svd_lanczos_oneside
186:       test:
187:          suffix: 2_trlanczos
188:          args: -svd_type trlanczos
189:       test:
190:          suffix: 2_trlanczos_one
191:          args: -svd_type trlanczos -svd_trlanczos_oneside
192:       test:
193:          suffix: 2_trlanczos_one_mgs
194:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
195:       test:
196:          suffix: 2_trlanczos_one_always
197:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
198:       test:
199:          suffix: 2_cross
200:          args: -svd_type cross
201:       test:
202:          suffix: 2_cross_exp
203:          args: -svd_type cross -svd_cross_explicitmatrix
204:          requires: !complex
205:       test:
206:          suffix: 2_cyclic
207:          args: -svd_type cyclic -svd_tol 1e-8
208:          requires: double
209:       test:
210:          suffix: 2_lapack
211:          args: -svd_type lapack
212:       test:
213:          suffix: 2_randomized
214:          args: -svd_type randomized

216:    testset:
217:       args: -svd_nsv 4 -mat_type aijcusparse
218:       requires: cuda
219:       output_file: output/test3_1.out
220:       filter: sed -e "s/22176/22175/" | sed -e "s/21798/21797/" | sed -e "s/16826/16825/" | sed -e "s/15129/15128/"
221:       test:
222:          suffix: 3_cuda_lanczos
223:          args: -svd_type lanczos
224:       test:
225:          suffix: 3_cuda_lanczos_one
226:          args: -svd_type lanczos -svd_lanczos_oneside
227:       test:
228:          suffix: 3_cuda_trlanczos
229:          args: -svd_type trlanczos
230:       test:
231:          suffix: 3_cuda_trlanczos_one
232:          args: -svd_type trlanczos -svd_trlanczos_oneside
233:       test:
234:          suffix: 3_cuda_trlanczos_one_mgs
235:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_type mgs
236:       test:
237:          suffix: 3_cuda_trlanczos_one_always
238:          args: -svd_type trlanczos -svd_trlanczos_oneside -bv_orthog_refine always
239:       test:
240:          suffix: 3_cuda_cross
241:          args: -svd_type cross
242:       test:
243:          suffix: 3_cuda_cyclic
244:          args: -svd_type cyclic
245:       test:
246:          suffix: 3_cuda_cyclic_exp
247:          args: -svd_type cyclic -svd_cyclic_explicitmatrix
248:       test:
249:          suffix: 3_cuda_randomized
250:          args: -svd_type randomized

252:    test:
253:       suffix: 4
254:       args: -svd_type lapack -svd_nsv 4
255:       output_file: output/test3_1.out
256:       nsize: 2

258:    test:
259:       suffix: 5
260:       args: -svd_nsv 4 -svd_view_values draw -svd_monitor draw::draw_lg
261:       requires: x
262:       output_file: output/test3_1.out

264: TEST*/