-
Notifications
You must be signed in to change notification settings - Fork 12
/
MatrixBLAS.C
381 lines (311 loc) · 9.27 KB
/
MatrixBLAS.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Developed by Sandeep Sharma and Garnet K.-L. Chan, 2012
Copyright (c) 2012, Garnet K.-L. Chan
This program is integrated in Molpro with the permission of
Sandeep Sharma and Garnet K.-L. Chan
*/
#include <iostream>
#include <cmath>
#include <include/newmatutils.h>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include "MatrixBLAS.h"
#include "global.h"
#ifdef BLAS
#include "blas_calls.h"
#endif
double SpinAdapted::dotproduct(const ColumnVector& a, const ColumnVector& b)
{
assert(a.Nrows() == b.Nrows());
#ifdef BLAS
return DDOT(a.Storage(), a.Store(), 1, b.Store(), 1);
#else
return a.t() * b;
#endif
}
double SpinAdapted::dotproduct(const RowVector& a, const RowVector& b)
{
assert(a.Ncols() == b.Ncols());
#ifdef BLAS
return DDOT(a.Storage(), a.Store(), 1, b.Store(), 1);
#else
return a * b.t();
#endif
}
double SpinAdapted::rowdoubleproduct(Matrix& a, int rowa, Matrix& b, int rowb)
{
assert(a.Ncols() == b.Ncols());
double* aptr = a.Store() + a.Ncols() * rowa;
double* bptr = b.Store() + b.Ncols() * rowb;
return DDOT(a.Ncols(), aptr, 1, bptr, 1);
}
void SpinAdapted::xsolve_AxeqB(const Matrix& a, const ColumnVector& b, ColumnVector& x)
{
FORTINT ar = a.Nrows();
int bc = 1;
int info=0;
FORTINT* ipiv = new FORTINT[ar];
double* bwork = new double[ar];
for(int i = 0;i<ar;++i)
bwork[i] = b.element(i);
double* workmat = new double[ar*ar];
for(int i = 0;i<ar;++i)
for(int j = 0;j<ar;++j)
workmat[i*ar+j] = a.element(j,i);
GESV(ar, bc, workmat, ar, ipiv, bwork, ar, info);
delete[] ipiv;
delete[] workmat;
for(int i = 0;i<ar;++i)
x.element(i) = bwork[i];
delete[] bwork;
if(info != 0)
{
pout << "Xsolve failed with info error " << info << endl;
abort();
}
}
void SpinAdapted::svd(Matrix& M, DiagonalMatrix& d, Matrix& U, Matrix& V)
{
int nrows = M.Nrows();
int ncols = M.Ncols();
assert(nrows >= ncols);
int minmn = min(nrows, ncols);
int maxmn = max(nrows, ncols);
int eigenrows = min(minmn, minmn);
d.ReSize(minmn);
Matrix Ut;
Ut.ReSize(nrows, nrows);
V.ReSize(ncols, ncols);
int lwork = maxmn * maxmn + 100;
double* workspace = new double[lwork];
// first transpose matrix
Matrix Mt;
Mt = M.t();
int info = 0;
DGESVD('A', 'A', nrows, ncols, Mt.Store(), nrows, d.Store(),
Ut.Store(), nrows, V.Store(), ncols, workspace, lwork, info);
U.ReSize(nrows, ncols);
SpinAdapted::Clear(U);
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
U(i+1,j+1) = Ut(j+1,i+1);
delete[] workspace;
}
void SpinAdapted::svd(StackMatrix& M, DiagonalMatrix& d, Matrix& U, Matrix& V)
{
int nrows = M.Nrows();
int ncols = M.Ncols();
assert(nrows >= ncols);
int minmn = min(nrows, ncols);
int maxmn = max(nrows, ncols);
int eigenrows = min(minmn, minmn);
d.ReSize(minmn);
Matrix Ut;
Ut.ReSize(nrows, nrows);
V.ReSize(ncols, ncols);
int lwork = maxmn * maxmn + 100;
double* workspace = new double[lwork];
// first transpose matrix
Matrix Mt;
//Mt = M.t();
int info = 0;
DGESVD('A', 'A', nrows, ncols, Mt.Store(), nrows, d.Store(),
Ut.Store(), nrows, V.Store(), ncols, workspace, lwork, info);
U.ReSize(nrows, ncols);
SpinAdapted::Clear(U);
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
U(i+1,j+1) = Ut(j+1,i+1);
delete[] workspace;
}
void SpinAdapted::diagonalise_tridiagonal(std::vector<double>& diagonal, std::vector<double>& offdiagonal, int numelements, Matrix& vec)
{
int nrows = numelements;
int ncols = numelements;
vec.ReSize(nrows, nrows);
Matrix vec_transpose; vec_transpose = vec;
vector<double> workarray(4*nrows-2,0);
int info = 0;
DSTEV('V', nrows, &(diagonal[0]), &(offdiagonal[0]), vec_transpose.Store(), nrows, &(workarray[0]), info);
if (info != 0)
{
pout << "failed to converge :: " <<info<< endl;
abort();
}
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
vec(j+1,i+1) = vec_transpose(i+1,j+1);
}
void SpinAdapted::diagonalise(Matrix& sym, DiagonalMatrix& d, Matrix& vec)
{
int nrows = sym.Nrows();
int ncols = sym.Ncols();
assert(nrows == ncols);
d.ReSize(nrows);
vec.ReSize(nrows, nrows);
Matrix workmat;
workmat = sym;
vector<double> workquery(1);
int info = 0;
double* dptr = d.Store();
int query = -1;
DSYEV('V', 'L', nrows, workmat.Store(), nrows, dptr, &(workquery[0]), query, info); // do query to find best size
int optlength = static_cast<int>(workquery[0]);
vector<double> workspace(optlength);
DSYEV('V', 'U', nrows, workmat.Store(), nrows, dptr, &(workspace[0]), optlength, info); // do query to find best size
if (info > 0)
{
pout << "failed to converge " << endl;
abort();
}
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
vec(j+1,i+1) = workmat(i+1,j+1);
}
void SpinAdapted::diagonalise(StackMatrix& sym, DiagonalMatrix& d)
{
int nrows = sym.Nrows();
int ncols = sym.Ncols();
assert(nrows == ncols);
d.ReSize(nrows);
std::vector<double> workmat(nrows*ncols, 0.0);
DCOPY(nrows*ncols, sym.Store(), 1, &workmat[0], 1);
vector<double> workquery(1);
int info = 0;
double* dptr = d.Store();
int query = -1;
DSYEV('V', 'L', nrows, &workmat[0], nrows, dptr, &(workquery[0]), query, info); // do query to find best size
int optlength = static_cast<int>(workquery[0]);
vector<double> workspace(optlength);
DSYEV('V', 'U', nrows, &workmat[0], nrows, dptr, &(workspace[0]), optlength, info); // do query to find best size
if (info > 0)
{
pout << "failed to converge " << endl;
abort();
}
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
sym(j+1,i+1) = workmat[i*ncols+j];
}
double SpinAdapted::CheckSum (Matrix& a)
{
double val = 0.;
for (int i = 0; i < a.Nrows (); ++i)
for (int j = 0; j < a.Ncols (); ++j)
val += a.element (i, j);
return val;
}
void SpinAdapted::CatenateProduct (const ObjectMatrix<Matrix*>& a, Matrix& b, bool allocate)
{
try
{
std::vector<int> indexRows (a.Nrows ());
std::vector<int> indexCols (a.Ncols ());
int rowLength = 0;
int colLength = 0;
for (int i = 0; i < indexRows.size (); ++i)
{
indexRows [i] = (i > 0) ? a (i - 1,0)->Nrows () + indexRows [i - 1] : 1;
rowLength += a (i,0)->Nrows ();
}
for (int i = 0; i < indexCols.size (); ++i)
{
indexCols [i] = (i > 0) ? a (0,i - 1)->Ncols () + indexCols [i - 1] : 1;
colLength += a (0,i)->Ncols ();
}
if (!allocate)
assert (b.Nrows () == rowLength && b.Ncols () == colLength); // precondition
else
b.ReSize (rowLength, colLength);
for (int i = 0; i < a.Nrows (); ++i)
for (int j = 0; j < a.Ncols (); ++j)
{
#ifdef BLAS
int bcols = b.Ncols();
double* bptr = b.Store() + bcols * (indexRows[i] - 1) + (indexCols[j] - 1);
Matrix* aij = a(i, j);
double* aptr = aij->Store();
int nrows = aij->Nrows();
int ncols = aij->Ncols();
for (int r = 0; r < nrows; ++r)
{
DCOPY(ncols, aptr, 1, bptr, 1);
aptr += ncols;
bptr += bcols;
}
#else
b.SubMatrix (indexRows [i], indexRows [i] + a (i,j)->Nrows () - 1, indexCols [j], indexCols [j] + a (i,j)->Ncols () - 1) = *(a (i,j));
#endif
}
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
}
void SpinAdapted::CatenateProduct (const ObjectMatrix<StackMatrix*>& a, StackMatrix& b)
{
try
{
std::vector<int> indexRows (a.Nrows ());
std::vector<int> indexCols (a.Ncols ());
int rowLength = 0;
int colLength = 0;
for (int i = 0; i < indexRows.size (); ++i)
{
indexRows [i] = (i > 0) ? a (i - 1,0)->Nrows () + indexRows [i - 1] : 1;
rowLength += a (i,0)->Nrows ();
}
for (int i = 0; i < indexCols.size (); ++i)
{
indexCols [i] = (i > 0) ? a (0,i - 1)->Ncols () + indexCols [i - 1] : 1;
colLength += a (0,i)->Ncols ();
}
for (int i = 0; i < a.Nrows (); ++i)
for (int j = 0; j < a.Ncols (); ++j)
{
#ifdef BLAS
int bcols = b.Ncols();
double* bptr = b.Store() + bcols * (indexRows[i] - 1) + (indexCols[j] - 1);
StackMatrix* aij = a(i, j);
double* aptr = aij->Store();
int nrows = aij->Nrows();
int ncols = aij->Ncols();
for (int r = 0; r < nrows; ++r)
{
DCOPY(ncols, aptr, 1, bptr, 1);
aptr += ncols;
bptr += bcols;
}
#else
b.SubMatrix (indexRows [i], indexRows [i] + a (i,j)->Nrows () - 1, indexCols [j], indexCols [j] + a (i,j)->Ncols () - 1) = *(a (i,j));
#endif
}
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
}
void SpinAdapted::Save(const Matrix& a, std::ofstream &ofs)
{
boost::archive::binary_oarchive save_mat(ofs);
save_mat << a;
}
void SpinAdapted::Load(Matrix& a, std::ifstream &ifs)
{
boost::archive::binary_iarchive load_mat(ifs);
load_mat >> a;
}
void SpinAdapted::DebugPrint (vector<int>& v)
{
for (int i = 0; i < v.size(); ++i)
pout << v[i] << endl;
}
void SpinAdapted::DebugPrint (vector<double>& v)
{
for (int i = 0; i < v.size(); ++i)
pout << v[i] << endl;
}