-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
arpack.cpp
218 lines (187 loc) · 5.24 KB
/
arpack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Sergey Lisitsyn
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/
#include <shogun/mathematics/arpack.h>
#ifdef HAVE_ARPACK
#ifdef HAVE_LAPACK
#include <shogun/lib/config.h>
#include <cblas.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <string.h>
using namespace shogun;
namespace shogun
{
void arpack_dsaupd(double* matrix, int n, int nev, const char* which,
int mode, bool pos, double shift, double* eigenvalues,
double* eigenvectors, int& status)
{
// check if nev is greater than n
if (nev>n)
SG_SERROR("Number of required eigenpairs is greater than order of the matrix");
// check specified mode
if (mode!=1 && mode!=3)
SG_SERROR("Mode not supported yet");
// init ARPACK's reverse communication parameter
// (should be zero initially)
int ido = 0;
// specify that non-general eigenproblem will be solved
// (Ax=lGx, where G=I)
char bmat[2] = "I";
// init tolerance (zero means machine precision)
double tol = 0.0;
// allocate array to hold residuals
double* resid = SG_MALLOC(double, n);
// set number of Lanczos basis vectors to be used
// (with max(4*nev,n) sufficient for most tasks)
int ncv = nev*4>n ? n : nev*4;
// allocate array 'v' for dsaupd routine usage
int ldv = n;
double* v = SG_MALLOC(double, ldv*ncv);
// init array for i/o params for routine
int* iparam = SG_MALLOC(int, 11);
// specify method for selecting implicit shifts (1 - exact shifts)
iparam[0] = 1;
// specify max number of iterations
iparam[2] = 3*n;
// set the computation mode (1 for regular or 3 for shift-inverse)
iparam[6] = mode;
// init array indicating locations of vectors for routine callback
int* ipntr = SG_MALLOC(int, 11);
// allocate workaround arrays
double* workd = SG_MALLOC(double, 3*n);
int lworkl = ncv*(ncv+8);
double* workl = SG_MALLOC(double, lworkl);
// init info holding status (should be zero at first call)
int info = 0;
// which eigenpairs to find
char* which_ = strdup(which);
// All
char* all_ = strdup("A");
int* ipiv;
// shift-invert mode init
if (mode==3)
{
// subtract shift from main diagonal
if (shift!=0.0)
{
for (int i=0; i<n; i++)
matrix[i*n+i] -= shift;
}
// compute factorization according to pos value
if (pos)
{
SG_SDEBUG("ARPACK: Using Cholesky factorization");
clapack_dpotrf(CblasColMajor,CblasUpper,n,matrix,n);
}
else
{
SG_SDEBUG("ARPACK: Using LUP factorization");
ipiv = SG_MALLOC(int, n);
clapack_dgetrf(CblasColMajor,n,n,matrix,n,ipiv);
}
}
// main computation loop
double* tmp;
if (mode==3) tmp = SG_MALLOC(double, n);
int i,j;
do
{
dsaupd_(&ido, bmat, &n, which_, &nev, &tol, resid,
&ncv, v, &ldv, iparam, ipntr, workd, workl,
&lworkl, &info);
if ((ido==1)||(ido==-1))
{
if (mode==1)
// compute (workd+ipntr[1]-1) = A*(workd+ipntr[0]-1)
{
cblas_dsymv(CblasColMajor,CblasUpper,
n,1.0,matrix,n,
(workd+ipntr[0]-1),1,
0.0,(workd+ipntr[1]-1),1);
}
if (mode==3)
{
// solve system of eqs A*(workd+ipntr[0]-1) = (workd+ipntr[1]-1)
for (i=0; i<n; i++)
tmp[i] = (workd+ipntr[0]-1)[i];
if (pos)
clapack_dpotrs(CblasColMajor,CblasUpper,n,1,matrix,n,tmp,n);
else
clapack_dgetrs(CblasColMajor,CblasNoTrans,n,1,matrix,n,ipiv,tmp,n);
for (i=0; i<n; i++)
(workd+ipntr[1]-1)[i] = tmp[i];
}
}
} while ((ido==1)||(ido==-1));
if (!pos && mode==3) SG_FREE(ipiv);
if (mode==3) SG_FREE(tmp);
// check if DSAUPD failed
if (info<0)
{
if ((info<=-1)&&(info>=-6))
SG_SWARNING("DSAUPD failed. Wrong parameter passed.");
else if (info==-7)
SG_SWARNING("DSAUPD failed. Workaround array size is not sufficient.");
else
SG_SWARNING("DSAUPD failed. Error code: %d.", info);
status = -1;
}
else
{
if (info==1)
SG_SWARNING("Maximum number of iterations reached.\n");
// allocate select for dseupd
int* select = SG_MALLOC(int, ncv);
// allocate d to hold eigenvalues
double* d = SG_MALLOC(double, 2*ncv);
// sigma for dseupd
double sigma = shift;
// init ierr indicating dseupd possible errors
int ierr = 0;
// specify that eigenvectors to be computed too
int rvec = 1;
// call dseupd_ routine
dseupd_(&rvec, all_, select, d, v, &ldv, &sigma, bmat,
&n, which_, &nev, &tol, resid, &ncv, v, &ldv,
iparam, ipntr, workd, workl, &lworkl, &ierr);
// check for errors
if (ierr!=0)
{
SG_SWARNING("DSEUPD failed with status=%d", ierr);
status = -1;
}
else
{
// store eigenpairs to specified arrays
for (i=0; i<nev; i++)
{
eigenvalues[i] = d[i];
for (j=0; j<n; j++)
eigenvectors[j*nev+i] = v[i*n+j];
}
}
// cleanup
SG_FREE(select);
SG_FREE(d);
}
// cleanup
SG_FREE(all_);
SG_FREE(which_);
SG_FREE(resid);
SG_FREE(v);
SG_FREE(iparam);
SG_FREE(ipntr);
SG_FREE(workd);
SG_FREE(workl);
};
}
#endif /* HAVE_LAPACK */
#endif /* HAVE_ARPACK */