Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 29.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "platform.h"
16
17#if NCNN_SIMPLEOMP
18
19#include "simpleomp.h"
20#include "cpu.h" // ncnn::get_cpu_count()
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <stdint.h>
26#include <stdarg.h>
27
28#if __clang__
29extern "C" typedef void (*kmpc_micro)(int32_t* gtid, int32_t* tid, ...);
30extern "C" typedef void (*kmpc_micro_0)(int32_t* gtid, int32_t* tid);
31extern "C" typedef void (*kmpc_micro_1)(int32_t* gtid, int32_t* tid, void*);
32extern "C" typedef void (*kmpc_micro_2)(int32_t* gtid, int32_t* tid, void*, void*);
33extern "C" typedef void (*kmpc_micro_3)(int32_t* gtid, int32_t* tid, void*, void*, void*);
34extern "C" typedef void (*kmpc_micro_4)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*);
35extern "C" typedef void (*kmpc_micro_5)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*);
36extern "C" typedef void (*kmpc_micro_6)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*);
37extern "C" typedef void (*kmpc_micro_7)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*);
38extern "C" typedef void (*kmpc_micro_8)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*);
39extern "C" typedef void (*kmpc_micro_9)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*);
40extern "C" typedef void (*kmpc_micro_10)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
41extern "C" typedef void (*kmpc_micro_11)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
42extern "C" typedef void (*kmpc_micro_12)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
43extern "C" typedef void (*kmpc_micro_13)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
44extern "C" typedef void (*kmpc_micro_14)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
45extern "C" typedef void (*kmpc_micro_15)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
46extern "C" typedef void (*kmpc_micro_16)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
47extern "C" typedef void (*kmpc_micro_17)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
48extern "C" typedef void (*kmpc_micro_18)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
49extern "C" typedef void (*kmpc_micro_19)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
50extern "C" typedef void (*kmpc_micro_20)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
51extern "C" typedef void (*kmpc_micro_21)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
52extern "C" typedef void (*kmpc_micro_22)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
53extern "C" typedef void (*kmpc_micro_23)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
54extern "C" typedef void (*kmpc_micro_24)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
55extern "C" typedef void (*kmpc_micro_25)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
56extern "C" typedef void (*kmpc_micro_26)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
57extern "C" typedef void (*kmpc_micro_27)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
58extern "C" typedef void (*kmpc_micro_28)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
59extern "C" typedef void (*kmpc_micro_29)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
60extern "C" typedef void (*kmpc_micro_30)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
61extern "C" typedef void (*kmpc_micro_31)(int32_t* gtid, int32_t* tid, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*, void*);
62#endif // __clang__
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68static void init_g_kmp_global();
69static void* kmp_threadfunc(void* args);
70
71#ifdef __cplusplus
72} // extern "C"
73#endif
74
75namespace ncnn {
76
77class KMPTask
78{
79public:
80 // per-team
81#if __clang__
82 // libomp abi
83 kmpc_micro fn;
84 int argc;
85 void** argv;
86#else
87 // libgomp abi
88 void (*fn)(void*);
89 void* data;
90#endif
91 int num_threads;
92
93 // per-task
94 int thread_num;
95
96 // finish status
97 int* num_threads_to_wait;
98 Mutex* finish_lock;
99 ConditionVariable* finish_condition;
100};
101
102class KMPTaskQueue
103{
104public:
105 KMPTaskQueue(int _max_size)
106 {
107 max_size = _max_size;
108 tasks = new KMPTask*[max_size];
109 size = 0;
110 front = 0;
111 back = 0;
112 }
113
114 ~KMPTaskQueue()
115 {
116 delete[] tasks;
117 }
118
119 void dispatch(KMPTask* v, int n)
120 {
121 lock.lock();
122
123 if (size + n > max_size)
124 {
125 lock.unlock();
126
127 for (int i = 0; i < n; i++)
128 {
129 put(&v[i]);
130 }
131 return;
132 }
133
134 for (int i = 0; i < n; i++)
135 {
136 tasks[back] = &v[i];
137 back++;
138 if (back == max_size)
139 back = 0;
140 }
141
142 size += n;
143
144 lock.unlock();
145
146 condition.signal();
147 }
148
149 void put(KMPTask* v)
150 {
151 lock.lock();
152 while (size >= max_size)
153 {
154 condition.wait(lock);
155 }
156 tasks[back] = v;
157 back++;
158 if (back == max_size)
159 back = 0;
160 size++;
161 lock.unlock();
162
163 condition.signal();
164 }
165
166 void get(KMPTask*& v)
167 {
168 lock.lock();
169 while (size == 0)
170 {
171 condition.wait(lock);
172 }
173 v = tasks[front];
174 front++;
175 if (front == max_size)
176 front = 0;
177 size--;
178 lock.unlock();
179
180 condition.signal();
181 }
182
183private:
184 Mutex lock;
185 ConditionVariable condition;
186
187 // ring buffer queue
188 int max_size;
189 KMPTask** tasks;
190 int size;
191 int front;
192 int back;
193};
194
195class KMPGlobal
196{
197public:
198 KMPGlobal()
199 {
200 kmp_max_threads = 0;
201 kmp_threads = 0;
202 kmp_threads_tid = 0;
203 kmp_task_queue = 0;
204 }
205
206 ~KMPGlobal()
207 {
208 deinit();
209 }
210
211 void try_init()
212 {
213 pthread_once(&is_initialized, init_g_kmp_global);
214 }
215
216public:
217 static pthread_once_t is_initialized;
218
219 void init()
220 {
221 // NCNN_LOGE("KMPGlobal init");
222 kmp_max_threads = ncnn::get_cpu_count();
223
224 kmp_task_queue = new ncnn::KMPTaskQueue(std::max(kmp_max_threads * 4, 16));
225
226 if (kmp_max_threads > 1)
227 {
228 kmp_threads = new ncnn::Thread*[kmp_max_threads - 1];
229 kmp_threads_tid = new int[kmp_max_threads - 1];
230 for (int i = 0; i < kmp_max_threads - 1; i++)
231 {
232 kmp_threads_tid[i] = i + 1;
233 kmp_threads[i] = new ncnn::Thread(kmp_threadfunc, (void*)&kmp_threads_tid[i]);
234 }
235 }
236 }
237
238 void deinit()
239 {
240 // NCNN_LOGE("KMPGlobal deinit");
241 if (kmp_max_threads > 1)
242 {
243 // TODO portable stack allocation
244 ncnn::KMPTask* tasks = (ncnn::KMPTask*)alloca((kmp_max_threads - 1) * sizeof(ncnn::KMPTask));
245 for (int i = 0; i < kmp_max_threads - 1; i++)
246 {
247#if __clang__
248 tasks[i].fn = 0;
249 tasks[i].argc = 0;
250 tasks[i].argv = (void**)0;
251#else
252 tasks[i].fn = 0;
253 tasks[i].data = 0;
254#endif
255 tasks[i].num_threads = kmp_max_threads;
256 tasks[i].thread_num = i + 1;
257 tasks[i].num_threads_to_wait = 0;
258 tasks[i].finish_lock = 0;
259 tasks[i].finish_condition = 0;
260 }
261
262 // dispatch 1 ~ kmp_max_threads
263 kmp_task_queue->dispatch(tasks, kmp_max_threads - 1);
264
265 for (int i = 0; i < kmp_max_threads - 1; i++)
266 {
267#ifndef __EMSCRIPTEN__
268 // FIXME emscripten complains
269 // pthread_join attempted on thread 12345678,
270 // which does not point to a valid thread, or does not exist anymore!
271 kmp_threads[i]->join();
272#endif
273 delete kmp_threads[i];
274 }
275 delete[] kmp_threads;
276 delete[] kmp_threads_tid;
277 }
278
279 delete kmp_task_queue;
280 }
281
282public:
283 int kmp_max_threads;
284 ncnn::Thread** kmp_threads;
285 int* kmp_threads_tid;
286 ncnn::KMPTaskQueue* kmp_task_queue;
287};
288
289} // namespace ncnn
290
291pthread_once_t ncnn::KMPGlobal::is_initialized = PTHREAD_ONCE_INIT;
292
293static ncnn::KMPGlobal g_kmp_global;
294
295static ncnn::ThreadLocalStorage tls_num_threads;
296static ncnn::ThreadLocalStorage tls_thread_num;
297
298static void init_g_kmp_global()
299{
300 g_kmp_global.init();
301}
302
303#ifdef __cplusplus
304extern "C" {
305#endif
306
307int omp_get_max_threads()
308{
309 return ncnn::get_cpu_count();
310}
311
312int omp_get_dynamic()
313{
314 return 1;
315}
316
317void omp_set_dynamic(int /*dynamic*/)
318{
319 // always dynamic, ignore
320}
321
322void omp_set_num_threads(int num_threads)
323{
324 tls_num_threads.set(reinterpret_cast<void*>((size_t)std::max(num_threads, 1)));
325}
326
327int omp_get_num_threads()
328{
329 return std::max((int)reinterpret_cast<size_t>(tls_num_threads.get()), 1);
330}
331
332int omp_get_thread_num()
333{
334 return (int)reinterpret_cast<size_t>(tls_thread_num.get());
335}
336
337#if __clang__
338int kmp_get_blocktime()
339{
340 return 0;
341}
342
343void kmp_set_blocktime(int /*blocktime*/)
344{
345 // always passive, ignore
346}
347
348static int kmp_invoke_microtask(kmpc_micro fn, int gtid, int tid, int argc, void** argv)
349{
350 // fprintf(stderr, "__kmp_invoke_microtask %d %d %d\n", gtid, tid, argc);
351
352 switch (argc)
353 {
354 case 0:
355 (*(kmpc_micro_0)fn)(&gtid, &tid);
356 break;
357 case 1:
358 (*(kmpc_micro_1)fn)(&gtid, &tid, argv[0]);
359 break;
360 case 2:
361 (*(kmpc_micro_2)fn)(&gtid, &tid, argv[0], argv[1]);
362 break;
363 case 3:
364 (*(kmpc_micro_3)fn)(&gtid, &tid, argv[0], argv[1], argv[2]);
365 break;
366 case 4:
367 (*(kmpc_micro_4)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3]);
368 break;
369 case 5:
370 (*(kmpc_micro_5)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4]);
371 break;
372 case 6:
373 (*(kmpc_micro_6)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
374 break;
375 case 7:
376 (*(kmpc_micro_7)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
377 break;
378 case 8:
379 (*(kmpc_micro_8)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
380 break;
381 case 9:
382 (*(kmpc_micro_9)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
383 break;
384 case 10:
385 (*(kmpc_micro_10)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
386 break;
387 case 11:
388 (*(kmpc_micro_11)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10]);
389 break;
390 case 12:
391 (*(kmpc_micro_12)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11]);
392 break;
393 case 13:
394 (*(kmpc_micro_13)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12]);
395 break;
396 case 14:
397 (*(kmpc_micro_14)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13]);
398 break;
399 case 15:
400 (*(kmpc_micro_15)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14]);
401 break;
402 case 16:
403 (*(kmpc_micro_16)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15]);
404 break;
405 case 17:
406 (*(kmpc_micro_17)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16]);
407 break;
408 case 18:
409 (*(kmpc_micro_18)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17]);
410 break;
411 case 19:
412 (*(kmpc_micro_19)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18]);
413 break;
414 case 20:
415 (*(kmpc_micro_20)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]);
416 break;
417 case 21:
418 (*(kmpc_micro_21)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20]);
419 break;
420 case 22:
421 (*(kmpc_micro_22)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21]);
422 break;
423 case 23:
424 (*(kmpc_micro_23)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22]);
425 break;
426 case 24:
427 (*(kmpc_micro_24)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23]);
428 break;
429 case 25:
430 (*(kmpc_micro_25)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24]);
431 break;
432 case 26:
433 (*(kmpc_micro_26)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25]);
434 break;
435 case 27:
436 (*(kmpc_micro_27)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25], argv[26]);
437 break;
438 case 28:
439 (*(kmpc_micro_28)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25], argv[26], argv[27]);
440 break;
441 case 29:
442 (*(kmpc_micro_29)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25], argv[26], argv[27], argv[28]);
443 break;
444 case 30:
445 (*(kmpc_micro_30)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25], argv[26], argv[27], argv[28], argv[29]);
446 break;
447 case 31:
448 (*(kmpc_micro_31)fn)(&gtid, &tid, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19], argv[20], argv[21], argv[22], argv[23], argv[24], argv[25], argv[26], argv[27], argv[28], argv[29], argv[30]);
449 break;
450 default:
451 // assert never reach here
452 break;
453 }
454
455 return 0;
456}
457#endif // __clang__
458
459static void* kmp_threadfunc(void* args)
460{
461#if __clang__
462 int tid = *(int*)args;
463#else
464 (void)args;
465#endif
466
467 for (;;)
468 {
469 ncnn::KMPTask* task;
470 g_kmp_global.kmp_task_queue->get(task);
471
472 // fprintf(stderr, "get %d\n", tid);
473
474 if (!task->fn)
475 break;
476
477 tls_num_threads.set(reinterpret_cast<void*>((size_t)task->num_threads));
478 tls_thread_num.set(reinterpret_cast<void*>((size_t)task->thread_num));
479
480#if __clang__
481 kmp_invoke_microtask(task->fn, task->thread_num, tid, task->argc, task->argv);
482#else
483 task->fn(task->data);
484#endif
485
486 // update finished
487 {
488 task->finish_lock->lock();
489 *task->num_threads_to_wait = *task->num_threads_to_wait - 1;
490 if (*task->num_threads_to_wait == 0)
491 {
492 task->finish_condition->signal();
493 }
494 task->finish_lock->unlock();
495 }
496 }
497
498 // fprintf(stderr, "exit\n");
499 return 0;
500}
501
502#if __clang__
503int32_t __kmpc_global_thread_num(void* /*loc*/)
504{
505 // NCNN_LOGE("__kmpc_global_thread_num");
506 return 0;
507}
508
509void __kmpc_push_num_threads(void* /*loc*/, int32_t /*gtid*/, int32_t num_threads)
510{
511 // NCNN_LOGE("__kmpc_push_num_threads %d", num_threads);
512 omp_set_num_threads(num_threads);
513}
514
515void __kmpc_fork_call(void* /*loc*/, int32_t argc, kmpc_micro fn, ...)
516{
517 g_kmp_global.try_init();
518
519 // NCNN_LOGE("__kmpc_fork_call %d", argc);
520 int num_threads = omp_get_num_threads();
521
522 // build argv
523 void* argv[32];
524 {
525 va_list ap;
526 va_start(ap, fn);
527 for (int i = 0; i < argc; i++)
528 argv[i] = va_arg(ap, void*);
529 va_end(ap);
530 }
531
532 if (g_kmp_global.kmp_max_threads == 1 || num_threads == 1)
533 {
534 for (int i = 0; i < num_threads; i++)
535 {
536 tls_thread_num.set(reinterpret_cast<void*>((size_t)i));
537
538 kmp_invoke_microtask(fn, 0, 0, argc, argv);
539 }
540
541 return;
542 }
543
544 int num_threads_to_wait = num_threads - 1;
545 ncnn::Mutex finish_lock;
546 ncnn::ConditionVariable finish_condition;
547
548 // TODO portable stack allocation
549 ncnn::KMPTask* tasks = (ncnn::KMPTask*)alloca((num_threads - 1) * sizeof(ncnn::KMPTask));
550 for (int i = 0; i < num_threads - 1; i++)
551 {
552 tasks[i].fn = fn;
553 tasks[i].argc = argc;
554 tasks[i].argv = (void**)argv;
555 tasks[i].num_threads = num_threads;
556 tasks[i].thread_num = i + 1;
557 tasks[i].num_threads_to_wait = &num_threads_to_wait;
558 tasks[i].finish_lock = &finish_lock;
559 tasks[i].finish_condition = &finish_condition;
560 }
561
562 // dispatch 1 ~ num_threads
563 g_kmp_global.kmp_task_queue->dispatch(tasks, num_threads - 1);
564
565 // dispatch 0
566 {
567 tls_num_threads.set(reinterpret_cast<void*>((size_t)num_threads));
568 tls_thread_num.set(reinterpret_cast<void*>((size_t)0));
569
570 kmp_invoke_microtask(fn, 0, 0, argc, argv);
571 }
572
573 // wait for finished
574 {
575 finish_lock.lock();
576 if (num_threads_to_wait != 0)
577 {
578 finish_condition.wait(finish_lock);
579 }
580 finish_lock.unlock();
581 }
582}
583
584void __kmpc_for_static_init_4(void* /*loc*/, int32_t gtid, int32_t /*sched*/, int32_t* last, int32_t* lower, int32_t* upper, int32_t* /*stride*/, int32_t /*incr*/, int32_t /*chunk*/)
585{
586 // NCNN_LOGE("__kmpc_for_static_init_4");
587 int num_threads = omp_get_num_threads();
588
589 // TODO only support i++
590 int32_t count = *upper - *lower + 1;
591 int32_t threads = std::min(count, (int32_t)num_threads);
592 int32_t count_per_thread = count / threads;
593 int32_t remain = count % threads;
594
595 *last = gtid == (int32_t)(threads - 1);
596 *lower = gtid * count_per_thread + std::min(remain, gtid);
597 *upper = std::min((gtid + 1) * count_per_thread + std::min(remain, gtid + 1) - 1, *upper);
598}
599
600void __kmpc_for_static_init_4u(void* /*loc*/, int32_t gtid, int32_t /*sched*/, int32_t* last, uint32_t* lower, uint32_t* upper, int32_t* /*stride*/, int32_t /*incr*/, int32_t /*chunk*/)
601{
602 // NCNN_LOGE("__kmpc_for_static_init_4u");
603 int num_threads = omp_get_num_threads();
604
605 // TODO only support i++
606 uint32_t count = *upper - *lower + 1;
607 uint32_t threads = std::min(count, (uint32_t)num_threads);
608 uint32_t count_per_thread = count / threads;
609 uint32_t remain = count % threads;
610
611 *last = gtid == (int32_t)(threads - 1);
612 *lower = gtid * count_per_thread + std::min(remain, (uint32_t)gtid);
613 *upper = std::min((gtid + 1) * count_per_thread + std::min(remain, (uint32_t)gtid + 1) - 1, *upper);
614}
615
616void __kmpc_for_static_init_8(void* /*loc*/, int32_t gtid, int32_t /*sched*/, int32_t* last, int64_t* lower, int64_t* upper, int64_t* /*stride*/, int64_t /*incr*/, int64_t /*chunk*/)
617{
618 // NCNN_LOGE("__kmpc_for_static_init_8");
619 int num_threads = omp_get_num_threads();
620
621 // TODO only support i++
622 int64_t count = *upper - *lower + 1;
623 int64_t threads = std::min(count, (int64_t)num_threads);
624 int64_t count_per_thread = count / threads;
625 int64_t remain = count % threads;
626
627 *last = gtid == (int64_t)(threads - 1);
628 *lower = gtid * count_per_thread + std::min(remain, (int64_t)gtid);
629 *upper = std::min((gtid + 1) * count_per_thread + std::min(remain, (int64_t)gtid + 1) - 1, *upper);
630}
631
632void __kmpc_for_static_init_8u(void* /*loc*/, int32_t gtid, int32_t /*sched*/, int32_t* last, uint64_t* lower, uint64_t* upper, int64_t* /*stride*/, int64_t /*incr*/, int64_t /*chunk*/)
633{
634 // NCNN_LOGE("__kmpc_for_static_init_8u");
635 int num_threads = omp_get_num_threads();
636
637 // TODO only support i++
638 uint64_t count = *upper - *lower + 1;
639 uint64_t threads = std::min(count, (uint64_t)num_threads);
640 uint64_t count_per_thread = count / threads;
641 uint64_t remain = count % threads;
642
643 *last = gtid == (int64_t)(threads - 1);
644 *lower = gtid * count_per_thread + std::min(remain, (uint64_t)gtid);
645 *upper = std::min((gtid + 1) * count_per_thread + std::min(remain, (uint64_t)gtid + 1) - 1, *upper);
646}
647
648void __kmpc_for_static_fini(void* /*loc*/, int32_t gtid)
649{
650 // NCNN_LOGE("__kmpc_for_static_fini");
651 (void)gtid;
652}
653#else // __clang__
654
655static ncnn::ThreadLocalStorage tls_parallel_context;
656
657struct parallel_context
658{
659 int num_threads_to_wait;
660 ncnn::Mutex finish_lock;
661 ncnn::ConditionVariable finish_condition;
662 ncnn::KMPTask* tasks;
663};
664
665void GOMP_parallel_start(void (*fn)(void*), void* data, unsigned num_threads)
666{
667 g_kmp_global.try_init();
668
669 // NCNN_LOGE("GOMP_parallel_start %p %p %u", fn, data, num_threads);
670 if (num_threads == 0)
671 {
672 num_threads = omp_get_max_threads();
673 }
674
675 if (g_kmp_global.kmp_max_threads == 1 || num_threads == 1)
676 {
677 for (unsigned i = 0; i < num_threads; i++)
678 {
679 tls_num_threads.set(reinterpret_cast<void*>((size_t)num_threads));
680 tls_thread_num.set(reinterpret_cast<void*>((size_t)i));
681
682 fn(data);
683 }
684
685 return;
686 }
687
688 parallel_context* pc = new parallel_context;
689
690 tls_parallel_context.set(pc);
691
692 pc->num_threads_to_wait = num_threads - 1;
693
694 pc->tasks = new ncnn::KMPTask[num_threads - 1];
695 for (unsigned i = 0; i < num_threads - 1; i++)
696 {
697 pc->tasks[i].fn = fn;
698 pc->tasks[i].data = data;
699 pc->tasks[i].num_threads = num_threads;
700 pc->tasks[i].thread_num = i + 1;
701 pc->tasks[i].num_threads_to_wait = &pc->num_threads_to_wait;
702 pc->tasks[i].finish_lock = &pc->finish_lock;
703 pc->tasks[i].finish_condition = &pc->finish_condition;
704 }
705
706 // dispatch 1 ~ num_threads
707 g_kmp_global.kmp_task_queue->dispatch(pc->tasks, num_threads - 1);
708
709 // dispatch 0
710 {
711 tls_num_threads.set(reinterpret_cast<void*>((size_t)num_threads));
712 tls_thread_num.set(reinterpret_cast<void*>((size_t)0));
713 }
714}
715
716void GOMP_parallel_end()
717{
718 // NCNN_LOGE("GOMP_parallel_end");
719 parallel_context* pc = (parallel_context*)tls_parallel_context.get();
720 tls_parallel_context.set(0);
721
722 // wait for finished
723 {
724 pc->finish_lock.lock();
725 if (pc->num_threads_to_wait != 0)
726 {
727 pc->finish_condition.wait(pc->finish_lock);
728 }
729 pc->finish_lock.unlock();
730 }
731
732 delete[] pc->tasks;
733 delete pc;
734}
735
736void GOMP_parallel(void (*fn)(void*), void* data, unsigned num_threads, unsigned int /*flags*/)
737{
738 g_kmp_global.try_init();
739
740 // NCNN_LOGE("GOMP_parallel %p %p %u", fn, data, num_threads);
741 if (num_threads == 0)
742 {
743 num_threads = omp_get_max_threads();
744 }
745
746 if (g_kmp_global.kmp_max_threads == 1 || num_threads == 1)
747 {
748 for (unsigned i = 0; i < num_threads; i++)
749 {
750 tls_num_threads.set(reinterpret_cast<void*>((size_t)num_threads));
751 tls_thread_num.set(reinterpret_cast<void*>((size_t)i));
752
753 fn(data);
754 }
755
756 return;
757 }
758
759 int num_threads_to_wait = num_threads - 1;
760 ncnn::Mutex finish_lock;
761 ncnn::ConditionVariable finish_condition;
762
763 // TODO portable stack allocation
764 ncnn::KMPTask* tasks = (ncnn::KMPTask*)alloca((num_threads - 1) * sizeof(ncnn::KMPTask));
765 for (unsigned i = 0; i < num_threads - 1; i++)
766 {
767 tasks[i].fn = fn;
768 tasks[i].data = data;
769 tasks[i].num_threads = num_threads;
770 tasks[i].thread_num = i + 1;
771 tasks[i].num_threads_to_wait = &num_threads_to_wait;
772 tasks[i].finish_lock = &finish_lock;
773 tasks[i].finish_condition = &finish_condition;
774 }
775
776 // dispatch 1 ~ num_threads
777 g_kmp_global.kmp_task_queue->dispatch(tasks, num_threads - 1);
778
779 // dispatch 0
780 {
781 tls_num_threads.set(reinterpret_cast<void*>((size_t)num_threads));
782 tls_thread_num.set(reinterpret_cast<void*>((size_t)0));
783
784 fn(data);
785 }
786
787 // wait for finished
788 {
789 finish_lock.lock();
790 if (num_threads_to_wait != 0)
791 {
792 finish_condition.wait(finish_lock);
793 }
794 finish_lock.unlock();
795 }
796}
797#endif // __clang__
798
799#ifdef __cplusplus
800} // extern "C"
801#endif
802
803#endif // NCNN_SIMPLEOMP