-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhpl_rand.hpp
162 lines (140 loc) · 3.54 KB
/
hpl_rand.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
#ifndef HPL_RAND_HPP
#define HPL_RAND_HPP
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
struct RandCoeff {
uint64_t a;
uint64_t c;
static RandCoeff default_vals(){
return {6364136223846793005, 1};
}
RandCoeff operator*(const RandCoeff &rhs) const {
return {a * rhs.a, a * rhs.c + c};
}
RandCoeff pow(const uint64_t n) const {
if (0 == n){
return RandCoeff{1, 0};
} else {
RandCoeff tmp = pow(n/2);
tmp = tmp * tmp;
if(n%2){
return tmp * (*this);
}else{
return tmp;
}
}
}
};
struct RandStat{
uint64_t x;
static RandStat initialize(
uint64_t seed,
RandCoeff coef = RandCoeff::default_vals())
{
return coef * RandStat{seed};
}
friend RandStat operator*(RandCoeff coef, RandStat stat){
return { coef.a * stat.x + coef.c };
}
// returns [-0.5:0.5]
operator double() const {
return static_cast<int64_t>(x) * 0x1.fffffffffffffP-65;
}
operator float() const {
float tmp = static_cast<double>(*this);
return tmp;
}
};
// fill subumat (i0:nrow-1, j0:ncol-1) of fullmat (0:nrow-1, 0:ncol-1)
template<typename F>
static void fill_one_panel_with_rand(
int const n,
int const i0,
int const j0,
int const nrow,
int const ncol,
F *a,
size_t const lda,
uint64_t const seed,
bool const calc_diag = true)
{
RandStat stat_00 = RandStat::initialize(seed);
RandCoeff inc1 = RandCoeff::default_vals();
RandCoeff jump_one_col = inc1.pow(n);
RandCoeff jump_ij = inc1.pow(i0 + n * static_cast<uint64_t>(j0));
RandStat stat_ij = jump_ij * stat_00;
RandStat at_0j = stat_ij;
for(int j=0; j<ncol; j++){
RandStat at_ij = at_0j;
for(int i=0; i<nrow; i++){
double t = static_cast<double>(at_ij);
a[j*lda + i] = static_cast<F>(t);
at_ij = inc1 * at_ij;
}
at_0j = jump_one_col * at_0j;
}
if(calc_diag && (i0 == j0) && (nrow==ncol)){
RandCoeff jump_i0 = inc1.pow(i0);
RandStat stat_i0 = jump_i0 * stat_00;
for(int i=0; i<(nrow<ncol?nrow:ncol); i++){
RandStat stat_ij = stat_i0;
double sum = 0.0;
for(int j=0; j<n; j++){
if(i0+i!=j)
sum += fabs(double(stat_ij));
stat_ij = jump_one_col * stat_ij;
}
a[lda*i + i] = static_cast<F>(sum);
stat_i0 = inc1 * stat_i0;
}
}
}
static inline
double calc_diag(int const i, int const n, uint64_t const seed){
RandStat stat_00 = RandStat::initialize(seed);
RandCoeff inc1 = RandCoeff::default_vals();
RandCoeff jump_one_col = inc1.pow(n);
RandCoeff jump_i0 = inc1.pow(i);
RandStat stat_ij = jump_i0 * stat_00;
double sum = 0.0;
for(int j=0; j<n; j++){
if(i!=j)
sum += fabs(double(stat_ij));
stat_ij = jump_one_col * stat_ij;
}
return sum;
}
// debug
static inline
double mat_elem(int n, int i, int j, int seed){
RandStat stat_00 = RandStat::initialize(seed);
RandCoeff inc1 = RandCoeff::default_vals();
RandCoeff jump_ij = inc1.pow(i + static_cast<uint64_t>(n) * j);
return double(jump_ij * stat_00);
}
template<typename F>
struct Matgen{
uint64_t seed;
int n;
F const* diag;
RandCoeff incl1, jumpn, jumpi, jumpj;
enum {NUM_POWERS = 16};
RandCoeff powers[NUM_POWERS];
double scalea, scaleb;
Matgen(uint64_t seed, int n, int iskip, int jskip, F const* diag): seed(seed), n(n), diag(diag) {
incl1 = RandCoeff::default_vals();
jumpn = incl1.pow(n);
jumpi = incl1.pow(iskip);
jumpj = incl1.pow(n * static_cast<uint64_t>(jskip));
for(int i=0; i<NUM_POWERS; i++){
powers[i] = incl1.pow(i);
}
scalea = sqrt(n*sqrt(n));
scaleb = 1;
}
RandCoeff jump(int i, int j) const {
return incl1.pow(i + n * static_cast<uint64_t>(j));
}
};
#endif