-
Notifications
You must be signed in to change notification settings - Fork 11
/
jacobi_solver.metal
317 lines (180 loc) · 10.2 KB
/
jacobi_solver.metal
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
#include <metal_stdlib>
using namespace metal;
struct jacobi_solver_constants {
int dim;
};
void atomic_add_float( device atomic_uint* atom_var, const float val )
{
uint fetched_uint, assigning_uint;
float fetched_float, assigning_float;
fetched_uint = atomic_exchange_explicit( atom_var, 0, memory_order_relaxed );
fetched_float = *( (thread float*) &fetched_uint );
assigning_float = fetched_float + val;
assigning_uint = *( (thread uint*) &assigning_float );
while ( (fetched_uint = atomic_exchange_explicit( atom_var, assigning_uint, memory_order_relaxed ) ) != 0 ) {
uint fetched_uint_again = atomic_exchange_explicit( atom_var, 0, memory_order_relaxed );
float fetched_float_again = *( (thread float*) &fetched_uint_again );
fetched_float = *( (thread float*) &(fetched_uint) );
assigning_float = fetched_float_again + fetched_float;
assigning_uint = *( (thread uint*) &assigning_float );
}
}
kernel void solve_col_major (
device const float* A [[ buffer(0) ]],
device const float* Dinv [[ buffer(1) ]],
device const float* b [[ buffer(2) ]],
device const float* xin [[ buffer(3) ]],
device float* xout [[ buffer(4) ]],
device atomic_uint* x_error [[ buffer(5) ]],
device const jacobi_solver_constants& constants [[ buffer(6) ]],
const uint thread_position_in_threadgroup [[ thread_position_in_threadgroup ]],
const uint threadgroup_position_in_grid [[ threadgroup_position_in_grid ]],
const uint thread_position_in_grid [[ thread_position_in_grid ]]
) {
if ( (int)thread_position_in_grid < constants.dim ) {
const int row = thread_position_in_grid;
float sum = 0.0;
for ( int col = 0; col < constants.dim; col++ ) {
sum += ( A[ row + constants.dim * col] * xin[col] );
}
xout[row] = (b[row] - sum)*Dinv[row];
atomic_add_float( x_error, (xout[row] - xin[row])*(xout[row] - xin[row]) );
}
}
kernel void solve_row_major (
device const float* A [[ buffer(0) ]],
device const float* Dinv [[ buffer(1) ]],
device const float* b [[ buffer(2) ]],
device const float* xin [[ buffer(3) ]],
device float* xout [[ buffer(4) ]],
device atomic_uint* x_error [[ buffer(5) ]],
device const jacobi_solver_constants& constants [[ buffer(6) ]],
const uint thread_position_in_threadgroup [[ thread_position_in_threadgroup ]],
const uint threadgroup_position_in_grid [[ threadgroup_position_in_grid ]],
const uint thread_position_in_grid [[ thread_position_in_grid ]],
const uint threads_per_threadgroup [[ threads_per_threadgroup ]],
const uint thread_index_in_simdgroup [[ thread_index_in_simdgroup ]],
const uint simdgroup_index_in_threadgroup [[ simdgroup_index_in_threadgroup ]],
const uint simdgroups_per_threadgroup [[ simdgroups_per_threadgroup ]]
) {
const int THREADS_PER_THREADGROUP = 1024; // macos
threadgroup float sum_cache[ THREADS_PER_THREADGROUP ];
const int row = threadgroup_position_in_grid;
float sum = 0.0;
for ( int col = thread_position_in_threadgroup ; col < constants.dim ; col += threads_per_threadgroup ) {
sum += ( A[ row * constants.dim + col ] * xin[ col ] );
}
const float warp_sum = simd_sum (sum);
if ( thread_index_in_simdgroup == 0 ){
sum_cache[ simdgroup_index_in_threadgroup ] = warp_sum;
}
threadgroup_barrier( mem_flags::mem_threadgroup );
if ( simdgroup_index_in_threadgroup == 0 ) {
const float local_sum = (thread_index_in_simdgroup< simdgroups_per_threadgroup)? sum_cache[ thread_index_in_simdgroup ] : 0.0;
const float warp_sum = simd_sum( local_sum );
if ( thread_position_in_threadgroup == 0 ) {
xout[row] = (b[row] - warp_sum)*Dinv[row];
atomic_add_float( x_error, (xout[row] - xin[row])*(xout[row] - xin[row]) );
}
}
}
// - input vector cached
// - col major for coalesced load
// - one thread per row
// NOTE: Use of threadgroup memory in metal does not make it faster if the num rows exceeds 1K.
kernel void solve_col_major_old (
device const float* A [[ buffer(0) ]],
device const float* Dinv [[ buffer(1) ]],
device const float* b [[ buffer(2) ]],
device const float* xin [[ buffer(3) ]],
device float* xout [[ buffer(4) ]],
device float& x_error [[ buffer(5) ]],
device const jacobi_solver_constants& constants [[ buffer(6) ]],
const uint thread_position_in_threadgroup [[ thread_position_in_threadgroup ]],
const uint threadgroup_position_in_grid [[ threadgroup_position_in_grid ]],
const uint thread_position_in_grid [[ thread_position_in_grid ]]
) {
const int THREADS_PER_THREADGROUP = 1024; // macos
const int THREADGROUP_MEM_MAX_IN_FLOAT = 8* 1024; // macos
// 1st step: xout = A*xi
threadgroup float xin_cache[ THREADGROUP_MEM_MAX_IN_FLOAT ];
for ( int col_base = 0; col_base < constants.dim; col_base += THREADGROUP_MEM_MAX_IN_FLOAT ) {
for ( int row_base = 0; row_base < constants.dim; row_base += THREADS_PER_THREADGROUP ){
for ( int i = 0; i < THREADGROUP_MEM_MAX_IN_FLOAT / THREADS_PER_THREADGROUP; i++ ) {
const int col_base_xin_cache = i * THREADS_PER_THREADGROUP + thread_position_in_threadgroup;
if ( col_base + col_base_xin_cache < constants.dim ) {
xin_cache[ col_base_xin_cache ] = xin[ col_base + col_base_xin_cache ];
}
}
threadgroup_barrier( mem_flags::mem_threadgroup );
const int row = row_base + thread_position_in_threadgroup;
if ( row < constants.dim ) {
const int col_end = ( constants.dim - col_base < THREADGROUP_MEM_MAX_IN_FLOAT )
? ( constants.dim - col_base )
: THREADGROUP_MEM_MAX_IN_FLOAT ;
float sum = 0.0;
for ( int k = 0; k < col_end; k++ ) {
sum += ( A[ row + constants.dim * ( col_base + k) ] * xin_cache[k] );
}
xout[row] += sum;
}
threadgroup_barrier( mem_flags::mem_device );
}
}
// 2nd step: xout = (b - xout) * Dinv
for ( int i_base = 0; i_base < constants.dim; i_base += THREADS_PER_THREADGROUP ) {
const int i = i_base + thread_position_in_threadgroup;
if ( i < constants.dim ) {
xout[i] = (b[i] - xout[i])*Dinv[i];
}
}
}
kernel void solve_row_major_old (
device const float* A [[ buffer(0) ]],
device const float* Dinv [[ buffer(1) ]],
device const float* b [[ buffer(2) ]],
device const float* xin [[ buffer(3) ]],
device float* xout [[ buffer(4) ]],
device float& x_error [[ buffer(5) ]],
device const jacobi_solver_constants& constants [[ buffer(6) ]],
const uint thread_position_in_threadgroup [[ thread_position_in_threadgroup ]],
const uint threadgroup_position_in_grid [[ threadgroup_position_in_grid ]],
const uint thread_position_in_grid [[ thread_position_in_grid ]],
const uint threads_per_threadgroup [[ threads_per_threadgroup ]]
) {
const int THREADS_PER_THREADGROUP = 1024; // macos
// 1st step: xout = A*xi
threadgroup float sum_cache[ THREADS_PER_THREADGROUP ];
for ( int row = 0; row < constants.dim; row++ ) {
float sum = 0.0;
for ( int col = thread_position_in_threadgroup ; col < constants.dim ; col += threads_per_threadgroup ) {
sum += ( A[ row * constants.dim + col ] * xin[ col ] );
}
sum_cache[ thread_position_in_threadgroup ] = sum;
threadgroup_barrier( mem_flags::mem_threadgroup );
// reduce
for ( int offset = threads_per_threadgroup / 2 ; offset >= 1; offset >>= 1 ) {
if ( offset > 16 ) {
if ( thread_position_in_threadgroup + offset < threads_per_threadgroup ) {
sum = sum_cache[thread_position_in_threadgroup] + sum_cache[thread_position_in_threadgroup + offset];
sum_cache[thread_position_in_threadgroup] = sum;
}
threadgroup_barrier( mem_flags::mem_threadgroup );
}
else {
sum += simd_shuffle_down( sum, offset );
}
}
if ( thread_position_in_threadgroup == 0 ) {
xout[row] = sum;
}
}
threadgroup_barrier( mem_flags::mem_device );
// 2nd step: xout = (b - xout) * Dinv
for ( int i_base = 0; i_base < constants.dim; i_base += THREADS_PER_THREADGROUP ) {
const int i = i_base + thread_position_in_threadgroup;
if ( i < constants.dim ) {
xout[i] = (b[i] - xout[i])*Dinv[i];
}
}
}