forked from bobbens/ceigs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceigs_cs.c
217 lines (191 loc) · 5.66 KB
/
ceigs_cs.c
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
#include "ceigs.h"
#include "ceigs_cs.h"
#include <cs.h>
#include <math.h>
#include <assert.h>
#include <string.h>
/* Common .*/
static int eigs_w_Av_cs( double *w, int n, const cs *A, const double *v );
/**
* @brief Performs w<-Av where A is a sparse matrix and w,v are both vectors.
* @param[out] w Output vector.
* @param[in] n Length of vectors.
* @param[in] A Sparse matrix to multiply v by.
* @param[in] v Vector to multiply the sparse matrix by.
* @return 0 on success.
*/
static int eigs_w_Av_cs( double *w, int n, const cs *A, const double *v )
{
int err;
memset( w, 0, n*sizeof(double) );
err = cs_gaxpy( A, v, w );
if (err != 1)
fprintf( stderr, "error while running cs_gaxpy\n" );
return !err;
}
/**
* @brief Default driver for dsaupd_ using mode EIGS_MODE_I_REGULAR.
*/
int eigs_dsdrv1_cs( int ido, int n, double *workd, const int *ipntr, const void *data_A, const void *data_M, void *extra )
{
const cs *A = (const cs*) data_A;
(void) data_M;
(void) extra;
/* Matrix vector multiplication ${\bf w}\leftarrow{\bf A}{\bf v}$.
* The vector is in workd(ipntr(1)).
* The result vector must be returned in the array workd(ipntr(2)). */
if (ido == 1)
eigs_w_Av_cs( &workd[ ipntr[1]-1 ], n, A, &workd[ ipntr[0]-1 ] );
return 0;
}
void* eigs_dsdrv2_init_cs( int n, const void *data_A, const void *data_M,
const EigsOpts_t *opts, cs_fact_type_t type )
{
const cs *A = (const cs*) data_A;
(void) data_M;
cs *C, *B, *T;
int i, f;
cs_fact_t *fact;
if (opts->sigma == 0.) {
C = (cs*) A;
f = 0;
}
else {
/* Create Temoporary B matrix as the identity. */
B = cs_spalloc( n, n, n, 1, 1 );
for (i=0; i<n; i++)
cs_entry( B, i, i, 1 );
T = B;
B = cs_compress( T );
cs_spfree( T );
/* C = A - sigma B */
C = cs_add( A, B, 1.0, -opts->sigma );
cs_spfree( B );
f = 1;
}
/* Factorize C and keep factorization. */
fact = cs_fact_init_type( C, type );
if (f)
cs_spfree(C);
return fact;
}
void eigs_dsdrv2_free_cs( void* data, const EigsOpts_t *opts )
{
(void) opts;
cs_fact_t *fact = (cs_fact_t*) data;
cs_fact_free( fact );
}
int eigs_dsdrv2_cs( int ido, int n, double *workd, const int *ipntr, const void *data_A, const void *data_M, void *extra )
{
(void) data_A;
(void) data_M;
cs_fact_t *fact = (cs_fact_t*) extra;
/* Matrix vector multiplication ${\bf w}\leftarrow{\bf A}{\bf v}$.
* The vector is in workd(ipntr(1)).
* The result vector must be returned in the array workd(ipntr(2)). */
if ((ido == -1) || (ido == 1)) {
memcpy( &workd[ ipntr[1]-1 ], &workd[ ipntr[0]-1 ], n*sizeof(double) );
cs_fact_solve( &workd[ ipntr[1]-1 ], fact );
}
return 0;
}
/**
* @brief Default driver for dsaupd_ using mode EIGS_MODE_G_REGINVERSE.
*/
void* eigs_dsdrv3_init_cs( int n, const void *data_A, const void *data_M,
const EigsOpts_t *opts, cs_fact_type_t type )
{
(void) data_A;
const cs *M = (const cs*) data_M;
(void) n;
(void) opts;
cs_fact_t *fact;
fact = cs_fact_init_type( M, type );
return fact;
}
void eigs_dsdrv3_free_cs( void* data, const EigsOpts_t *opts )
{
(void) opts;
cs_fact_t *fact = (cs_fact_t*) data;
cs_fact_free( fact );
}
int eigs_dsdrv3_cs( int ido, int n, double *workd, const int *ipntr,
const void *data_A, const void *data_M, void *extra )
{
const cs *A = (const cs*) data_A;
const cs *M = (const cs*) data_M;
cs_fact_t *fact = (cs_fact_t*) extra;
if ((ido == -1) || (ido == 1)) {
/* y <-- A*x */
eigs_w_Av_cs( &workd[ ipntr[1]-1 ], n, A, &workd[ ipntr[0]-1 ] );
memcpy( &workd[ ipntr[0]-1 ], &workd[ ipntr[1]-1 ], n*sizeof(double) );
/* M*y = A*x, solve for y */
cs_fact_solve( &workd[ ipntr[1]-1 ], fact );
}
else if (ido == 2)
/* y <-- M*x */
eigs_w_Av_cs( &workd[ ipntr[1]-1 ], n, M, &workd[ ipntr[0]-1 ] );
return 0;
}
void* eigs_dsdrv4_init_cs( int n, const void *data_A, const void *data_M,
const EigsOpts_t *opts, cs_fact_type_t type )
{
const cs *A = (const cs*) data_A;
const cs *M = (const cs*) data_M;
(void) n;
cs *C;
int f;
cs_fact_t *fact;
/* C = A - sigma M */
if (opts->sigma == 0.0) {
C = (cs*) A;
f = 0;
}
else {
C = cs_add( A, M, 1.0, -opts->sigma );
f = 1;
}
fact = cs_fact_init_type( C, type ); /* Only care about factorization. */
if (f)
cs_spfree(C);
return fact;
}
void eigs_dsdrv4_free_cs( void* data, const EigsOpts_t *opts )
{
(void) opts;
cs_fact_t *fact = (cs_fact_t*) data;
cs_fact_free( fact );
}
/**
* @brief Default driver for dsaupd_ using mode EIGS_MODE_G_SHIFTINVERT.
*
* Solving: A*x = lambda*M*x
*/
int eigs_dsdrv4_cs( int ido, int n, double *workd, const int *ipntr,
const void *data_A, const void *data_M, void *extra )
{
(void) data_A;
const cs *M = (const cs*) data_M;
cs_fact_t *fact = (cs_fact_t*) extra;
if (ido == -1) {
/* y <-- inv[ A - sigma*M ] * M*x
* input: workd(ipntr(1))
* output: workd(ipntr(2)) */
eigs_w_Av_cs( &workd[ ipntr[1]-1 ], n, M, &workd[ ipntr[0]-1 ] );
cs_fact_solve( &workd[ ipntr[1]-1 ], fact );
}
else if (ido == 1) {
/* y <-- inv[ A - sigma*M ] * M*x
* input: M*x in workd(ipntr(3))
* output: workd(ipntr(2)) */
memcpy( &workd[ ipntr[1]-1 ], &workd[ ipntr[2]-1 ], n*sizeof(double) );
cs_fact_solve( &workd[ ipntr[1]-1 ], fact );
}
else if (ido == 2) {
/* y <-- M*x
* input: workd(ipntr(1))
* output: workd(ipntr(2)) */
eigs_w_Av_cs( &workd[ ipntr[1]-1 ], n, M, &workd[ ipntr[0]-1 ] );
}
return 0;
}