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[] = "SVD via the cross-product matrix with a user-provided EPS.\n\n"
12 : "The command line options are:\n"
13 : " -m <m>, where <m> = matrix rows.\n"
14 : " -n <n>, where <n> = matrix columns (defaults to m+2).\n\n";
15 :
16 : #include <slepcsvd.h>
17 :
18 : /*
19 : This example computes the singular values of a rectangular bidiagonal matrix
20 :
21 : | 1 2 |
22 : | 1 2 |
23 : | 1 2 |
24 : A = | . . |
25 : | . . |
26 : | 1 2 |
27 : | 1 2 |
28 : */
29 :
30 2 : int main(int argc,char **argv)
31 : {
32 2 : Mat A;
33 2 : SVD svd;
34 2 : EPS eps;
35 2 : ST st;
36 2 : KSP ksp;
37 2 : PC pc;
38 2 : PetscInt m=20,n,Istart,Iend,i,col[2];
39 2 : PetscScalar value[] = { 1, 2 };
40 2 : PetscBool flg,expmat;
41 :
42 2 : PetscFunctionBeginUser;
43 2 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
44 2 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
45 2 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg));
46 2 : if (!flg) n=m+2;
47 2 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nRectangular bidiagonal matrix, m=%" PetscInt_FMT " n=%" PetscInt_FMT "\n\n",m,n));
48 :
49 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 : Generate the matrix
51 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
52 :
53 2 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
54 2 : PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
55 2 : PetscCall(MatSetFromOptions(A));
56 2 : PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
57 42 : for (i=Istart;i<Iend;i++) {
58 40 : col[0]=i; col[1]=i+1;
59 40 : if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,value,INSERT_VALUES));
60 40 : else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],value[0],INSERT_VALUES));
61 : }
62 2 : PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
63 2 : PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
64 :
65 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66 : Create a standalone EPS with appropriate settings
67 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68 :
69 2 : PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
70 2 : PetscCall(EPSGetST(eps,&st));
71 2 : PetscCall(STSetType(st,STSINVERT));
72 2 : PetscCall(STGetKSP(st,&ksp));
73 2 : PetscCall(KSPSetType(ksp,KSPBCGS));
74 2 : PetscCall(KSPGetPC(ksp,&pc));
75 2 : PetscCall(PCSetType(pc,PCJACOBI));
76 2 : PetscCall(EPSSetFromOptions(eps));
77 :
78 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79 : Compute singular values
80 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
81 :
82 2 : PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
83 2 : PetscCall(SVDSetOperators(svd,A,NULL));
84 2 : PetscCall(SVDSetType(svd,SVDCROSS));
85 2 : PetscCall(SVDCrossSetEPS(svd,eps));
86 2 : PetscCall(SVDSetWhichSingularTriplets(svd,SVD_SMALLEST));
87 2 : PetscCall(SVDSetFromOptions(svd));
88 2 : PetscCall(PetscObjectTypeCompare((PetscObject)svd,SVDCROSS,&flg));
89 2 : if (flg) {
90 2 : PetscCall(SVDCrossGetExplicitMatrix(svd,&expmat));
91 2 : if (expmat) PetscCall(PetscPrintf(PETSC_COMM_WORLD," Using explicit matrix with cross solver\n"));
92 : }
93 2 : PetscCall(SVDSolve(svd));
94 :
95 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 : Display solution and clean up
97 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98 2 : PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,PETSC_VIEWER_STDOUT_WORLD));
99 2 : PetscCall(SVDDestroy(&svd));
100 2 : PetscCall(EPSDestroy(&eps));
101 2 : PetscCall(MatDestroy(&A));
102 2 : PetscCall(SlepcFinalize());
103 : return 0;
104 : }
105 :
106 : /*TEST
107 :
108 : testset:
109 : output_file: output/test6_1.out
110 : test:
111 : suffix: 1_subspace
112 : args: -eps_type subspace
113 : test:
114 : suffix: 1_lobpcg
115 : args: -eps_type lobpcg -st_type precond
116 : test:
117 : suffix: 2_cuda
118 : args: -eps_type subspace -mat_type aijcusparse
119 : requires: cuda
120 : test:
121 : suffix: 2_hip
122 : args: -eps_type subspace -mat_type aijhipsparse
123 : requires: hip
124 :
125 : TEST*/
|