-
Notifications
You must be signed in to change notification settings - Fork 12
/
MatrixBLAS.h
436 lines (395 loc) · 12 KB
/
MatrixBLAS.h
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
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
*/
#ifndef SPIN_MATRIX_BLAS_HEADER
#define SPIN_MATRIX_BLAS_HEADER
#define WANT_STREAM
#include "StackMatrix.h"
#include <newmat.h>
#include <newmatio.h>
#include <cassert>
#include <cstdio>
#include <vector>
#include <map>
#include <fstream>
#include "blas_calls.h"
#include "ObjectMatrix.h"
using namespace std;
namespace SpinAdapted{
template<class T> void MatrixScale(double d, T& a)
{
#ifdef BLAS
DSCAL(a.Storage(), d, a.Store(), 1);
#else
a *= d;
#endif
}
template<class T1, class T2> double MatrixDotProduct(const T1& a, const T2& b)
{
assert((a.Nrows() == b.Nrows()) && (a.Ncols() == b.Ncols()));
#ifdef BLAS
return DDOT(a.Storage(), a.Store(), 1, b.Store(), 1);
#else
abort();
#endif
}
template<class T> void MatrixNormalise(T& a)
{
double norm = MatrixDotProduct(a, a);
MatrixScale(1./sqrt(norm), a);
}
template<class T> void Randomise (T& a)
{
Real* val = a.Store ();
for (int i = 0; i < a.Storage (); ++i)
{
*val = double(rand ()) / RAND_MAX;
++val;
}
}
template<class T> void SymmetricRandomise (T& a)
{
assert(a.Nrows() == a.Ncols());
for (int i=0; i<a.Nrows(); i++)
for (int j=0; j<i+1; j++) {
a(i+1, j+1) = double(rand())/RAND_MAX;
a(j+1, i+1) = a(i+1, j+1);
}
}
template<class T1, class T2> void MatrixScaleAdd (double d, const T1& aref, T2& b){
#ifdef BLAS
//ugly hack, sometimes aref is a TransposeMatrix which does not have the function Store()
//so I cast it as a Matrix and then use the usual Matrix functions
const T1& a = static_cast<const T1&>(aref);
assert (a.Nrows () == b.Nrows () && a.Ncols () == b.Ncols ());
int n = a.Nrows () * a.Ncols ();
assert (n == (b.Nrows () * b.Ncols ()));
DAXPY (n, d, a.Store (), 1, b.Store (), 1);
#else
b += d * a;
#endif
}
template<class T1, class T2, class T3> void MatrixTensorProduct (const T1& a_ref, char conjA, Real scaleA, const T2& b_ref, char conjB, Real scaleB, T3& c, int rowstride, int colstride){
#ifndef BLAS
T1 A;
T2 B;
#endif
T1& a = const_cast<T1&>(a_ref); // for BLAS calls
T2& b = const_cast<T2&>(b_ref);
FORTINT arows = a.Nrows();
FORTINT acols = a.Ncols();
// some specialisations
#ifdef FAST_MTP
// if ((brows == 1) && (bcols == 1))
{
double b00 = *b.Store();
if (conjA == 'n')
{
double* cptr = c.Store()+ rowstride*c.Ncols() + colstride;
for (int i=0; i< a.Nrows();i++)
DAXPY(a.Ncols(), scaleA * scaleB * b00, a.Store()+i*a.Ncols(), 1, cptr + i*c.Ncols(), 1);
return;
}
else
{
double* aptr = a.Store();
double* cptr = c.Store() + rowstride*c.Ncols() + colstride;
for (int col = 0; col < acols; ++col)
{
DAXPY(arows, scaleA * scaleB * b00, aptr, acols, cptr, 1);
++aptr;
cptr += c.Ncols();//arows;
}
return;
}
}
// else
// abort();
#else
try
{
if (conjA == 'n' && conjB == 'n')
{
#ifdef BLAS
FORTINT aRows = a.Nrows ();
FORTINT aCols = a.Ncols ();
FORTINT bRows = b.Nrows ();
FORTINT bCols = b.Ncols ();
for (int i = 0; i < aRows; ++i)
for (int j = 0; j < aCols; ++j)
{
Real scale = scaleA * scaleB * a (i+1,j+1);
for (int k = 0; k < bRows; ++k)
DAXPY (bCols, scale, &b (k+1,1), 1, &c (i * bRows + k+1 +rowstride,j * bCols+1+colstride), 1);
}
return;
#else
A = a;
B = b;
#endif
}
else if (conjA == 't' && conjB == 'n')
{
#ifdef BLAS
FORTINT aRows = a.Ncols ();
FORTINT aCols = a.Nrows ();
FORTINT bRows = b.Nrows ();
FORTINT bCols = b.Ncols ();
for (int i = 0; i < aRows; ++i)
for (int j = 0; j < aCols; ++j)
{
Real scale = scaleA * scaleB * a (j+1,i+1);
for (int k = 0; k < bRows; ++k)
DAXPY (bCols, scale, &b (k+1,1), 1, &c (i * bRows + k+1+rowstride,j * bCols+1+colstride), 1);
}
return;
#else
A = a.t ();
B = b;
#endif
}
else if (conjA == 'n' && conjB == 't')
{
#ifdef BLAS
FORTINT aRows = a.Nrows ();
FORTINT aCols = a.Ncols ();
FORTINT bRows = b.Ncols ();
FORTINT bCols = b.Nrows ();
for (int i = 0; i < aRows; ++i)
for (int j = 0; j < aCols; ++j)
{
Real scale = scaleA * scaleB * a (i+1,j+1);
for (int k = 0; k < bRows; ++k)
DAXPY (bCols, scale, &b (1,k+1), bRows, &c (i * bRows + k+1+rowstride,j * bCols+1+colstride), 1);
}
return;
#else
A = a;
B = b.t ();
#endif
}
else if (conjA == 't' && conjB == 't')
{
#ifdef BLAS
FORTINT aRows = a.Ncols ();
FORTINT aCols = a.Nrows ();
FORTINT bRows = b.Ncols ();
FORTINT bCols = b.Nrows ();
for (int i = 0; i < aRows; ++i)
for (int j = 0; j < aCols; ++j)
{
Real scale = scaleA * scaleB * a (j+1,i+1);
for (int k = 0; k < bRows; ++k)
DAXPY (bCols, scaleA * scaleB * a (j+1,i+1), &b (1,k+1), bRows, &c (i * bRows + k+1+rowstride,j * bCols+1+colstride), 1);
}
return;
#else
A = a.t ();
B = b.t ();
#endif
}
else
abort ();
#ifndef BLAS
for (int i = 1; i <= A.Nrows (); ++i)
for (int j = 1; j <= A.Ncols (); ++j)
c.SubMatrix ((i - 1) * B.Nrows () + 1, i * B.Nrows (), (j - 1) * B.Ncols () + 1, j * B.Ncols ()) += (scaleA * scaleB) * A (i,j) * B;
#endif
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
#endif
}
template<class T1, class T2> void MatrixMultiply (double d, const T1& a, T2& b)
{
// b += d * a;
#ifdef BLAS
assert ((a.Nrows () == b.Nrows ()) && (a.Ncols () == b.Ncols ()));
FORTINT n = a.Nrows () * a.Ncols ();
DAXPY (n, d, a.Store (), 1, b.Store (), 1);
#else
b += d * a;
#endif
}
template<class T1, class T2, class T3> void MatrixMultiply (const T1& a, char conjA, const T2& b, char conjB, T3& c, Real scale, double cfactor = 1.)
{
//dmrginp.justmultiply.start();
//dmrginp.justmultiply -> start(); //ROA
T1& a_ref = const_cast<T1&>(a); // for BLAS calls
T2& b_ref = const_cast<T2&>(b);
try
{
FORTINT aRows = a_ref.Nrows ();
FORTINT aCols = a_ref.Ncols ();
FORTINT bRows = b_ref.Nrows ();
FORTINT bCols = b_ref.Ncols ();
FORTINT cRows = c.Nrows ();
FORTINT cCols = c.Ncols ();
dmrginp.matmultNum++;
if (conjA == 'n' && conjB == 'n')
{
dmrginp.matmultFlops[omprank] += aCols*cRows*cCols;
assert ((aCols == bRows) && (cRows == aRows) && (cCols == bCols));
#ifdef BLAS
dgemm_ (&conjA, &conjB, &bCols, &aRows, &bRows, &scale, b.Store (), &bCols, a.Store (), &aCols, &cfactor, c.Store (), &bCols);
#else
c += (scale * a) * b;
#endif
}
else if (conjA == 'n' && conjB == 't')
{
dmrginp.matmultFlops[omprank] += aCols*cRows*cCols;
assert ((aCols == bCols) && (cRows == aRows) && (cCols == bRows));
#ifdef BLAS
dgemm_ (&conjB, &conjA, &bRows, &aRows, &bCols, &scale, b.Store (), &bCols, a.Store (), &aCols, &cfactor, c.Store (), &bRows);
#else
c += (scale * a) * b.t ();
#endif
}
else if (conjA == 't' && conjB == 'n')
{
dmrginp.matmultFlops[omprank] += aRows*cRows*cCols;
assert ((aRows == bRows) && (cRows == aCols) && (cCols == bCols));
#ifdef BLAS
dgemm_ (&conjB, &conjA, &bCols, &aCols, &bRows, &scale, b.Store (), &bCols, a.Store (), &aCols, &cfactor, c.Store (), &bCols);
#else
c += (scale * a.t ()) * b;
#endif
}
else if (conjA == 't' && conjB == 't')
{
dmrginp.matmultFlops[omprank] += aRows*cRows*cCols;
assert ((aRows == bCols) && (cRows == aCols) && (cCols == bRows));
#ifdef BLAS
dgemm_ (&conjB, &conjA, &bRows, &aCols, &bCols, &scale, b.Store (), &bCols, a.Store (), &aCols, &cfactor, c.Store (), &bRows);
#else
c += (scale * a.t ()) * b.t ();
#endif
}
else
abort ();
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
//dmrginp.justmultiply.stop();
//dmrginp.justmultiply -> stop(); //ROA
}
template<class T> void Clear (T& a)
{
#ifdef BLAS
memset(a.Store(), 0, a.Storage() * sizeof(double));
#else
a = 0.;
#endif
}
template<class T1, class T2> void MatrixRotate (T1 const& a, T2& b, T1 const& c, T2& d)
{
try
{
assert (d.Nrows () == a.Ncols () && d.Ncols () == c.Ncols ());
#ifdef BLAS
T1 work (b.Nrows (), c.Ncols ());
Clear (work);
MatrixMultiply (b, 'n', c, 'n', work, 1.);
MatrixMultiply (a, 't', work, 'n', d, 1.);
#else
d = a.t () * b * c;
#endif
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
}
template<class T> void MatrixDiagonalScale(double d, const T& a, double* b)
{
//assert (a.Nrows () == a.Ncols () && a.Nrows () == b.Ncols ());
#ifdef BLAS
FORTINT n = a.Nrows ();
DAXPY (n, d, a.Store (), n+1, b, 1);
#else
//b += d * a; Should add the non-blas analogue
#endif
}
/*
template<> void MatrixRotate<Matrix> (const Matrix& a, const Matrix& b, const Matrix& c, Matrix& d)
{
try
{
assert (d.Nrows () == a.Ncols () && d.Ncols () == c.Ncols ());
#ifdef BLAS
Matrix work (b.Nrows (), c.Ncols ());
Clear (work);
MatrixMultiply (b, 'n', c, 'n', work, 1.);
MatrixMultiply (a, 't', work, 'n', d, 1.);
#else
d = a.t () * b * c;
#endif
}
catch (Exception)
{
pout << Exception::what () << endl;
abort ();
}
}
*/
void svd(Matrix& M, DiagonalMatrix& e, Matrix& U, Matrix& V);
void svd(StackMatrix& M, DiagonalMatrix& e, Matrix& U, Matrix& V);
void xsolve_AxeqB(const Matrix& a, const ColumnVector& b, ColumnVector& x);
//void MatrixDiagonalScale(double d, const Matrix& a, double* b);
double dotproduct(const ColumnVector& a, const ColumnVector& b);
double dotproduct(const RowVector& a, const RowVector& b);
double rowdoubleproduct(Matrix& a, int rowa, Matrix& b, int rowb);
void diagonalise(Matrix& sym, DiagonalMatrix& d, Matrix& vec);
void diagonalise(StackMatrix& sym, DiagonalMatrix& d);
void diagonalise_tridiagonal(std::vector<double>& diagonal, std::vector<double>& offdiagonal, int numelements, Matrix& vec);
void CatenateProduct (const ObjectMatrix<Matrix*>& a, Matrix& b, bool allocate = false);
void CatenateProduct (const ObjectMatrix<StackMatrix*>& a, StackMatrix& b);
char TransposeOf (char c);
inline char TransposeOf (char c) { assert (c == 'n' || c == 't'); return (c == 'n') ? 't' : 'n'; }
void MatrixRotate (const Matrix& a, const Matrix& b, const Matrix& c, Matrix& d);
void Save (const Matrix& a, std::ofstream &ofs);
void Load (Matrix& a, std::ifstream &ifs);
template<class T> void print(ostream& os, const std::vector<T>& v)
{
for (int i = 0; i < v.size(); ++i)
os << v[i] << " ";
os << endl;
}
double CheckSum (Matrix& a);
void DebugPrint (std::vector<int>& v);
void DebugPrint (std::vector<double>& v);
template<class T> void CyclicPermute (std::vector<T>& v, std::vector<T>& result, int n);
template<class T> void CyclicPermute (std::vector<T>& v, std::vector<T>& result, int n)
{
assert (n >= 0);
result.resize (v.size ());
for (int i = 0; i < v.size (); ++i)
result [i] = v [(i + n) % v.size ()];
}
template<class T> bool find(vector<T>& v, const T& value) { return std::find(v.begin(), v.end(), value) != v.end(); }
template<class T> void get_sorted_indices(const std::vector<T>& data, std::vector<int>& indices)
{
multimap<T, int> sorted_data;
for (int i = 0; i < data.size(); ++i)
sorted_data.insert(make_pair<T, int>(data[i], i));
typename multimap<T, int>::iterator it = sorted_data.begin();
indices.reserve(data.size());
while (it != sorted_data.end())
{
indices.push_back(it->second);
++it;
}
}
}
#endif