-
Notifications
You must be signed in to change notification settings - Fork 33
/
MatrixBLAS.C
596 lines (534 loc) · 14.9 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
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
#ifdef USING_DOUBLE
#define GAXPY DAXPY
#define GEMM DGEMM
#endif
#ifdef USING_FLOAT
#define GAXPY SAXPY
#define GEMM SGEMM
#endif
void SpinAdapted::MatrixScale(double d, Matrix& a)
{
#ifdef BLAS
DSCAL(a.Storage(), d, a.Store(), 1);
#else
a *= d;
#endif
}
double SpinAdapted::MatrixDotProduct(const Matrix& a, const Matrix& 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
}
void SpinAdapted::MatrixNormalise(Matrix& a)
{
double norm = MatrixDotProduct(a, a);
MatrixScale(1./sqrt(norm), a);
}
void SpinAdapted::Randomise (Matrix& a)
{
Real* val = a.Store ();
for (int i = 0; i < a.Storage (); ++i)
{
*val = double(rand ()) / RAND_MAX;
++val;
}
}
void SpinAdapted::SymmetricRandomise (Matrix& 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);
}
}
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::MatrixScaleAdd (double d, const Matrix& a, Matrix& b)
{
assert (a.Nrows () == b.Nrows () && a.Ncols () == b.Ncols ());
#ifdef BLAS
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
}
void SpinAdapted::MatrixDiagonalScale(double d, const Matrix& a, double* b)
{
//assert (a.Nrows () == a.Ncols () && a.Nrows () == b.Ncols ());
#ifdef BLAS
int n = a.Nrows ();
DAXPY (n, d, a.Store (), n+1, b, 1);
#else
//b += d * a; Should add the non-blas analogue
#endif
}
void SpinAdapted::MatrixTensorProduct (const Matrix& a_ref, char conjA, Real scaleA, const Matrix& b_ref, char conjB, Real scaleB, Matrix& c, int rowstride, int colstride, bool allocate)
{
#ifndef BLAS
Matrix A;
Matrix B;
#endif
Matrix& a = const_cast<Matrix&>(a_ref); // for BLAS calls
Matrix& b = const_cast<Matrix&>(b_ref);
int arows = a.Nrows();
int 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')
{
if (allocate)
{
c.ReSize (a.Nrows () * b.Nrows (), a.Ncols () * b.Ncols ());
Clear (c);
}
//assert ((c.Nrows () == (a.Nrows () * b.Nrows ())) && (c.Ncols () == (a.Ncols () * b.Ncols ())));
#ifdef BLAS
int aRows = a.Nrows ();
int aCols = a.Ncols ();
int bRows = b.Nrows ();
int 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)
GAXPY (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')
{
if (allocate)
{
c.ReSize (a.Ncols () * b.Nrows (), a.Nrows () * b.Ncols ());
Clear (c);
}
//assert ((c.Nrows () == (a.Ncols () * b.Nrows ())) && (c.Ncols () == (a.Nrows () * b.Ncols ())));
#ifdef BLAS
int aRows = a.Ncols ();
int aCols = a.Nrows ();
int bRows = b.Nrows ();
int 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)
GAXPY (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')
{
if (allocate)
{
c.ReSize (a.Nrows () * b.Ncols (), a.Ncols () * b.Nrows ());
Clear (c);
}
//assert ((c.Nrows () == (a.Nrows () * b.Ncols ())) && (c.Ncols () == (a.Ncols () * b.Nrows ())));
#ifdef BLAS
int aRows = a.Nrows ();
int aCols = a.Ncols ();
int bRows = b.Ncols ();
int 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)
GAXPY (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')
{
if (allocate)
{
c.ReSize (a.Ncols () * b.Ncols (), a.Nrows () * b.Nrows ());
Clear (c);
}
//assert ((c.Nrows () == (a.Ncols () * b.Ncols ())) && (c.Ncols () == (a.Nrows () * b.Nrows ())));
#ifdef BLAS
int aRows = a.Ncols ();
int aCols = a.Nrows ();
int bRows = b.Ncols ();
int 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)
GAXPY (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
}
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::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::MatrixMultiply (double d, const Matrix& a, Matrix& b)
{
// b += d * a;
#ifdef BLAS
assert ((a.Nrows () == b.Nrows ()) && (a.Ncols () == b.Ncols ()));
int n = a.Nrows () * a.Ncols ();
GAXPY (n, d, a.Store (), 1, b.Store (), 1);
#else
b += d * a;
#endif
}
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::MatrixMultiply (const Matrix& a, char conjA, const Matrix& b, char conjB, Matrix& c, Real scale, double cfactor)
{
//dmrginp.justmultiply.start();
//dmrginp.justmultiply -> start(); //ROA
Matrix& a_ref = const_cast<Matrix&>(a); // for BLAS calls
Matrix& b_ref = const_cast<Matrix&>(b);
try
{
int aRows = a_ref.Nrows ();
int aCols = a_ref.Ncols ();
int bRows = b_ref.Nrows ();
int bCols = b_ref.Ncols ();
int cRows = c.Nrows ();
int cCols = c.Ncols ();
if (conjA == 'n' && conjB == 'n')
{
assert ((aCols == bRows) && (cRows == aRows) && (cCols == bCols));
#ifdef BLAS
GEMM ('n', 'n', 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')
{
assert ((aCols == bCols) && (cRows == aRows) && (cCols == bRows));
#ifdef BLAS
GEMM ('t', 'n', 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')
{
assert ((aRows == bRows) && (cRows == aCols) && (cCols == bCols));
#ifdef BLAS
GEMM ('n', 't', 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')
{
assert ((aRows == bCols) && (cRows == aCols) && (cCols == bRows));
#ifdef BLAS
GEMM ('t', 't', 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
}
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::MatrixRotate (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 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;
}