-
Notifications
You must be signed in to change notification settings - Fork 2
/
tql2.hpp
214 lines (178 loc) · 4.97 KB
/
tql2.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
#pragma once
template <class T>
__device__ __noinline__ int
tql2_( const int nm, const int n,
T * __restrict__ d_, T * __restrict__ e_, T * __restrict__ z_,
// optional
int const max_sweep = 100,
T const tol = machine_epsilon<T>()*(std::is_same<T,double>::value?512:16),
bool const do_sort = (DO_SORT==1)
)
{
const int myid = threadIdx.x % WARP_GPU_SIZE + 1;
#define z(row,col) (*(z_+((row)-1)+((col)-1)*nm))
#define d(index) (*(d_+((index)-1)))
#define e(index) (*(e_+((index)-1)))
#define pos(index) (*(pos_+((index)-1)))
const int tile_size = WARP_GPU_SIZE;
T * shmem = __SHMEM__();
const T ZERO = static_cast<T>(0.0e0);
const T ONE = static_cast<T>(1.0e0);
int ierror = 0;
_if_ (n == 1) { return ierror; }
#pragma unroll 1
for(int i=1; i<=n; i++) {
#pragma unroll 1
for(int j=myid; j<=n; j+=WARP_GPU_SIZE) {
z(j, i) = ZERO;
}
}
#pragma unroll 1
for(int i=myid; i<=n; i+=WARP_GPU_SIZE) {
z(i, i) = ONE;
} sync_over_warp();
T shift = ZERO;
T tst1 = ZERO;
#pragma unroll 1
for(int l=1; l<=n; l++) { // most outer loop
tst1 = Max(tst1, (Abs(d(l)) + Abs(e(l)))*tol);
sync_over_warp();
int m = 1;
_if_ (myid == 1) {
#pragma unroll 1
for(m=l; m<=n; m++) {
const T tst2 = Abs(e(m));
_if_ (tst2 <= tst1) break;
}
} bcast_over_warp(m,1);
_if_ (m != l) { // non-isolated diagonal
int itr;
#pragma unroll 1
for(itr=0; itr<max_sweep; itr++) {
T dl1;
T delta_d;
sync_over_warp(); {
const T dl_old = d(l);
const T el = e(l);
const T p = Div(d(l+1) - dl_old, el + el);
const T r = pythag1(p);
const T psr = p + Sign(r, p);
const T dl = Div(el, psr);
dl1 = el * psr;
_if_ (myid==1) { d(l) = dl; d(l+1) = dl1; }
delta_d = dl_old - dl;
} sync_over_warp();
for(int i=l+1+myid; i<=n; i+=WARP_GPU_SIZE) {
d(i) -= delta_d;
} sync_over_warp();
shift += delta_d;
T c = ONE;
T c2 = c;
T c3 = c;
T s = ZERO;
T s2 = s;
T p = d(m);
const T el1 = e(l+1);
#pragma unroll 1
for(int i0=m-1; i0>=l; i0-=tile_size) {
int i1=max(l,i0-tile_size+1);
T * c_tmp = shmem;
T * s_tmp = c_tmp + tile_size;
T cc, ss, ee, dd;
#pragma unroll 1
for(int i=i0; i>=i1; i--) {
c3 = c2;
c2 = c;
s2 = s;
const T ei = e(i);
const T di = d(i);
const T g = c * ei;
const T h = c * p;
const T r = pythag(p, ei);
const T ei1 = s * r;
s = Div(ei, r);
c = Div(p, r);
p = c * di - s * g;
const T di1 = h + s * (c * g + s * di);
#if 0
sync_over_warp();
_if_ (i0-i==myid-1) {
e(i + 1) = ei1; d(i + 1) = di1;
c_tmp[myid-1] = c; s_tmp[myid-1] = s;
} sync_over_warp();
#else
_if_ (i0-i==myid-1) {
ee = ei1; dd = di1; cc = c; ss = s;
}
#endif
} sync_over_warp();
#if 0
#else
_if_ (i0-i1>=myid-1) {
int i = i0 - (myid - 1);
e(i + 1) = ee; d(i + 1) = dd;
c_tmp[myid-1] = cc; s_tmp[myid-1] = ss;
} sync_over_warp();
#endif
for(int i=i0; i>=i1; i--) {
c = c_tmp[i0-i]; s = s_tmp[i0-i];
T * zki0_ptr = &z(myid,i+0);
T * zki1_ptr = &z(myid,i+1);
for(int k=myid; k<=n; k+=WARP_GPU_SIZE ) {
const T h0 = *zki0_ptr;
const T h1 = *zki1_ptr;
*zki1_ptr = s * h0 + c * h1;
*zki0_ptr = c * h0 - s * h1;
zki0_ptr+=WARP_GPU_SIZE; zki1_ptr+=WARP_GPU_SIZE;
}
} sync_over_warp();
}
_if_ (myid==1) {
const T r = Div(-s * s2 * c3 * el1 * e(l), dl1);
e(l) = s * r;
d(l) = c * r;
} sync_over_warp();
{
const T tst2 = Abs(e(l));
_if_(tst2 <= tst1) break;
}
} sync_over_warp();
_if_ (itr>=max_sweep) { ierror = l; break; }
}
_if_ (myid==1) {
d(l) += shift;
} sync_over_warp();
}
_if_ (do_sort) {
_if_ (ierror == 0) {
int * const pos_ = (int *)e_;
for(int i=myid; i<=n; i+=WARP_GPU_SIZE) {
pos(i) = i;
} sync_over_warp();
#pragma unroll 1
for(int i=2; i<=n; i++) {
const int l = i - 1;
_if_ (myid==1) {
T dl = d(l);
int il = l;
#pragma unroll 1
for (int j=i; j<=n; j++) {
const T dj = d(j);
const bool flag = dl > dj;
__UPDATE__(dl, dj, flag);
__UPDATE__(il, j, flag);
}
_if_ (il!=l) {
int p=pos(l); pos(l)=pos(il); pos(il)=p;
d(il)=d(l); d(l)=dl;
}
}
} sync_over_warp();
}
}
#undef z
#undef d
#undef e
sync_over_warp();
return ierror;
}