-
Notifications
You must be signed in to change notification settings - Fork 15
/
nist_spblas.cc
3692 lines (2966 loc) · 93.6 KB
/
nist_spblas.cc
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
typedef int spmat_t;
#ifndef MKL
/* this part wraps and implements the NIST interface */
/*
*
* Sparse BLAS (Basic Linear Algebra Subprograms) Library
*
* A C++ implementation of the routines specified by the ANSI C
* interface specification of the Sparse BLAS in the BLAS Technical
* Forum Standard[1]. For details, see [2].
*
* Mathematical and Computational Sciences Division
* National Institute of Technology,
* Gaithersburg, MD USA
*
*
* [1] BLAS Technical Forum: www.netlib.org/blas/blast-forum/
* [2] I. S. Duff, M. A. Heroux, R. Pozo, "An Overview of the Sparse Basic
* Linear Algebra Subprograms: The new standard of the BLAS Techincal
* Forum," Vol. 28, No. 2, pp. 239-267,ACM Transactions on Mathematical
* Software (TOMS), 2002.
*
*
* DISCLAIMER:
*
* This software was developed at the National Institute of Standards and
* Technology (NIST) by employees of the Federal Government in the course
* of their official duties. Pursuant to title 17 Section 105 of the
* United States Code, this software is not subject to copyright protection
* and is in the public domain. NIST assumes no responsibility whatsoever for
* its use by other parties, and makes no guarantees, expressed or implied,
* about its quality, reliability, or any other characteristic.
*
*
*/
/* numeric is for accumulate() below */
#include <iostream>
#include <complex>
#include <numeric>
#include <vector>
#include <utility>
/* pair defined here */
#include "blas_enum.h"
#include "blas_sparse_proto.h"
#include <cstring> /* for memset */
#include <cassert> /* for assert */
#ifdef SPBLAS_ERROR_FATAL
#include <cassert>
#define ASSERT_RETURN(x, ret_val) assert(x)
#define ERROR_RETURN(ret_val) assert(0)
#else
#define ASSERT_RETURN(x, ret_val) {if (!(x)) return ret_val;}
#define ERROR_RETURN(ret_val) return ret_val
#endif
using namespace std;
namespace NIST_SPBLAS
{
/**
Generic sparse matrix (base) class: defines only the structure
(size, symmetry, etc.) and maintains state during construction,
but does not specify the actual nonzero values, or their type.
*/
class Sp_mat
{
private:
int num_rows_;
int num_cols_;
int num_nonzeros_;
/* ... */
int void_;
int nnew_; /* avoid using "new" since it is a C++ keyword */
int open_;
int valid_;
int unit_diag_ ;
int complex_;
int real_;
int single_precision_;
int double_precision_;
int upper_triangular_;
int lower_triangular_;
int upper_symmetric_;
int lower_symmetric_;
int upper_hermitian_;
int lower_hermitian_;
int general_;
int one_base_;
/* optional block information */
int Mb_; /* matrix is partitioned into Mb x Nb blocks */
int Nb_; /* otherwise 0, if regular (non-blocked) matrix */
int k_; /* for constant blocks, each block is k x l */
int l_; /* otherwise 0, if variable blocks are used. */
int rowmajor_; /* 1,if block storage is rowm major. */
int colmajor_; /* 1,if block storage is column major. */
/* unused optimization paramters */
int opt_regular_;
int opt_irregular_;
int opt_block_;
int opt_unassembled_;
vector<int> K_; /* these are GLOBAL index of starting point of block */
vector<int> L_; /* i.e. block(i,j) starts at global location (K[i],L[i]) */
/* and of size (K[i+1]-K[i] x L[i+1]-L[i]) */
public:
Sp_mat(int M, int N) :
num_rows_(M), /* default construction */
num_cols_(N),
num_nonzeros_(0),
void_(0),
nnew_(1),
open_(0),
valid_(0),
unit_diag_(0),
complex_(0),
real_(0),
single_precision_(0),
double_precision_(0),
upper_triangular_(0),
lower_triangular_(0),
upper_symmetric_(0),
lower_symmetric_(0),
upper_hermitian_(0),
lower_hermitian_(0),
general_(0),
one_base_(0),
Mb_(0),
Nb_(0),
k_(0),
l_(0),
rowmajor_(0),
colmajor_(0),
opt_regular_(0),
opt_irregular_(1),
opt_block_(0),
opt_unassembled_(0),
K_(),
L_()
{}
int& num_rows() { return num_rows_; }
int& num_cols() { return num_cols_; }
int& num_nonzeros() { return num_nonzeros_;}
int num_rows() const { return num_rows_; }
int num_cols() const { return num_cols_; }
int num_nonzeros() const { return num_nonzeros_;}
int is_one_base() const { return (one_base_ ? 1 : 0); }
int is_zero_base() const { return (one_base_ ? 0 : 1); }
int is_void() const { return void_; }
int is_new() const { return nnew_; }
int is_open() const { return open_; }
int is_valid() const { return valid_; }
int is_unit_diag() const { return unit_diag_; }
int is_complex() const { return complex_;}
int is_real() const { return real_;}
int is_single_precision() const { return single_precision_;}
int is_double_precision() const { return double_precision_;}
int is_upper_triangular() const { return upper_triangular_;}
int is_lower_triangular() const { return lower_triangular_;}
int is_triangular() const { return upper_triangular_ ||
lower_triangular_; }
int is_lower_symmetric() const { return lower_symmetric_; }
int is_upper_symmetric() const { return upper_symmetric_; }
int is_symmetric() const { return upper_symmetric_ ||
lower_symmetric_; }
int is_lower_hermitian() const { return lower_hermitian_; }
int is_upper_hermitian() const { return upper_hermitian_; }
int is_hermitian() const { return lower_hermitian_ ||
upper_hermitian_; }
int is_general() const { return !( is_hermitian() || is_symmetric()) ; }
int is_lower_storage() const { return is_lower_triangular() ||
is_lower_symmetric() ||
is_lower_hermitian() ; }
int is_upper_storage() const { return is_upper_triangular() ||
is_upper_symmetric() ||
is_upper_hermitian() ; }
int is_opt_regular() const { return opt_regular_; }
int is_opt_irregular() const { return opt_irregular_; }
int is_opt_block() const { return opt_block_;}
int is_opt_unassembled() const { return opt_unassembled_;}
int K(int i) const { return (k_ ? i*k_ : K_[i] ); }
int L(int i) const { return (l_ ? i*l_ : L_[i] ); }
int is_rowmajor() const { return rowmajor_; }
int is_colmajor() const { return colmajor_; }
void set_one_base() { one_base_ = 1; }
void set_zero_base() { one_base_ = 0; }
void set_void() { void_ = 1; nnew_ = open_ = valid_ = 0;}
void set_new() { nnew_ = 1; void_ = open_ = valid_ = 0;}
void set_open() { open_ = 1; void_ = nnew_ = valid_ = 0;}
void set_valid() { valid_ = 1; void_ = nnew_ = open_ = 0; }
void set_unit_diag() { unit_diag_ = 1;}
void set_complex() {complex_ = 1; }
void set_real() { real_ = 1; }
void set_single_precision() { single_precision_ = 1; }
void set_double_precision() { double_precision_ = 1; }
void set_upper_triangular() { upper_triangular_ = 1; }
void set_lower_triangular() { lower_triangular_ = 1; }
void set_upper_symmetric() { upper_symmetric_ = 1; }
void set_lower_symmetric() { lower_symmetric_ = 1; }
void set_upper_hermitian() { upper_hermitian_ = 1; }
void set_lower_hermitian() { lower_hermitian_ = 1; }
void set_const_block_parameters(int Mb, int Nb, int k, int l)
{
Mb_ = Mb;
Nb_ = Nb;
k_ = k;
l_ = l;
}
void set_var_block_parameters(int Mb, int Nb, const int *k, const int *l)
{
Mb_ = Mb;
Nb_ = Nb;
k_ = 0;
l_ = 0;
K_.resize(Mb+1);
K_[0] = 0;
for (int i=0; i<Mb; i++)
K_[i+1] = k[i] + K_[i];
L_.resize(Nb+1);
L_[0] = 0;
for (int j=0; j<Mb; j++)
K_[j+1] = k[j] + K_[j];
}
virtual int end_construction()
{
if (is_open() || is_new())
{
set_valid();
return 0;
}
else
ERROR_RETURN(1);
}
virtual void print() const;
virtual void destroy() {};
virtual ~Sp_mat() {};
};
template <class T>
class TSp_mat : public Sp_mat
{
private:
vector< vector< pair<T, int> > > S;
vector<T> diag; /* optional diag if matrix is
triangular. Created
at end_construction() phase */
private:
inline T sp_dot_product( const vector< pair<T, int> > &r,
const T* x, int incx ) const
{
T sum(0);
if (incx == 1)
{
for ( typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
{
//sum = sum + p->first * x[p->second];
sum += p->first * x[p->second];
}
}
else /* incx != 1 */
{
for ( typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
{
//sum = sum + p->first * x[p->second * incx];
sum += p->first * x[p->second * incx];
}
}
return sum;
}
inline T sp_conj_dot_product( const vector< pair<T, int> > &r,
const T* x, int incx ) const
{
T sum(0);
if (incx == 1)
{
for ( typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
{
sum += (p->first) * x[p->second];
}
}
else /* incx != 1 */
{
for ( typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
{
//sum = sum + p->first * x[p->second * incx];
sum += (p->first) * x[p->second * incx];
}
}
return sum;
}
inline void sp_axpy( const T& alpha, const vector< pair<T,int> > &r,
T* y, int incy) const
{
if (incy == 1)
{
for (typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
y[p->second] += alpha * p->first;
}
else /* incy != 1 */
{
for (typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
y[incy * p->second] += alpha * p->first;
}
}
inline void sp_conj_axpy( const T& alpha, const vector< pair<T,int> > &r,
T* y, int incy) const
{
if (incy == 1)
{
for (typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
y[p->second] += alpha * p->first;
}
else /* incy != 1 */
{
for (typename vector< pair<T,int> >::const_iterator p = r.begin();
p < r.end(); p++)
y[incy * p->second] += alpha * p->first;
}
}
void mult_diag(const T& alpha, const T* x, int incx, T* y, int incy)
const
{
const T* X = x;
T* Y = y;
typename vector<T>::const_iterator d= diag.begin();
for ( ; d < diag.end(); X+=incx, d++, Y+=incy)
{
*Y += alpha * *d * *X;
}
}
void mult_conj_diag(const T& alpha, const T* x, int incx, T* y, int incy)
const
{
const T* X = x;
T* Y = y;
typename vector<T>::const_iterator d= diag.begin();
for ( ; d < diag.end(); X+=incx, d++, Y+=incy)
{
*Y += alpha * (*d) * *X;
}
}
void nondiag_mult_vec(const T& alpha, const T* x, int incx,
T* y, int incy) const
{
int M = num_rows();
if (incy == 1)
{
for (int i=0; i<M; i++)
y[i] += alpha * sp_dot_product(S[i], x, incx);
}
else
{
for (int i=0; i<M; i++)
y[i * incy] += alpha * sp_dot_product(S[i], x, incx);
}
}
void nondiag_mult_vec_conj(const T& alpha, const T* x, int incx,
T* y, int incy) const
{
int M = num_rows();
if (incy == 1)
{
for (int i=0; i<M; i++)
y[i] += alpha * sp_conj_dot_product(S[i], x, incx);
}
else
{
for (int i=0; i<M; i++)
y[i * incy] += alpha * sp_conj_dot_product(S[i], x, incx);
}
}
void nondiag_mult_vec_transpose(const T& alpha, const T* x, int incx,
T* y, int incy) const
{
/* saxpy: y += (alpha * x[i]) row[i] */
int M = num_rows();
const T* X = x;
for (int i=0; i<M; i++, X += incx)
sp_axpy( alpha * *X, S[i], y, incy);
}
void nondiag_mult_vec_conj_transpose(const T& alpha, const T* x, int incx,
T* y, int incy) const
{
/* saxpy: y += (alpha * x[i]) row[i] */
int M = num_rows();
const T* X = x;
for (int i=0; i<M; i++, X += incx)
sp_conj_axpy( alpha * *X, S[i], y, incy);
}
void mult_vec(const T& alpha, const T* x, int incx, T* y, int incy)
const
{
nondiag_mult_vec(alpha, x, incx, y, incy);
if (is_triangular() || is_symmetric())
mult_diag(alpha, x, incx, y, incy);
if (is_symmetric())
nondiag_mult_vec_transpose(alpha, x, incx, y, incy);
}
void mult_vec_transpose(const T& alpha, const T* x, int incx, T* y,
int incy) const
{
nondiag_mult_vec_transpose(alpha, x, incx, y, incy);
if (is_triangular() || is_symmetric())
mult_diag(alpha, x, incx, y, incy);
if (is_symmetric())
nondiag_mult_vec(alpha, x, incx, y, incy);
}
void mult_vec_conj_transpose(const T& alpha, const T* x, int incx, T* y,
int incy) const
{
nondiag_mult_vec_conj_transpose(alpha, x, incx, y, incy);
if (is_triangular() || is_symmetric())
mult_conj_diag(alpha, x, incx, y, incy);
if (is_symmetric())
nondiag_mult_vec_conj(alpha, x, incx, y, incy);
}
int triangular_solve(T alpha, T* x, int incx ) const
{
if (alpha == (T) 0.0)
ERROR_RETURN(1);
if ( ! is_triangular() )
ERROR_RETURN(1);
int N = num_rows();
if (is_lower_triangular())
{
for (int i=0, ii=0; i<N; i++, ii += incx)
{
x[ii] = (x[ii] - sp_dot_product(S[i], x, incx)) / diag[i];
}
if (alpha != (T) 1.0)
{
for (int i=0, ii=0; i<N; i++, ii += incx)
x[ii] /= alpha;
}
}
else if (is_upper_triangular())
{
for (int i=N-1, ii=(N-1)*incx ; 0<=i ; i--, ii-=incx)
{
x[ii] = (x[ii] - sp_dot_product(S[i],x, incx)) / diag[i];
}
if (alpha != (T) 1.0)
{
for (int i=N-1, ii=(N-1)*incx ; 0<=i ; i--, ii-=incx)
x[ii] /= alpha;
}
}
else
ERROR_RETURN(1);
return 0;
}
int transpose_triangular_solve(T alpha, T* x, int incx) const
{
if ( ! is_triangular())
return -1;
int N = num_rows();
if (is_lower_triangular())
{
for (int j=N-1, jj=(N-1)*incx; 0<=j; j--, jj -= incx)
{
x[jj] /= diag[j] ;
sp_axpy( -x[jj], S[j], x, incx);
}
if (alpha != (T) 1.0)
{
for (int jj=(N-1)*incx; 0<=jj; jj -=incx)
x[jj] /= alpha;
}
}
else if (is_upper_triangular())
{
for (int j=0, jj=0; j<N; j++, jj += incx)
{
x[jj] /= diag[j];
sp_axpy(- x[jj], S[j], x, incx);
}
if (alpha != (T) 1.0)
{
for (int jj=(N-1)*incx; 0<=jj; jj -=incx)
x[jj] /= alpha;
}
}
else
ERROR_RETURN(1);
return 0;
}
int transpose_triangular_conj_solve(T alpha, T* x, int incx) const
{
if ( ! is_triangular())
return -1;
int N = num_rows();
if (is_lower_triangular())
{
for (int j=N-1, jj=(N-1)*incx; 0<=j; j--, jj -= incx)
{
x[jj] /= (diag[j]) ;
sp_conj_axpy( -x[jj], S[j], x, incx);
}
if (alpha != (T) 1.0)
{
for (int jj=(N-1)*incx; 0<=jj; jj -=incx)
x[jj] /= alpha;
}
}
else if (is_upper_triangular())
{
for (int j=0, jj=0; j<N; j++, jj += incx)
{
x[jj] /= (diag[j]);
sp_conj_axpy(- x[jj], S[j], x, incx);
}
if (alpha != (T) 1.0)
{
for (int jj=(N-1)*incx; 0<=jj; jj -=incx)
x[jj] /= alpha;
}
}
else
ERROR_RETURN(1);
return 0;
}
public:
inline T& val(pair<T, int> &VP) { return VP.first; }
inline int& col_index(pair<T,int> &VP) { return VP.second; }
inline const T& val(pair<T, int> const &VP) const { return VP.first; }
inline int col_index(pair<T,int> const &VP) const { return VP.second; }
TSp_mat( int M, int N) : Sp_mat(M,N), S(M), diag() {}
/* custom ctors */
/* row select ctor */
typedef typename vector< vector< pair<T, int> > >::const_iterator Siter_t;
TSp_mat( int N, Siter_t first, Siter_t last) : Sp_mat(last-first,N), S(first, last), diag() { }
/* csr ctor */
TSp_mat( int M, int N, T *x, int *row, int *col ) : Sp_mat(M, N), S(M), diag()
{
for (int i = 0; i < M; i++)
for (int j = 0; j < (row[i+1] - row[i]); j++)
{
S[i].push_back(make_pair(x[row[i] + j], col[row[i] + j]));
}
num_nonzeros() = row[M];
}
void destroy()
{
// set vector sizes to zero
(vector<T>(0)).swap(diag);
(vector< vector< pair<T, int> > > (0) ).swap(S);
}
/**
This function is the entry point for all of the insert routines in
this implementation. It fills the sparse matrix, one entry at a time.
If matrix is declared unit_diagonal, then inserting any diagonal
values is ignored. If it is symmetric (upper/lower) or triangular
(upper/lower) inconsistent values are not caught. (That is, entries
into the upper region of a lower triangular matrix is not reported.)
[NOTE: the base is determined at the creation phase, and can be determined
by testing whether BLAS_usgp(A, blas_one_base) returns 1. If it returns 0,
then offsets are zero based.]
@param val the numeric value of entry A(i,j)
@param i the row index of A(i,j)
@param j the column index of A(i,j)
@return 0 if succesful, 1 otherwise
*/
int insert_entry(T val, int i, int j)
{
if (is_one_base())
{
i--;
j--;
}
/* make sure the indices are in range */
ASSERT_RETURN(i >= 0, 1);
ASSERT_RETURN(i < num_rows(), 1);
ASSERT_RETURN(j >= 0, 1);
ASSERT_RETURN(j < num_cols(), 1);
/* allocate space for the diagonal, if this is the first time
* trying to insert values.
*/
if (is_new())
{
set_open();
if (is_triangular() || is_symmetric())
{
diag.resize(num_rows());
if (is_unit_diag())
{
for (unsigned int ii=0; ii< diag.size(); ii++)
diag[ii] = T(1.0);
}
else
{
for (unsigned int ii=0; ii< diag.size(); ii++)
diag[ii] = (T) 0.0;
}
}
}
if (is_open())
{
if (i==j && (is_triangular() || is_symmetric() || is_hermitian()) )
{
if (!is_unit_diag())
{
diag[i] += val;
}
else /* if unit diagonal */
{
if (val != (T) 1)
ERROR_RETURN(0); /* tries to insert non-unit diagonal */
}
if (is_upper_storage() && i > j)
ERROR_RETURN(0); /* tries to fill lower-triangular region */
else
if (is_lower_storage() && i < j)
ERROR_RETURN(0); /* tries to fill upper-triangular region */
}
else
{
S[i].push_back( make_pair(val, j) );
}
num_nonzeros() ++;
}
return 0;
}
int insert_entries( int nz, const T* Val, const int *I, const int *J)
{
for (int i=0; i<nz; i++)
{
insert_entry(Val[i], I[i], J[i]) ;
}
return 0;
}
int insert_row(int k, int nz, const T* Val, const int *J)
{
for (int i=0; i<nz; i++)
insert_entry(Val[i], k, J[i]);
return 0;
}
int insert_col(int k, int nz, const T* Val, const int *I)
{
for (int i=0; i<nz; i++)
insert_entry(Val[i], I[i], k);
return 0;
}
int insert_block(const T* Val, int row_stride,
int col_stride, int bi, int bj)
{
/* translate from block index to global indices */
int Iend = K(bi+1);
int Jend = L(bj+1);
for (int i=K(bi), r=0; i<Iend; i++, r += row_stride)
for (int j=L(bi); j<Jend; j++, r += col_stride)
insert_entry( Val[r], i, j );
return 0;
}
int end_construction()
{
return Sp_mat::end_construction();
}
int usmv(enum blas_trans_type transa, const T& alpha, const T* x , int incx,
T* y, int incy) const
{
ASSERT_RETURN(is_valid(), -1);
if (transa == blas_no_trans)
mult_vec(alpha, x, incx, y, incy);
else
if (transa == blas_conj_trans)
mult_vec_conj_transpose(alpha, x, incx, y, incy);
else
if ( transa == blas_trans)
mult_vec_transpose(alpha, x, incx, y, incy);
else
ERROR_RETURN(1);
return 0;
}
int usmm(enum blas_order_type ordera, enum blas_trans_type transa,
int nrhs, const T& alpha, const T* b, int ldb, T* C, int ldC) const
{
if (ordera == blas_rowmajor)
{
/* for each column of C, perform a mat_vec */
for (int i=0; i<nrhs; i++)
{
usmv( transa, alpha, &b[i], ldb, &C[i], ldC );
}
return 0;
}
else
if (ordera == blas_colmajor)
{
/* for each column of C, perform a mat_vec */
for (int i=0; i<nrhs; i++)
{
usmv( transa, alpha, &b[i*ldb], 1, &C[i*ldC], 1 );
}
return 0;
}
else
ERROR_RETURN(1);
}
int ussv( enum blas_trans_type transa, const T& alpha, T* x, int incx) const
{
if (transa == blas_trans)
return transpose_triangular_solve(alpha, x, incx);
else
if (transa == blas_conj_trans)
return transpose_triangular_conj_solve(alpha, x, incx);
else
if (transa == blas_no_trans)
return triangular_solve(alpha, x, incx);
else
ERROR_RETURN(1);
}
int ussm( enum blas_order_type ordera, enum blas_trans_type transa, int nrhs,
const T& alpha, T* C, int ldC) const
{
if (ordera == blas_rowmajor)
{
/* for each column of C, perform a usmv */
for (int i=0; i<nrhs; i++)
{
ussv(
transa, alpha, &C[i], ldC );
}
return 0;
}
else
if (ordera == blas_colmajor)
{
/* for each column of C, perform a mat_vec */
for (int i=0; i<nrhs; i++)
{
ussv( transa, alpha, &C[i*ldC], 1 );
}
return 0;
}
else
ERROR_RETURN(1);
}
void print() const
{
Sp_mat::print(); /* print matrix header info */
/* if there is actual data, print out contents */
for (int i=0; i<num_rows(); i++)
for (unsigned int j=0; j< S[i].size(); j++)
cout << i << " " << col_index(S[i][j]) <<
" " << val(S[i][j]) << "\n";
/* if matrix is triangular, print out diagonals */
if (is_upper_triangular() || is_lower_triangular())
{
for (unsigned int i=0; i< diag.size(); i++)
cout << i << " " << i << " " << diag[i] << "\n";
}
}
/* custom, these functions assume irregular structure */
inline void colmeans( T *means )
{
for (typename vector< vector< pair<T, int> > >::const_iterator i = S.begin(); i < S.end(); i++)
for (typename vector< pair<T, int> >::const_iterator v = i->begin(); v < i->end(); v++)
means[v->second] += v->first;
for (int j = 0; j < num_cols(); j++)
means[j] /= num_rows();
}
inline void rowmeans( T *means )
{
for (int i = 0; i < num_rows(); i++)
for (typename vector< pair<T, int> >::const_iterator v = S[i].begin(); v < S[i].end(); v++)
means[i] += v->first;
for (int i = 0; i < num_rows(); i++)
means[i] /= num_cols();
}
inline void colvars( T *vars )
{
vector<T> means(num_cols());
vector<int> nnz(num_cols());
colmeans(&means[0]);
for (typename vector< vector< pair<T, int> > >::const_iterator i = S.begin(); i < S.end(); i++)
for (typename vector< pair<T, int> >::const_iterator v = i->begin(); v < i->end(); v++)
{
const T x = v->first - means[v->second];
vars[v->second] += x * x;
nnz[v->second]++;
}
for (int j = 0; j < num_cols(); j++)
{
vars[j] += means[j] * means[j] * (num_rows() - nnz[j]);
vars[j] /= num_rows();
}
}
inline void rowvars( T * vars )
{
vector<T> means(num_rows());
vector<int> nnz(num_rows());
rowmeans(&means[0]);
for (int i = 0; i < num_rows(); i++)
for (typename vector< pair<T, int> >::const_iterator v = S[i].begin(); v < S[i].end(); v++)
{
const T x = v->first - means[i];
vars[i] += x * x;
nnz[i]++;
}