-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhts_impl_def.hpp
4463 lines (4170 loc) · 138 KB
/
hts_impl_def.hpp
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
// ************************************************************************
//
// ShyLU: Hybrid preconditioner package
// Copyright 2012 Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact A.M. Bradley ([email protected])
//
// ************************************************************************
#ifndef INCLUDE_HTS_IMPL_DEF_HPP
#define INCLUDE_HTS_IMPL_DEF_HPP
#ifdef _OPENMP
# include <omp.h>
#else
namespace htsimpl {
// Protect against a header that has #define'd replacements for OpenMP
// functions.
#ifndef omp_get_max_threads
inline int omp_get_max_threads () { return 1; }
#endif
#ifndef omp_set_num_threads
inline void omp_set_num_threads (const int&) {}
#endif
#ifndef omp_get_num_threads
inline int omp_get_num_threads () { return 1; }
#endif
#ifndef omp_get_thread_num
inline int omp_get_thread_num () { return 0; }
#endif
}
#endif
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>
#define F77_BLAS_MANGLE(name,NAME) name ## _
#ifdef HTS_USE_COMPLEX
# include <complex>
#endif
#ifdef HTS_USE_MKL
# ifdef HTS_USE_COMPLEX
# ifndef MKL_Complex8
# define MKL_Complex8 std::complex<float>
# endif
# ifndef MKL_Complex16
# define MKL_Complex16 std::complex<double>
# endif
# endif
# include <mkl.h>
#endif
#include "hts_impl.hpp"
//#define HTS_TIME
#ifdef HTS_TIME
//# define HTS_TIMENUM
# include <sys/time.h>
#endif
namespace htsimpl {
static const int parfor_static_size = 20;
#ifndef HTS_NO_BLAS
//todo Make this configurable.
typedef int blas_int;
template<typename T> void gemm(
char transa, char transb, blas_int m, blas_int nrhs, blas_int n, T alpha,
const T* a, blas_int lda, const T* b, blas_int ldb, T beta,
const T* c, blas_int ldc);
extern "C" {
void F77_BLAS_MANGLE(sgemm,SGEMM)(
char*, char*, blas_int*, blas_int*, blas_int*, float*, float*, blas_int*,
float*, blas_int*, float*, float*, blas_int*);
void F77_BLAS_MANGLE(dgemm,DGEMM)(
char*, char*, blas_int*, blas_int*, blas_int*, double*, double*, blas_int*,
double*, blas_int*, double*, double*, blas_int*);
#ifdef HTS_USE_COMPLEX
void F77_BLAS_MANGLE(cgemm,CGEMM)(
char*, char*, blas_int*, blas_int*, blas_int*, std::complex<float>*,
std::complex<float>*, blas_int*, std::complex<float>*, blas_int*,
std::complex<float>*, std::complex<float>*, blas_int*);
void F77_BLAS_MANGLE(zgemm,ZGEMM)(
char*, char*, blas_int*, blas_int*, blas_int*, std::complex<double>*,
std::complex<double>*, blas_int*, std::complex<double>*, blas_int*,
std::complex<double>*, std::complex<double>*, blas_int*);
#endif
}
template<> inline void gemm<float> (
char transa, char transb, blas_int m, blas_int nrhs, blas_int n, float alpha,
const float* a, blas_int lda, const float* b, blas_int ldb, float beta,
const float* c, blas_int ldc)
{
F77_BLAS_MANGLE(sgemm,SGEMM)(
&transa, &transb, &m, &nrhs, &n, &alpha, const_cast<float*>(a), &lda,
const_cast<float*>(b), &ldb, &beta, const_cast<float*>(c), &ldc);
}
template<> inline void gemm<double> (
char transa, char transb, blas_int m, blas_int nrhs, blas_int n, double alpha,
const double* a, blas_int lda, const double* b, blas_int ldb, double beta,
const double* c, blas_int ldc)
{
F77_BLAS_MANGLE(dgemm,DGEMM)(
&transa, &transb, &m, &nrhs, &n, &alpha, const_cast<double*>(a), &lda,
const_cast<double*>(b), &ldb, &beta, const_cast<double*>(c), &ldc);
}
#ifdef HTS_USE_COMPLEX
template<> inline void gemm<std::complex<float> > (
char transa, char transb, blas_int m, blas_int nrhs, blas_int n,
std::complex<float> alpha, const std::complex<float>* a, blas_int lda,
const std::complex<float>* b, blas_int ldb, std::complex<float> beta,
const std::complex<float>* c, blas_int ldc)
{
F77_BLAS_MANGLE(cgemm,CGEMM)(
&transa, &transb, &m, &nrhs, &n, &alpha,
const_cast<std::complex<float>*>(a), &lda,
const_cast<std::complex<float>*>(b), &ldb, &beta,
const_cast<std::complex<float>*>(c), &ldc);
}
template<> inline void gemm<std::complex<double> > (
char transa, char transb, blas_int m, blas_int nrhs, blas_int n,
std::complex<double> alpha, const std::complex<double>* a, blas_int lda,
const std::complex<double>* b, blas_int ldb, std::complex<double> beta,
const std::complex<double>* c, blas_int ldc)
{
F77_BLAS_MANGLE(zgemm,ZGEMM)(
&transa, &transb, &m, &nrhs, &n, &alpha,
const_cast<std::complex<double>*>(a), &lda,
const_cast<std::complex<double>*>(b), &ldb, &beta,
const_cast<std::complex<double>*>(c), &ldc);
}
#endif
#endif
#ifdef HTS_USE_MKL
// sparse A * dense x
template<typename T> void hts_mkl_csrmm(
const bool transp, const MKL_INT m, const MKL_INT n, const T* d,
const MKL_INT* ir, const MKL_INT* jc, const T* x, const int ldx,
T* y, const int ldy, const MKL_INT nrhs);
template<> inline void hts_mkl_csrmm<float> (
const bool transp, const MKL_INT m, const MKL_INT n, const float* d,
const MKL_INT* ir, const MKL_INT* jc, const float* x, const int ldx,
float* y, const int ldy, const MKL_INT nrhs)
{
char transa = transp ? 'T' : 'N';
static const char A_descr[6] = {'G', '*', '*', 'C', '*', '*'};
float alpha = -1, beta = 1;
for (int k = 0; k < nrhs; ++k)
mkl_scsrmv(
&transa, const_cast<MKL_INT*>(&m), const_cast<MKL_INT*>(&n),
&alpha, const_cast<char*>(A_descr), const_cast<float*>(d),
const_cast<MKL_INT*>(jc), const_cast<MKL_INT*>(ir),
const_cast<MKL_INT*>(ir+1), const_cast<float*>(x + k*ldx), &beta,
y + k*ldy);
}
template<> inline void hts_mkl_csrmm<double> (
const bool transp, const MKL_INT m, const MKL_INT n, const double* d,
const MKL_INT* ir, const MKL_INT* jc, const double* x, const int ldx,
double* y, const int ldy, const MKL_INT nrhs)
{
char transa = transp ? 'T' : 'N';
static const char A_descr[6] = {'G', '*', '*', 'C', '*', '*'};
double alpha = -1, beta = 1;
for (int k = 0; k < nrhs; ++k)
mkl_dcsrmv(
&transa, const_cast<MKL_INT*>(&m), const_cast<MKL_INT*>(&n),
&alpha, const_cast<char*>(A_descr), const_cast<double*>(d),
const_cast<MKL_INT*>(jc), const_cast<MKL_INT*>(ir),
const_cast<MKL_INT*>(ir+1), const_cast<double*>(x + k*ldx), &beta,
y + k*ldy);
}
#ifdef HTS_USE_COMPLEX
template<> inline void hts_mkl_csrmm<std::complex<float> > (
const bool transp, const MKL_INT m, const MKL_INT n,
const std::complex<float>* d, const MKL_INT* ir, const MKL_INT* jc,
const std::complex<float>* x, const int ldx, std::complex<float>* y,
const int ldy, const MKL_INT nrhs)
{
char transa = transp ? 'T' : 'N';
static const char A_descr[6] = {'G', '*', '*', 'C', '*', '*'};
std::complex<float> alpha(-1, 0), beta(1, 0);
for (int k = 0; k < nrhs; ++k)
mkl_ccsrmv(
&transa, const_cast<MKL_INT*>(&m), const_cast<MKL_INT*>(&n),
&alpha, const_cast<char*>(A_descr), const_cast<std::complex<float>*>(d),
const_cast<MKL_INT*>(jc), const_cast<MKL_INT*>(ir),
const_cast<MKL_INT*>(ir+1), const_cast<std::complex<float>*>(x + k*ldx),
&beta, y + k*ldy);
}
template<> inline void hts_mkl_csrmm<std::complex<double> > (
const bool transp, const MKL_INT m, const MKL_INT n,
const std::complex<double>* d, const MKL_INT* ir, const MKL_INT* jc,
const std::complex<double>* x, const int ldx, std::complex<double>* y,
const int ldy, const MKL_INT nrhs)
{
char transa = transp ? 'T' : 'N';
static const char A_descr[6] = {'G', '*', '*', 'C', '*', '*'};
std::complex<double> alpha(-1, 0), beta(1, 0);
for (int k = 0; k < nrhs; ++k)
mkl_zcsrmv(
&transa, const_cast<MKL_INT*>(&m), const_cast<MKL_INT*>(&n),
&alpha, const_cast<char*>(A_descr), const_cast<std::complex<double>*>(d),
const_cast<MKL_INT*>(jc), const_cast<MKL_INT*>(ir),
const_cast<MKL_INT*>(ir+1), const_cast<std::complex<double>*>(x + k*ldx),
&beta, y + k*ldy);
}
#endif
#endif
namespace {
class Timer {
public:
enum Op { total_pre = 0,
setup, transpose, tolower, perm,
lsetfind,
lsetinit, ls_1, ls_2, ls_3, ls_4, ls_4a, ls_4b, ls_4c, ls_4d, ls_5,
lsetinitp2p, lsp2p_1, lsp2p_3, lsp2p_6,
dpb, dpb_getmatrix, dpb_sort, dpb_tinit,
dpb_tinit_2, dpb_tinit_3,
for_reprocess,
NSETUPTIMERS,
total_re, numthr, numpart, numls, numrbt, numrbm, numperm,
NNUMTIMERS,
slvlls, slvlother, slvuls, slvuother,
NTIMERS };
static inline void init () {
#ifdef HTS_TIME
for (int i = 0; i < NTIMERS; ++i) et_[i] = 0;
#endif
}
static inline void start (const Op op) {
#ifdef HTS_TIME
gettimeofday(&t_start_[op], 0);
#endif
}
static inline void stop (const Op op) {
#ifdef HTS_TIME
timeval t2;
gettimeofday(&t2, 0);
const timeval& t1 = t_start_[op];
static const double us = 1.0e6;
et_[op] += (t2.tv_sec*us + t2.tv_usec - t1.tv_sec*us - t1.tv_usec)/us;
#endif
}
# define tpr(op) do { \
printf("%20s %10.3e %10.1f\n", #op, et_[op], 100*et_[op]/tot); \
} while (0)
#ifdef HTS_TIME
static void print_setup () {
const double tot = et_[total_pre];
tpr(setup); tpr(transpose); tpr(tolower);
tpr(lsetfind);
tpr(perm);
tpr(lsetinit); //tpr(ls_1); tpr(ls_2); tpr(ls_3); tpr(ls_4); tpr(ls_4a);
//tpr(ls_4b); tpr(ls_4c); tpr(ls_4d); tpr(ls_5);
tpr(lsetinitp2p); //tpr(lsp2p_1); tpr(lsp2p_3); tpr(lsp2p_6);
tpr(dpb); tpr(dpb_getmatrix); tpr(dpb_sort); tpr(dpb_tinit);
tpr(dpb_tinit_2); tpr(dpb_tinit_3);
printf("%20s %10.3e %10.1f\n", "total", et_[total_pre], 100.0);
}
#endif
#ifdef HTS_TIMENUM
static void print_numeric () {
const double tot = et_[total_re];
tpr(numthr); tpr(numpart); tpr(numls); tpr(numrbt); tpr(numrbm);
tpr(numperm);
printf("%20s %10.3e %10.1f\n", "total", et_[total_re], 100.0);
}
#endif
#undef tpr
private:
#ifdef HTS_TIME
static timeval t_start_[NTIMERS];
static double et_[NTIMERS];
#endif
};
#ifdef HTS_TIME
timeval Timer::t_start_[Timer::NTIMERS];
double Timer::et_[Timer::NTIMERS];
#endif
} // namespace
template<typename T> inline void touch (T* const p, const size_t n,
const T& init = T()) {
// 1 KB should be a safe lower bound on page size. Touch enough to touch every
// page; I don't think there's any need to touch more memory than that.
#if ! defined __MIC__
for (size_t i = 0; i < n; i += 1024 / sizeof(T))
p[i] = init;
// Make sure the last part is touched.
if (n) p[n-1] = init;
#endif
}
// In the following memcpy and memset versions, n is count rather than number of
// bytes.
template<typename T>
inline void memcpy (T* const dst, const T* const src, const std::size_t n) {
std::memcpy(dst, src, n*sizeof(T));
}
template<typename T>
inline void memset (T* const dst, const T val, const std::size_t n) {
for (std::size_t i = 0; i < n; ++i) dst[i] = val;
}
template<> inline void
memset (float* const dst, const float val, const std::size_t n)
{ std::memset(dst, val, n*sizeof(float)); }
template<> inline void
memset (double* const dst, const double val, const std::size_t n)
{ std::memset(dst, val, n*sizeof(double)); }
template<typename T> inline T*
allocn (const size_t n, const bool first_touch = false) {
if ( ! n) return 0;
T* p = new T[n];
if (first_touch) touch(p, n);
return p;
}
template<typename T> inline void deln (T*& p) {
if (p) delete[] p;
p = 0;
}
template<typename T> inline void deln_const (const T* p) {
if (p) delete[] p;
}
template<typename T> inline void del (T*& p) {
if (p) delete p;
p = 0;
}
// For exception safety when allocating.
template<typename T> class Allocnator {
T* p_;
bool dealloc_;
public:
Allocnator () : p_(0), dealloc_(true) {}
// Try to allocate memory.
Allocnator (const size_t n, const char* msg = "",
const bool first_touch = false)
: p_(0), dealloc_(true)
{ resize(n, msg, first_touch); }
void resize (const size_t n, const char* msg = "",
const bool first_touch = false) {
if (p_ && dealloc_) deln<T>(p_);
p_ = 0;
dealloc_ = true;
if ( ! n) return;
try { p_ = allocn<T>(n, first_touch); }
catch (...) {
throw hts::Exception(std::string(msg) + ": failed to allocate.");
}
}
T* get () { return p_; }
// Release the pointer to the user and subsequently don't dealloc.
T* release () { dealloc_ = false; return p_; }
// Dealloc only if the user hasn't released the pointer yet.
~Allocnator () { if (p_ && dealloc_) deln<T>(p_); }
};
template<typename T>
inline void Array<T>::init () {
n_ = cap_ = 0;
p_ = 0;
}
template<typename T>
inline Array<T>::Array (std::size_t n)
: p_(0), n_(0), cap_(0)
{ optclear_and_resize(n); }
template<typename T>
inline Array<T>::Array (std::size_t n, const T& val)
: p_(0), n_(0), cap_(0)
{ optclear_and_resize(n, val); }
template<typename T>
inline void Array<T>::clear () {
n_ = cap_ = 0;
deln(p_);
}
template<typename T>
inline void Array<T>::optclear_and_reserve (std::size_t n) {
n_ = 0;
if (n <= cap_) return;
clear();
p_ = allocn<T>(n);
cap_ = n;
}
template<typename T>
inline void Array<T>::optclear_and_reserve_ft (std::size_t n) {
n_ = 0;
if (n <= cap_) return;
clear();
p_ = allocn<T>(n, true);
cap_ = n;
}
template<typename T>
inline void Array<T>::optclear_and_resize (std::size_t n) {
if (n <= cap_) {
n_ = n;
return;
}
optclear_and_reserve(n);
n_ = n;
}
template<typename T>
inline void Array<T>::optclear_and_resize_ft (std::size_t n) {
if (n <= cap_) {
n_ = n;
return;
}
optclear_and_reserve_ft(n);
n_ = n;
}
template<typename T>
inline void Array<T>::optclear_and_resize (std::size_t n, const T& val) {
optclear_and_resize(n);
for (std::size_t i = 0; i < n_; ++i) p_[i] = val;
}
template<typename T>
inline void Array<T>::unsafe_push_back (const T& e) {
assert(n_ < cap_);
p_[n_++] = e;
}
template<typename T> inline T square (const T& x) { return x*x; }
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::
set_options (const typename ihts::Options& os, Options& od) {
od.min_block_size = os.min_block_size;
od.min_parallel_rows = os.min_parallel_rows;
od.pp_min_block_size = os.pp_min_block_size;
od.min_dense_density = os.min_dense_density;
od.ls_blk_sz = os.levelset_block_size;
od.lset_min_size = os.min_lset_size;
od.lset_min_size_scale_with_nthreads = os.lset_min_size_scale_with_nthreads;
od.lset_max_bad_fraction = os.lset_max_bad_fraction;
od.profile = os.profile;
od.printlvl = os.print_level;
}
template<typename Int, typename Size, typename Sclr> Impl<Int, Size, Sclr>::
Options::Options () {
set_options(typename HTS<Int, Size, Sclr>::Options(), *this);
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::Options::print (std::ostream& os) const {
os << " hts min_block_size " << min_block_size
<< " min_parallel_rows " << min_parallel_rows
<< " pp_min_block_size " << pp_min_block_size
<< " min_dense_density " << min_dense_density
<< " ls_blk_sz " << ls_blk_sz
<< " lset_min_size " << lset_min_size
<< " lset_min_size_scale_with_nthreads "
<< lset_min_size_scale_with_nthreads
<< " lset_max_bad_fraction " << lset_max_bad_fraction
<< " profile " << profile;
}
static void print_compiletime_options(std::ostream& os) {
#ifdef HTS_NO_BLAS
os << " HTS_NO_BLAS";
#endif
#ifdef HTS_USE_MKL
os << " HTS_USE_MKL";
#endif
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::print_options (const Options& o, std::ostream& os) {
print_compiletime_options(os);
os << std::endl;
o.print(os);
os << std::endl;
}
template<typename Int, typename Size, typename Sclr> Impl<Int, Size, Sclr>::
ConstCrsMatrix::~ConstCrsMatrix () {
if ( ! deallocate_) return;
assert( ! deallocator);
deln_const(ir); deln_const(jc); deln_const(d);
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::ConstCrsMatrix::deallocate () const {
if ( ! deallocator) return;
deallocator->counter--;
if (deallocator->counter > 0) return;
assert(deallocator->counter == 0);
deallocator->free_CrsMatrix_data();
}
template<typename Int, typename Size, typename Sclr> Impl<Int, Size, Sclr>::
CrsMatrix::~CrsMatrix () {
deln_const(ir); deln_const(jc); deln_const(d);
}
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::Partition::alloc_d () {
assert( ! cm->d);
cm->d = Allocnator<Sclr>(cm->ir[cm->m], "Partition::alloc_d").release();
}
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::Partition::alloc_A_idxs (const Size innz) {
assert( ! A_idxs);
this->nnz = innz;
A_idxs = Allocnator<Size>(nnz, "Partition::alloc_A_idxs").release();
}
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::Partition::clear () {
del(cm);
deln(A_idxs);
}
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::Partition::clear_d () { deln(cm->d); }
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::
partition_n_uniformly (const Int n, const Int nparts, Array<Int>& p) {
p.optclear_and_resize(nparts + 1);
const Int base = n / nparts;
Int rem = n - base*nparts;
Int extra = rem > 0 ? 1 : 0;
p[0] = 0;
for (Int i = 1; i <= nparts; ++i) {
p[i] = p[i-1] + base + extra;
if (rem > 0) {
--rem;
if (rem == 0) extra = 0;
}
}
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::
SparseData::init (const Int m, const Size nnz, const char* fail_msg,
const bool touch) {
free();
dealloc_ = true;
try {
ir = allocn<Size>(m+1, touch);
if (nnz > 0) {
jc = allocn<Int>(nnz, touch);
d = allocn<Sclr>(nnz, touch);
}
ir[0] = 0;
ir[m] = nnz;
} catch (...) {
free();
std::stringstream ss;
ss << fail_msg << ": SparseData failed to allocate. m = "
<< m << " nnz = " << nnz;
throw hts::Exception(ss.str());
}
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::SparseData::free () {
deln(ir);
deln(jc);
deln(d);
}
struct NumThreads {
int omp, mkl;
bool mkl_dynamic;
};
inline void set_num_threads (const int nthreads, NumThreads& save) {
save.omp = omp_get_max_threads();
#ifdef HTS_USE_MKL
save.mkl = mkl_get_max_threads();
save.mkl_dynamic = mkl_get_dynamic();
#endif
omp_set_num_threads(nthreads);
#ifdef HTS_USE_MKL
// We never use MKL threading.
mkl_set_dynamic(0);
mkl_set_num_threads(1);
#endif
}
inline void restore_num_threads (const NumThreads& save) {
#ifdef HTS_USE_MKL
mkl_set_dynamic(save.mkl_dynamic);
#endif
// This ruins performance on especially the KNC if the number of threads is
// actually switching. Need to think about this more. Right now, the interface
// does not promise OMP state will remain the same.
return;
omp_set_num_threads(save.omp);
#ifdef HTS_USE_MKL
mkl_set_num_threads(save.mkl);
#endif
}
inline bool check_nthreads (const int nt_requested, const int nt_rcvd,
std::string& msg) {
if (nt_requested != nt_rcvd) {
std::stringstream ss;
ss << "set_num_threads: " << nt_requested << " requested, but "
<< nt_rcvd << " obtained.";
msg = ss.str();
return false;
}
return true;
}
template<typename Int, typename Size, typename Sclr>
inline void Impl<Int, Size, Sclr>::
throw_if_nthreads_not_ok (const int nthreads) {
int nr;
# pragma omp parallel
nr = omp_get_num_threads();
std::string msg;
if ( ! check_nthreads(nthreads, nr, msg))
throw hts::Exception(msg);
}
// Return i such that ir[i] is the first value >= c. If no such i exists,
// return n.
template<typename Int>
inline Int find_first (const Int* jc, const Int n, const Int c) {
return c == 0 ? 0 : std::lower_bound(jc, jc+n, c) - jc;
}
// Return the number of nonzeros in row r that are in [c_first, c_last). The
// corresponding indices, relative to the start of the row, are i_first:i_last.
template<typename Int, typename Size, typename Sclr>
inline Int Impl<Int, Size, Sclr>::
find_first_and_last (const Size* const ir, const Int r, const Int* const jc,
const Int c_first, const Int c_last,
Int& i_first, Int& i_last) {
assert(c_last >= c_first);
const Size
iri = ir[r],
irip1 = ir[r+1];
const Int n = static_cast<Int>(irip1 - iri);
i_first = find_first(jc + iri, n, c_first);
if (i_first == n) {
i_last = i_first;
return 0;
}
i_last = i_first + find_first<Int>(jc + iri + i_first, n - i_first, c_last);
// A return value of n - i_first is OK.
return i_last - i_first;
}
// Crop the submatrix A(b) such that A(cb) has no 0 border.
template<typename Int, typename Size, typename Sclr>
Size Impl<Int, Size, Sclr>::
crop_matrix (const CrsMatrix& T, const Box& b, Box& cb) {
cb.r0 = -1;
Int r1 = -1;
cb.c0 = b.c0 + b.nc;
Int c1 = b.c0;
Size nnz = 0;
for (Int r = b.r0, lrow = 0; r < b.r0 + b.nr; ++r, ++lrow) {
Int i_first, i_last;
const Int cnt = find_first_and_last(T.ir, r, T.jc, b.c0, b.c0 + b.nc,
i_first, i_last);
if (cnt) {
nnz += cnt;
const Size irr = T.ir[r];
cb.c0 = std::min(cb.c0, T.jc[irr + i_first]);
c1 = std::max(c1, T.jc[irr + i_last - 1]);
r1 = r;
if (cb.r0 == -1) cb.r0 = r;
}
}
if (cb.r0 == -1) {
cb.r0 = b.r0;
cb.nr = 0;
} else cb.nr = r1 - cb.r0 + 1;
if (cb.c0 > c1) {
cb.c0 = b.c0;
cb.nc = 0;
} else cb.nc = c1 - cb.c0 + 1;
return nnz;
}
// Decide how many level sets to keep.
template<typename Int, typename Size, typename Sclr>
Int Impl<Int, Size, Sclr>::
decide_level_set_max_index (const Array<Int>& N, const Int size_thr,
const Options& o) {
Int N_end = static_cast<Int>(N.size());
while (N_end > 0 && N[N_end-1] < size_thr) --N_end;
Int nrows_total = 0, nrows_under = 0;
for (Int i = 0; i < N_end; ++i) {
const Int Ni = N[i];
nrows_total += Ni;
if (Ni < size_thr) nrows_under += Ni;
}
Int i;
for (i = N_end - 1; i >= 0; --i) {
const Int Ni = N[i];
if (Ni >= size_thr && nrows_under <= o.lset_max_bad_fraction * nrows_total)
break;
nrows_total -= Ni;
nrows_under -= Ni;
}
return i;
}
// Allocate lsets.
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::
alloc_lsets (
const Int lsmi, const Int sns, const Array<Int>& level, const Array<Int>& n,
typename LevelSetter::LevelSets& lsets)
{
if (lsmi < 0) return;
const Int Lm_sr = static_cast<Int>(level.size());
lsets.optclear_and_resize(lsmi+1);
for (Int i = 0; i <= lsmi; ++i) {
lsets[i].init();
lsets[i].optclear_and_reserve(sns * n[i]);
}
// Load.
for (Int i = 0; i < Lm_sr; ++i) {
const Int ilev = level[i];
if (ilev <= lsmi)
for (Int j = 0; j < sns; ++j)
lsets[ilev].unsafe_push_back(i * sns + j);
}
}
template<typename Int, typename Size, typename Sclr>
Int Impl<Int, Size, Sclr>::
locrsrow_schedule_serial (const ConstCrsMatrix& L, const Int sns,
Array<Int>& w) {
// Eq. 18 in Y. Saad's 1989 SIAM J Sci Stat Comput paper.
Int max_level = -1;
if (sns == 1) {
w.optclear_and_resize(L.m, -1);
for (Int r = 0; r < L.m; ++r) {
Int level = -1;
for (Size j = L.ir[r]; j < L.ir[r+1]; ++j)
level = std::max(level, w[L.jc[j]]);
++level;
w[r] = level;
max_level = std::max(max_level, level);
}
} else {
// Implement it for a blocked matrix, where the block size is sns >= 1.
const Int Lm_sr = L.m / sns;
w.optclear_and_resize(Lm_sr, -1);
for (Int sr = 0, r = 0; sr < Lm_sr; ++sr) {
Int level = -1;
for (Int i = 0; i < sns; ++i, ++r) {
// For each row in the block row:
for (Size j = L.ir[r]; j < L.ir[r+1]; ++j) {
const Int sc = L.jc[j] / sns;
level = std::max(level, w[sc]);
}
}
++level;
w[sr] = level;
max_level = std::max(max_level, level);
}
}
return max_level;
}
template<typename Int, typename Size, typename Sclr>
Int Impl<Int, Size, Sclr>::
locrsrow_schedule_sns1 (const ConstCrsMatrix& L, Array<Int>& w,
const Options& o) {
const Int
nthreads = omp_get_max_threads(),
blksz = nthreads*((o.pp_min_block_size + nthreads)/nthreads),
rows_per_thread = std::max(1, blksz / nthreads);
if (blksz > L.m)
return locrsrow_schedule_serial(L, 1, w);
Array<Size> frontier(blksz);
for (Int i = 0; i < blksz; ++i) frontier[i] = L.ir[i];
w.optclear_and_resize(L.m);
Int max_level = -1;
volatile Int done = -1;
# pragma omp parallel
{
# pragma omp for
for (Int i = 0; i < L.m; ++i) w[i] = -1;
const Size* const ir = L.ir;
const Int* const jc = L.jc;
for (Int c = 0; c < L.m; c += blksz) {
const Int tlim = std::min<Int>(c + blksz, L.m);
// On-diag serial triangle.
# pragma omp single nowait
{
for (Int r = c; r < tlim; ++r) {
// w[r] contains the max level seen so far.
Int level = w[r];
for (Size j = frontier[r - c], jlim = ir[r+1]; j < jlim; ++j)
level = std::max(level, w[jc[j]]);
++level;
w[r] = level;
max_level = std::max(max_level, level);
}
done = c;
}
if (tlim == L.m) break;
// Off-diag parallel block row.
const Int rlim = std::min<Int>(tlim + blksz, L.m);
while (done != c) ;
# pragma omp for schedule(static, rows_per_thread)
for (Int r = tlim; r < rlim; ++r) {
Int level = -1;
const Size jlim = ir[r+1];
for (Size j = ir[r]; j < jlim; ++j) {
const Int col = jc[j];
if (col >= tlim) {
frontier[r - tlim] = j;
w[r] = level;
break;
}
level = std::max(level, w[col]);
}
}
// Implied barrier from parfor.
}
}
return max_level;
}
template<typename Int, typename Size, typename Sclr>
Int Impl<Int, Size, Sclr>::
locrsrow_schedule (const ConstCrsMatrix& L, const Int sns, Array<Int>& w,
const Options& o) {
assert(L.m > 0);
if (sns == 1) return locrsrow_schedule_sns1(L, w, o);
const Int
Lm_sr = L.m / sns,
blksz = (o.pp_min_block_size + sns) / sns,
bnr = sns*blksz,
nthreads = omp_get_max_threads(),
rows_per_thread = std::max(1, (blksz + nthreads) / nthreads);
if (blksz > Lm_sr)
return locrsrow_schedule_serial(L, sns, w);
Array<Size> frontier(bnr);
for (Int i = 0; i < bnr; ++i) frontier[i] = L.ir[i];
w.optclear_and_resize(Lm_sr);
Int max_level = -1;
# pragma omp parallel
{
# pragma omp for
for (Int i = 0; i < Lm_sr; ++i) w[i] = -1;
const Size* const ir = L.ir;
const Int* const jc = L.jc;
for (Int sc = 0; sc < Lm_sr; sc += blksz) {
const Int stlim = std::min<Int>(sc + blksz, Lm_sr);
// On-diag serial triangle.
# pragma omp single nowait
{
const Int c = sns*sc;
for (Int sr = sc, r = c; sr < stlim; ++sr) {
Int level = w[sr];
for (Int i = 0; i < sns; ++i, ++r)
for (Size j = frontier[r - c], jlim = ir[r+1]; j < jlim; ++j) {
const Int jsc = jc[j] / sns;
assert(jsc >= sc);
level = std::max(level, w[jsc]);
}
++level;
w[sr] = level;
max_level = std::max(max_level, level);
}
}
if (stlim == Lm_sr) break;
// Off-diag parallel block row.
# pragma omp barrier
const Int
srlim = std::min<Int>(stlim + blksz, Lm_sr),
tlim = sns*stlim;
# pragma omp for schedule(static, rows_per_thread)
for (Int sr = stlim; sr < srlim; ++sr) {
Int level = -1;
for (Int i = 0, r = sns*sr; i < sns; ++i, ++r) {
const Size jlim = ir[r+1];
for (Size j = ir[r]; j < jlim; ++j) {
const Int c = jc[j];
if (c >= tlim) {
frontier[r - tlim] = j;
break;
}
const Int scol = c / sns;
level = std::max(level, w[scol]);
}
}
w[sr] = level;
}
// Implied barrier from parfor.
}
}
return max_level;
}
template<typename Int, typename Size, typename Sclr>
void Impl<Int, Size, Sclr>::
find_row_level_sets_Lcrs (const ConstCrsMatrix& L, const Int sns,
Int size_thr, typename LevelSetter::LevelSets& lsets,
const Options& o) {
assert(L.m % sns == 0);
Array<Int> w;
#ifdef __MIC__
// || is working pretty well on MIC, but not on CPU.
const Int max_level = locrsrow_schedule(L, sns, w, o);
#else
const Int max_level = locrsrow_schedule_serial(L, sns, w);
#endif
// Count level set sizes.
Array<Int> n(max_level+1, 0);
for (size_t i = 0; i < w.size(); ++i)
++n[w[i]];
// Cutoff.
const Int lsmi = decide_level_set_max_index(n, size_thr, o);
// Fill lsets.
alloc_lsets(lsmi, sns, w, n, lsets);
}
// Upper tri, CRS, col (not row) level sets. Equivalent to lower tri, CCS, row
// level sets.
template<typename Int, typename Size, typename Sclr>
Int Impl<Int, Size, Sclr>::
upcrscol_schedule_serial (const ConstCrsMatrix& U, const Int sns,
Array<Int>& w) {
Int max_level = -1;
if (sns == 1) {
w.optclear_and_resize(U.m, -1);
for (Int r = 0; r < U.m; ++r) {
++w[r];
const Int level = w[r];
max_level = std::max(level, max_level);
for (Size j = U.ir[r]; j < U.ir[r+1]; ++j) {
const Int c = U.jc[j];
w[c] = std::max(w[c], level);
}