-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathtask-allocator.c
571 lines (456 loc) · 15.3 KB
/
task-allocator.c
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
// https://github.com/ggerganov/ggml/issues/291
// https://github.com/ggerganov/llama.cpp/pull/1507
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <malloc.h> // using malloc.h with MSC/MINGW
#elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
#include <alloca.h>
#endif
#define GGML_ASSERT(x) \
do { \
if (!(x)) { \
fprintf(stderr, "GGML_ASSERT: %s:%d: %s\n", __FILE__, __LINE__, \
#x); \
abort(); \
} \
} while (0)
#define GGML_DEBUG 1
#if (GGML_DEBUG >= 1)
#define GGML_PRINT_DEBUG(...) printf(__VA_ARGS__)
#else
#define GGML_PRINT_DEBUG(...)
#endif
#if (GGML_DEBUG >= 5)
#define GGML_PRINT_DEBUG_5(...) printf(__VA_ARGS__)
#else
#define GGML_PRINT_DEBUG_5(...)
#endif
#define UNUSED(x) (void)(x)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#if defined(_WIN32)
#include <windows.h>
typedef volatile LONG atomic_int;
typedef atomic_int atomic_bool;
typedef HANDLE pthread_t;
typedef int thread_ret_t;
static void atomic_store(atomic_int *ptr, LONG val) {
InterlockedExchange(ptr, val);
}
static LONG atomic_load(atomic_int *ptr) {
return InterlockedCompareExchange(ptr, 0, 0);
}
static LONG atomic_fetch_add(atomic_int *ptr, LONG inc) {
return InterlockedExchangeAdd(ptr, inc);
}
static LONG atomic_fetch_sub(atomic_int *ptr, LONG dec) {
return atomic_fetch_add(ptr, -(dec));
}
static int pthread_create(pthread_t *out, void *unused,
thread_ret_t (*func)(void *), void *arg) {
(void)unused;
HANDLE handle =
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL);
if (handle == NULL) {
return EAGAIN;
}
*out = handle;
return 0;
}
static int pthread_join(pthread_t thread, void *unused) {
(void)unused;
return (int)WaitForSingleObject(thread, INFINITE);
}
static int sched_yield(void) {
// https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-yieldprocessor
YieldProcessor();
return 0;
}
#else // ! _WIN32
typedef void *thread_ret_t;
#include <pthread.h>
#include <stdatomic.h>
#endif
typedef pthread_t ggml_thread_t;
//-----------------------------------------------------------------------------
/// Most of the above codes are taken from
/// https://github.com/ggerganov/llama.cpp/tree/master/ggml.c
/// Copyright original authors.
//-----------------------------------------------------------------------------
#define MAX_THREADS 16
struct task_allocator {
int nth;
int n_multiplier; // >= 1
atomic_int lock; // 0 unlocked, 1 locked
// total assigned.
atomic_int global_counter;
atomic_int thread_queue_heads[MAX_THREADS];
atomic_int thread_queue_tails[MAX_THREADS];
};
static void task_allocator_reset(struct task_allocator *a) {
for (int i = 0; i < a->nth; ++i) {
atomic_store(&a->thread_queue_heads[i], 0);
atomic_store(&a->thread_queue_tails[i], a->n_multiplier);
}
atomic_store(&a->lock, 0);
atomic_store(&a->global_counter, 0);
}
// NOTE: when nth == 1, n_multiplier is actually useless.
static void task_allocator_init(struct task_allocator *a, int nth,
int n_multiplier) {
GGML_ASSERT(nth > 0);
GGML_ASSERT(nth <= MAX_THREADS);
GGML_ASSERT(n_multiplier > 0);
a->nth = nth;
a->n_multiplier = nth == 1 ? 1 : n_multiplier;
task_allocator_reset(a);
}
// ith: worker id (start from 0).
// chunk_idx and n_chunks will be updated.
// chunk_idx is set as -1 when nothing to do.
static void allocate_chunk(struct task_allocator *a, int ith, int *chunk_idx,
int *n_chunks) {
GGML_ASSERT(ith >= 0 && ith < a->nth);
int M = a->n_multiplier;
int nth = a->nth;
int total_chunks = M * nth;
*chunk_idx = -1;
*n_chunks = total_chunks;
while (atomic_fetch_add(&a->lock, 1) != 0) { // lock
atomic_fetch_sub(&a->lock, 1);
}
// all assigned?
if (atomic_load(&a->global_counter) == total_chunks) {
GGML_PRINT_DEBUG("[#_%d] %s(): nothing to do.\n", ith, __func__);
atomic_fetch_sub(&a->lock, 1); // unlock
return;
}
// try take its own, pop front.
{
int head = atomic_load(&a->thread_queue_heads[ith]);
int tail = atomic_load(&a->thread_queue_tails[ith]);
GGML_PRINT_DEBUG_5("[#_%d] %s(): head: %d, tail: %d.\n", ith, __func__,
head, tail);
if (head < tail) {
int idx = ith * M + head;
atomic_fetch_add(&a->thread_queue_heads[ith], 1);
atomic_fetch_add(&a->global_counter, 1);
GGML_PRINT_DEBUG("[#_%d] %s(): take the %3d-th chunk of its own.\n",
ith, __func__, head + 1);
*chunk_idx = idx;
*n_chunks = total_chunks;
atomic_fetch_sub(&a->lock, 1); // unlock
return;
}
}
// steal from others.
// TODO: optimize: steal from the slowest one.
for (int i = 0; i < nth; ++i) {
if (i == ith) {
continue;
}
int tail = atomic_load(&a->thread_queue_tails[i]);
if (tail == atomic_load(&a->thread_queue_heads[i])) {
continue;
}
// pop back
int idx = i * M + tail;
atomic_fetch_sub(&a->thread_queue_tails[i], 1);
atomic_fetch_add(&a->global_counter, 1);
GGML_PRINT_DEBUG("[#_%d] %s(): steal the %d-th chunk from #_%d\n", ith,
__func__, tail, i);
*chunk_idx = idx;
*n_chunks = total_chunks;
atomic_fetch_sub(&a->lock, 1); // unlock
return;
}
fprintf(stderr, "%s:%d should be unreachable!\n", __FILE__, __LINE__);
abort();
}
struct state_shared {
int n_threads;
int n_multiplier;
int n_nodes;
struct ggml_tensor *nodes;
// thread done counter for single node
atomic_int done_counter;
struct task_allocator task_allocator;
};
struct state {
ggml_thread_t thrd;
int ith;
struct state_shared *shared;
};
// simulate tensor that can be compute in parallel
struct ggml_tensor {
// simulate actual compute workload, e.g. src0 rows
int n_compute_units;
};
struct params {
int ith;
int nth;
// simulate performance jitters related to: OS workload, thread affinity,
// economic cores, ...
int jitter_percent;
struct task_allocator *task_allocator;
};
void compute_tensor(struct params params, struct ggml_tensor *node) {
GGML_PRINT_DEBUG_5("[#_%d] %s(): enter.\n", params.ith, __func__);
const int ith = params.ith;
int chunk_idx;
int n_chunks;
while (true) {
allocate_chunk(params.task_allocator, ith, &chunk_idx, &n_chunks);
if (chunk_idx < 0) {
break;
}
const int nr = node->n_compute_units;
const int dr = (nr + n_chunks - 1) / n_chunks;
const int ir0 = dr * chunk_idx;
const int ir1 = MIN(ir0 + dr, nr);
const int n_loops = 10000 * (100 + params.jitter_percent);
volatile int64_t x = 0;
for (int i = ir0; i <= ir1; ++i) {
for (int j = 0; j < n_loops; ++j) {
++x;
}
}
UNUSED(x);
}
GGML_PRINT_DEBUG_5("[#_%d] %s(): exit.\n", ith, __func__);
}
static thread_ret_t demo_compute_thread(void *data) {
struct state *state = (struct state *)data;
GGML_ASSERT(state);
struct state_shared *shared = state->shared;
GGML_ASSERT(shared);
struct task_allocator *allocator = &shared->task_allocator;
GGML_ASSERT(allocator);
int ith = state->ith;
int n_threads = shared->n_threads;
atomic_int *done_counter = &shared->done_counter;
for (int i = 0; i < shared->n_nodes; ++i) {
// Just slow down the last thread.
struct params params = {
.ith = state->ith,
.nth = n_threads, // suppose parallel
.task_allocator = allocator,
.jitter_percent = ith + 1 < n_threads ? 0 : 50,
};
struct ggml_tensor *node = &shared->nodes[i];
compute_tensor(params, node);
atomic_fetch_add(done_counter, 1);
GGML_PRINT_DEBUG_5("[#_%d] %s(): finished computing the node.\n", ith,
__func__);
if (ith == 0) {
while (atomic_load(done_counter) != n_threads) {
sched_yield();
}
GGML_PRINT_DEBUG_5(
"[#_%d] %s(): saw all threads finished computing the node.\n",
ith, __func__);
task_allocator_reset(allocator);
atomic_store(done_counter, 0);
} else {
while (atomic_load(done_counter) != 0) {
sched_yield();
}
}
}
GGML_PRINT_DEBUG_5("[#_%d] %s(): exited\n", ith, __func__);
return 0;
}
static void test_task_allocator_init(void) {
struct task_allocator a;
task_allocator_init(&a, 1, 2);
GGML_ASSERT(a.nth == 1);
GGML_ASSERT(a.n_multiplier == 1); // when nth == 1, force n_multiplier as 1
task_allocator_init(&a, 2, 2);
GGML_ASSERT(a.nth == 2);
GGML_ASSERT(a.n_multiplier == 2); // ok
}
static void task_allocator_unit_test_no_steal(void) {
int chunk_idx; // out
int n_chunks; // out
int n_threads = 2;
int n_multiplier = 2;
const int expected_n_slots = n_threads * n_multiplier;
struct task_allocator a;
task_allocator_init(&a, n_threads, n_multiplier);
struct test_data_t {
int ith; // call by
int chunk_idx; // expected
int n_chunks; // expected
};
struct test_data_t test_data[] = {
//////////////////// clang format /////////////////////////
{
.ith = 0,
.chunk_idx = 0,
},
{
.ith = 1,
.chunk_idx = 2,
},
{
.ith = 0,
.chunk_idx = 1,
},
{
.ith = 1,
.chunk_idx = 3,
},
{
.ith = 0,
.chunk_idx = -1,
},
{
.ith = 1,
.chunk_idx = -1,
}};
int t_len = sizeof(test_data) / sizeof(struct test_data_t);
for (int i = 0; i < t_len; i++) {
allocate_chunk(&a, test_data[i].ith, &chunk_idx, &n_chunks);
if (chunk_idx != test_data[i].chunk_idx) {
fprintf(stderr,
"%s(): chunk_idx mismatch. i: %d, actual: %d, expected: %d\n",
__func__, i, chunk_idx, test_data[i].chunk_idx);
abort();
}
if (n_chunks != expected_n_slots) {
fprintf(stderr,
"%s(): n_chunks mismatch. i: %d, actual: %d, expected: %d\n",
__func__, i, n_chunks, expected_n_slots);
abort();
}
}
}
static void task_allocator_unit_test_steal(void) {
int chunk_idx; // out
int n_chunks; // out
int n_threads = 2;
int n_multiplier = 2;
const int expected_n_slots = n_threads * n_multiplier;
struct task_allocator a;
task_allocator_init(&a, n_threads, n_multiplier);
struct test_data_t {
int ith; // call by
int chunk_idx; // expected
};
struct test_data_t test_data[] = {
//////////////////// clang format /////////////////////////
{
.ith = 0,
.chunk_idx = 0,
},
{
.ith = 0,
.chunk_idx = 1,
},
{
.ith = 1,
.chunk_idx = 2,
},
{
.ith = 0,
.chunk_idx = 4, // steal from tail
},
{
.ith = 0,
.chunk_idx = -1,
},
{
.ith = 1,
.chunk_idx = -1,
}};
int t_len = sizeof(test_data) / sizeof(struct test_data_t);
for (int i = 0; i < t_len; i++) {
allocate_chunk(&a, test_data[i].ith, &chunk_idx, &n_chunks);
if (chunk_idx != test_data[i].chunk_idx) {
fprintf(stderr,
"%s(): chunk_idx mismatch. i: %d, actual: %d, expected: %d\n",
__func__, i, chunk_idx, test_data[i].chunk_idx);
abort();
}
if (n_chunks != expected_n_slots) {
fprintf(stderr,
"%s(): n_chunks mismatch. i: %d, actual: %d, expected: %d\n",
__func__, i, n_chunks, expected_n_slots);
abort();
}
}
}
// Integration test.
static void test_task_allocator(int n_threads, int n_nodes, int n_compute_units,
int n_multiplier) {
fprintf(stderr,
"\n%s(): n_threads: %d, n_nodes: %d, n_compute_units: %d, "
"n_multiplier: %d ===>\n\n",
__func__, n_threads, n_nodes, n_compute_units, n_multiplier);
struct ggml_tensor *nodes = alloca(n_nodes * sizeof(struct ggml_tensor));
for (int i = 0; i < n_nodes; ++i) {
nodes[i].n_compute_units = n_compute_units;
}
struct state_shared shared = {
.n_threads = n_threads,
.n_nodes = n_nodes,
.nodes = nodes,
.done_counter = 0,
};
task_allocator_init(&shared.task_allocator, n_threads, n_multiplier);
struct state *workers = alloca(n_threads * sizeof(struct state));
for (int i = 0; i < n_threads; ++i) {
workers[i].ith = i;
workers[i].shared = &shared;
if (i > 0) {
pthread_create(&workers[i].thrd, NULL, demo_compute_thread,
&workers[i]);
}
}
demo_compute_thread(&workers[0]);
for (int i = 1; i < n_threads; ++i) {
pthread_join(workers[i].thrd, NULL);
}
}
//
// Conclusions:
//
// - Given workers A and B, and the accumulated time T_a and T_b:
// B can steal a chunk from A only if T_a > T_b + T_b_per_chunk.
// - Saw this situation: A steal B, B steal C.
// - n_chunks plays a key role, similar to choosing the best n_threads, it's
// difficult to choose the ideal n_chunks value. Performance drops when
// per-chunk compute time exceeds the scheduling overhead.
// - Work stealing chunked task allocator can save the response time
// significantly when the majority threads runs fast but a few suddenly or
// constantly slow.
//
int main(void) {
test_task_allocator_init();
task_allocator_unit_test_no_steal();
task_allocator_unit_test_steal();
// Integration tests
const int n_compute_units = 64;
if (false) {
int n_threads = 1;
int n_nodes = 1;
int n_multiplier = 2; // equivalent to 1
test_task_allocator(n_threads, n_nodes, n_compute_units, n_multiplier);
}
if (true) {
int n_threads = 2;
int n_nodes = 2;
int n_multiplier = 1;
test_task_allocator(n_threads, n_nodes, n_compute_units, n_multiplier);
}
if (true) {
int n_threads = 2;
int n_nodes = 2;
int n_multiplier = 8;
test_task_allocator(n_threads, n_nodes, n_compute_units, n_multiplier);
}
}