-
Notifications
You must be signed in to change notification settings - Fork 2
/
imtql2.hpp
180 lines (153 loc) · 4.46 KB
/
imtql2.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
#pragma once
template <class T>
__device__ __noinline__ int
//__device__ __forceinline__ int
imtql2_( const int nm, const int n,
T * __restrict__ d_, T * __restrict__ e_, T * __restrict__ z_,
// optional arguments
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();
#pragma unroll 1
for(int l=1; l<=n; l++) { // most outer loop
int itr;
#pragma unroll 1
for (itr=0; itr<=max_sweep; itr++) {
int m;
{
T g = Abs(d(l));
#pragma unroll 1
for(m=l; m<=n-1; m++) {
const T f = Abs(d(m+1));
const T tst1 = (g + f)*tol;
const T tst2 = Abs(e(m));
_if_ (tst2 <= tst1) break;
g = f;
}
}
_if_ (m == l) break; // convergence
T di1, r;
{
const T dl = d(l);
const T dl1 = d(l+1);
const T el = e(l);
const T f = Div(dl1 - dl, el+el);
const T g = pythag1(f);
di1 = d(m);
r = (di1 - dl) + Div(el, f + Sign(g, f));
}
int i;
T delta_d = ZERO;
{
T s = ONE;
T c = ONE;
#pragma unroll 1
for(int i0=m-1; i0>=l; i0-=WARP_GPU_SIZE) {
int i1=max(l,i0-WARP_GPU_SIZE+1);
T * c_tmp = shmem;
T * s_tmp = c_tmp + WARP_GPU_SIZE;
bool flag = false;
#pragma unroll 1
for(i=i0; i>=i1; i--) {
const T ei = e(i);
const T f = s * ei;
const T g = r;
r = pythag(f, r);
_if_ (r == ZERO) { flag = true; break; }
const T b = c * ei;
s = Div(f, r);
c = Div(g, r);
const T dix = di1 - delta_d;
di1 = d(i);
const T q = (di1 - dix) * s + 2 * c * b;
const T dx1 = fma(s, q, dix);
delta_d = dx1 - dix;
sync_over_warp();
_if_ (myid==1) {
e(i+1) = r; d(i+1) = dx1;
c_tmp[i0-i] = c; s_tmp[i0-i] = s;
} sync_over_warp();
r = c * q - b;
} i1 = max(i1, i+1);
#pragma unroll 4
for(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 f0 = *zki0_ptr;
const T f1 = *zki1_ptr;
*zki1_ptr = s * f0 + c * f1;
*zki0_ptr = c * f0 - s * f1;
zki0_ptr+=WARP_GPU_SIZE; zki1_ptr+=WARP_GPU_SIZE;
}
} sync_over_warp();
if (flag) { i = i1-1; break; }
}
}
_if_ (myid==1) {
const int j = max(i+1,l);
d(j) -= delta_d;
e(j) = r;
e(m) = ZERO;
}
}
_if_ (itr>max_sweep) { ierror = l; break; }
} 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;
}