Actual source code: test3.c

slepc-3.17.1 2022-04-11
Report Typos and Errors
  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:   SlepcInitialize(&argc,&argv,(char*)0,help);
 43:   PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
 44:   PetscOptionsGetInt(NULL,NULL,"-m",&M,NULL);
 45:   PetscPrintf(PETSC_COMM_WORLD,"\nSVD of a rectangular Grcar matrix, %" PetscInt_FMT "x%" PetscInt_FMT "\n\n",N,M);
 46:   PetscOptionsGetBool(NULL,NULL,"-skiporth",&skiporth,NULL);

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

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

 57:   MatGetOwnershipRange(A,&Istart,&Iend);
 58:   for (i=Istart;i<Iend;i++) {
 59:     col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
 60:     if (i==0) MatSetValues(A,1,&i,PetscMin(4,M-i+1),col+1,value+1,INSERT_VALUES);
 61:     else MatSetValues(A,1,&i,PetscMin(5,M-i+1),col,value,INSERT_VALUES);
 62:   }
 63:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 64:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 67:              Create the SVD context and solve the problem
 68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 70:   SVDCreate(PETSC_COMM_WORLD,&svd);
 71:   SVDSetOperators(svd,A,NULL);
 72:   SVDSetFromOptions(svd);

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

 83:   /*
 84:      Compute solution
 85:   */
 86:   SVDSolve(svd);
 87:   SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);

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

107:   /*
108:      Free work space
109:   */
110:   VecDestroy(&v0);
111:   VecDestroy(&w0);
112:   SVDDestroy(&svd);
113:   MatDestroy(&A);
114:   SlepcFinalize();
115:   return 0;
116: }

118: /*TEST

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

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

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

243:    test:
244:       suffix: 4
245:       args: -svd_type lapack -svd_nsv 4
246:       output_file: output/test3_1.out
247:       nsize: 2

249:    test:
250:       suffix: 5
251:       args: -svd_nsv 4 -svd_view_values draw -svd_monitor draw::draw_lg
252:       requires: x
253:       output_file: output/test3_1.out

255: TEST*/