• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Standard C headers */
2 #include <stddef.h>
3 
4 /* Public library header */
5 #include <pthreadpool.h>
6 
7 /* Internal library headers */
8 #include "threadpool-utils.h"
9 
10 
11 struct pthreadpool {
12 };
13 
14 static const struct pthreadpool static_pthreadpool = { };
15 
16 
pthreadpool_create(size_t threads_count)17 struct pthreadpool* pthreadpool_create(size_t threads_count) {
18 	if (threads_count <= 1) {
19 		return (struct pthreadpool*) &static_pthreadpool;
20 	}
21 
22 	return NULL;
23 }
24 
pthreadpool_get_threads_count(struct pthreadpool * threadpool)25 size_t pthreadpool_get_threads_count(struct pthreadpool* threadpool) {
26 	return 1;
27 }
28 
pthreadpool_parallelize_1d(struct pthreadpool * threadpool,pthreadpool_task_1d_t task,void * argument,size_t range,uint32_t flags)29 void pthreadpool_parallelize_1d(
30 	struct pthreadpool* threadpool,
31 	pthreadpool_task_1d_t task,
32 	void* argument,
33 	size_t range,
34 	uint32_t flags)
35 {
36 	for (size_t i = 0; i < range; i++) {
37 		task(argument, i);
38 	}
39 }
40 
pthreadpool_parallelize_1d_with_uarch(pthreadpool_t threadpool,pthreadpool_task_1d_with_id_t task,void * argument,uint32_t default_uarch_index,uint32_t max_uarch_index,size_t range,uint32_t flags)41 void pthreadpool_parallelize_1d_with_uarch(
42 	pthreadpool_t threadpool,
43 	pthreadpool_task_1d_with_id_t task,
44 	void* argument,
45 	uint32_t default_uarch_index,
46 	uint32_t max_uarch_index,
47 	size_t range,
48 	uint32_t flags)
49 {
50 	for (size_t i = 0; i < range; i++) {
51 		task(argument, default_uarch_index, i);
52 	}
53 }
54 
pthreadpool_parallelize_1d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_1d_tile_1d_t task,void * argument,size_t range,size_t tile,uint32_t flags)55 void pthreadpool_parallelize_1d_tile_1d(
56 	pthreadpool_t threadpool,
57 	pthreadpool_task_1d_tile_1d_t task,
58 	void* argument,
59 	size_t range,
60 	size_t tile,
61 	uint32_t flags)
62 {
63 	for (size_t i = 0; i < range; i += tile) {
64 		task(argument, i, min(range - i, tile));
65 	}
66 }
67 
pthreadpool_parallelize_2d(struct pthreadpool * threadpool,pthreadpool_task_2d_t task,void * argument,size_t range_i,size_t range_j,uint32_t flags)68 void pthreadpool_parallelize_2d(
69 	struct pthreadpool* threadpool,
70 	pthreadpool_task_2d_t task,
71 	void* argument,
72 	size_t range_i,
73 	size_t range_j,
74 	uint32_t flags)
75 {
76 	for (size_t i = 0; i < range_i; i++) {
77 		for (size_t j = 0; j < range_j; j++) {
78 			task(argument, i, j);
79 		}
80 	}
81 }
82 
pthreadpool_parallelize_2d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_2d_tile_1d_t task,void * argument,size_t range_i,size_t range_j,size_t tile_j,uint32_t flags)83 void pthreadpool_parallelize_2d_tile_1d(
84 	pthreadpool_t threadpool,
85 	pthreadpool_task_2d_tile_1d_t task,
86 	void* argument,
87 	size_t range_i,
88 	size_t range_j,
89 	size_t tile_j,
90 	uint32_t flags)
91 {
92 	for (size_t i = 0; i < range_i; i++) {
93 		for (size_t j = 0; j < range_j; j += tile_j) {
94 			task(argument, i, j, min(range_j - j, tile_j));
95 		}
96 	}
97 }
98 
pthreadpool_parallelize_2d_tile_2d(pthreadpool_t threadpool,pthreadpool_task_2d_tile_2d_t task,void * argument,size_t range_i,size_t range_j,size_t tile_i,size_t tile_j,uint32_t flags)99 void pthreadpool_parallelize_2d_tile_2d(
100 	pthreadpool_t threadpool,
101 	pthreadpool_task_2d_tile_2d_t task,
102 	void* argument,
103 	size_t range_i,
104 	size_t range_j,
105 	size_t tile_i,
106 	size_t tile_j,
107 	uint32_t flags)
108 {
109 	for (size_t i = 0; i < range_i; i += tile_i) {
110 		for (size_t j = 0; j < range_j; j += tile_j) {
111 			task(argument, i, j, min(range_i - i, tile_i), min(range_j - j, tile_j));
112 		}
113 	}
114 }
115 
pthreadpool_parallelize_2d_tile_2d_with_uarch(pthreadpool_t threadpool,pthreadpool_task_2d_tile_2d_with_id_t task,void * argument,uint32_t default_uarch_index,uint32_t max_uarch_index,size_t range_i,size_t range_j,size_t tile_i,size_t tile_j,uint32_t flags)116 void pthreadpool_parallelize_2d_tile_2d_with_uarch(
117 	pthreadpool_t threadpool,
118 	pthreadpool_task_2d_tile_2d_with_id_t task,
119 	void* argument,
120 	uint32_t default_uarch_index,
121 	uint32_t max_uarch_index,
122 	size_t range_i,
123 	size_t range_j,
124 	size_t tile_i,
125 	size_t tile_j,
126 	uint32_t flags)
127 {
128 	for (size_t i = 0; i < range_i; i += tile_i) {
129 		for (size_t j = 0; j < range_j; j += tile_j) {
130 			task(argument, default_uarch_index, i, j,
131 				min(range_i - i, tile_i), min(range_j - j, tile_j));
132 		}
133 	}
134 }
135 
pthreadpool_parallelize_3d(pthreadpool_t threadpool,pthreadpool_task_3d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,uint32_t flags)136 void pthreadpool_parallelize_3d(
137 	pthreadpool_t threadpool,
138 	pthreadpool_task_3d_t task,
139 	void* argument,
140 	size_t range_i,
141 	size_t range_j,
142 	size_t range_k,
143 	uint32_t flags)
144 {
145 	for (size_t i = 0; i < range_i; i++) {
146 		for (size_t j = 0; j < range_j; j++) {
147 			for (size_t k = 0; k < range_k; k++) {
148 				task(argument, i, j, k);
149 			}
150 		}
151 	}
152 }
153 
pthreadpool_parallelize_3d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_3d_tile_1d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t tile_k,uint32_t flags)154 void pthreadpool_parallelize_3d_tile_1d(
155 	pthreadpool_t threadpool,
156 	pthreadpool_task_3d_tile_1d_t task,
157 	void* argument,
158 	size_t range_i,
159 	size_t range_j,
160 	size_t range_k,
161 	size_t tile_k,
162 	uint32_t flags)
163 {
164 	for (size_t i = 0; i < range_i; i++) {
165 		for (size_t j = 0; j < range_j; j++) {
166 			for (size_t k = 0; k < range_k; k += tile_k) {
167 				task(argument, i, j, k, min(range_k - k, tile_k));
168 			}
169 		}
170 	}
171 }
172 
pthreadpool_parallelize_3d_tile_2d(pthreadpool_t threadpool,pthreadpool_task_3d_tile_2d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t tile_j,size_t tile_k,uint32_t flags)173 void pthreadpool_parallelize_3d_tile_2d(
174 	pthreadpool_t threadpool,
175 	pthreadpool_task_3d_tile_2d_t task,
176 	void* argument,
177 	size_t range_i,
178 	size_t range_j,
179 	size_t range_k,
180 	size_t tile_j,
181 	size_t tile_k,
182 	uint32_t flags)
183 {
184 	for (size_t i = 0; i < range_i; i++) {
185 		for (size_t j = 0; j < range_j; j += tile_j) {
186 			for (size_t k = 0; k < range_k; k += tile_k) {
187 				task(argument, i, j, k,
188 					min(range_j - j, tile_j), min(range_k - k, tile_k));
189 			}
190 		}
191 	}
192 }
193 
pthreadpool_parallelize_3d_tile_2d_with_uarch(pthreadpool_t threadpool,pthreadpool_task_3d_tile_2d_with_id_t task,void * argument,uint32_t default_uarch_index,uint32_t max_uarch_index,size_t range_i,size_t range_j,size_t range_k,size_t tile_j,size_t tile_k,uint32_t flags)194 void pthreadpool_parallelize_3d_tile_2d_with_uarch(
195 	pthreadpool_t threadpool,
196 	pthreadpool_task_3d_tile_2d_with_id_t task,
197 	void* argument,
198 	uint32_t default_uarch_index,
199 	uint32_t max_uarch_index,
200 	size_t range_i,
201 	size_t range_j,
202 	size_t range_k,
203 	size_t tile_j,
204 	size_t tile_k,
205 	uint32_t flags)
206 {
207 	for (size_t i = 0; i < range_i; i++) {
208 		for (size_t j = 0; j < range_j; j += tile_j) {
209 			for (size_t k = 0; k < range_k; k += tile_k) {
210 				task(argument, default_uarch_index, i, j, k,
211 					min(range_j - j, tile_j), min(range_k - k, tile_k));
212 			}
213 		}
214 	}
215 }
216 
pthreadpool_parallelize_4d(pthreadpool_t threadpool,pthreadpool_task_4d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,uint32_t flags)217 void pthreadpool_parallelize_4d(
218 	pthreadpool_t threadpool,
219 	pthreadpool_task_4d_t task,
220 	void* argument,
221 	size_t range_i,
222 	size_t range_j,
223 	size_t range_k,
224 	size_t range_l,
225 	uint32_t flags)
226 {
227 	for (size_t i = 0; i < range_i; i++) {
228 		for (size_t j = 0; j < range_j; j++) {
229 			for (size_t k = 0; k < range_k; k++) {
230 				for (size_t l = 0; l < range_l; l++) {
231 					task(argument, i, j, k, l);
232 				}
233 			}
234 		}
235 	}
236 }
237 
pthreadpool_parallelize_4d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_4d_tile_1d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t tile_l,uint32_t flags)238 void pthreadpool_parallelize_4d_tile_1d(
239 	pthreadpool_t threadpool,
240 	pthreadpool_task_4d_tile_1d_t task,
241 	void* argument,
242 	size_t range_i,
243 	size_t range_j,
244 	size_t range_k,
245 	size_t range_l,
246 	size_t tile_l,
247 	uint32_t flags)
248 {
249 	for (size_t i = 0; i < range_i; i++) {
250 		for (size_t j = 0; j < range_j; j++) {
251 			for (size_t k = 0; k < range_k; k++) {
252 				for (size_t l = 0; l < range_l; l += tile_l) {
253 					task(argument, i, j, k, l, min(range_l - l, tile_l));
254 				}
255 			}
256 		}
257 	}
258 }
259 
pthreadpool_parallelize_4d_tile_2d(pthreadpool_t threadpool,pthreadpool_task_4d_tile_2d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t tile_k,size_t tile_l,uint32_t flags)260 void pthreadpool_parallelize_4d_tile_2d(
261 	pthreadpool_t threadpool,
262 	pthreadpool_task_4d_tile_2d_t task,
263 	void* argument,
264 	size_t range_i,
265 	size_t range_j,
266 	size_t range_k,
267 	size_t range_l,
268 	size_t tile_k,
269 	size_t tile_l,
270 	uint32_t flags)
271 {
272 	for (size_t i = 0; i < range_i; i++) {
273 		for (size_t j = 0; j < range_j; j++) {
274 			for (size_t k = 0; k < range_k; k += tile_k) {
275 				for (size_t l = 0; l < range_l; l += tile_l) {
276 					task(argument, i, j, k, l,
277 						min(range_k - k, tile_k), min(range_l - l, tile_l));
278 				}
279 			}
280 		}
281 	}
282 }
283 
pthreadpool_parallelize_4d_tile_2d_with_uarch(pthreadpool_t threadpool,pthreadpool_task_4d_tile_2d_with_id_t task,void * argument,uint32_t default_uarch_index,uint32_t max_uarch_index,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t tile_k,size_t tile_l,uint32_t flags)284 void pthreadpool_parallelize_4d_tile_2d_with_uarch(
285 	pthreadpool_t threadpool,
286 	pthreadpool_task_4d_tile_2d_with_id_t task,
287 	void* argument,
288 	uint32_t default_uarch_index,
289 	uint32_t max_uarch_index,
290 	size_t range_i,
291 	size_t range_j,
292 	size_t range_k,
293 	size_t range_l,
294 	size_t tile_k,
295 	size_t tile_l,
296 	uint32_t flags)
297 {
298 	for (size_t i = 0; i < range_i; i++) {
299 		for (size_t j = 0; j < range_j; j++) {
300 			for (size_t k = 0; k < range_k; k += tile_k) {
301 				for (size_t l = 0; l < range_l; l += tile_l) {
302 					task(argument, default_uarch_index, i, j, k, l,
303 						min(range_k - k, tile_k), min(range_l - l, tile_l));
304 				}
305 			}
306 		}
307 	}
308 }
309 
pthreadpool_parallelize_5d(pthreadpool_t threadpool,pthreadpool_task_5d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,uint32_t flags)310 void pthreadpool_parallelize_5d(
311 	pthreadpool_t threadpool,
312 	pthreadpool_task_5d_t task,
313 	void* argument,
314 	size_t range_i,
315 	size_t range_j,
316 	size_t range_k,
317 	size_t range_l,
318 	size_t range_m,
319 	uint32_t flags)
320 {
321 	for (size_t i = 0; i < range_i; i++) {
322 		for (size_t j = 0; j < range_j; j++) {
323 			for (size_t k = 0; k < range_k; k++) {
324 				for (size_t l = 0; l < range_l; l++) {
325 					for (size_t m = 0; m < range_m; m++) {
326 						task(argument, i, j, k, l, m);
327 					}
328 				}
329 			}
330 		}
331 	}
332 }
333 
pthreadpool_parallelize_5d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_5d_tile_1d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,size_t tile_m,uint32_t flags)334 void pthreadpool_parallelize_5d_tile_1d(
335 	pthreadpool_t threadpool,
336 	pthreadpool_task_5d_tile_1d_t task,
337 	void* argument,
338 	size_t range_i,
339 	size_t range_j,
340 	size_t range_k,
341 	size_t range_l,
342 	size_t range_m,
343 	size_t tile_m,
344 	uint32_t flags)
345 {
346 	for (size_t i = 0; i < range_i; i++) {
347 		for (size_t j = 0; j < range_j; j++) {
348 			for (size_t k = 0; k < range_k; k++) {
349 				for (size_t l = 0; l < range_l; l++) {
350 					for (size_t m = 0; m < range_m; m += tile_m) {
351 						task(argument, i, j, k, l, m, min(range_m - m, tile_m));
352 					}
353 				}
354 			}
355 		}
356 	}
357 }
358 
pthreadpool_parallelize_5d_tile_2d(pthreadpool_t threadpool,pthreadpool_task_5d_tile_2d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,size_t tile_l,size_t tile_m,uint32_t flags)359 void pthreadpool_parallelize_5d_tile_2d(
360 	pthreadpool_t threadpool,
361 	pthreadpool_task_5d_tile_2d_t task,
362 	void* argument,
363 	size_t range_i,
364 	size_t range_j,
365 	size_t range_k,
366 	size_t range_l,
367 	size_t range_m,
368 	size_t tile_l,
369 	size_t tile_m,
370 	uint32_t flags)
371 {
372 	for (size_t i = 0; i < range_i; i++) {
373 		for (size_t j = 0; j < range_j; j++) {
374 			for (size_t k = 0; k < range_k; k++) {
375 				for (size_t l = 0; l < range_l; l += tile_l) {
376 					for (size_t m = 0; m < range_m; m += tile_m) {
377 						task(argument, i, j, k, l, m,
378 							min(range_l - l, tile_l), min(range_m - m, tile_m));
379 					}
380 				}
381 			}
382 		}
383 	}
384 }
385 
pthreadpool_parallelize_6d(pthreadpool_t threadpool,pthreadpool_task_6d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,size_t range_n,uint32_t flags)386 void pthreadpool_parallelize_6d(
387 	pthreadpool_t threadpool,
388 	pthreadpool_task_6d_t task,
389 	void* argument,
390 	size_t range_i,
391 	size_t range_j,
392 	size_t range_k,
393 	size_t range_l,
394 	size_t range_m,
395 	size_t range_n,
396 	uint32_t flags)
397 {
398 	for (size_t i = 0; i < range_i; i++) {
399 		for (size_t j = 0; j < range_j; j++) {
400 			for (size_t k = 0; k < range_k; k++) {
401 				for (size_t l = 0; l < range_l; l++) {
402 					for (size_t m = 0; m < range_m; m++) {
403 						for (size_t n = 0; n < range_n; n++) {
404 							task(argument, i, j, k, l, m, n);
405 						}
406 					}
407 				}
408 			}
409 		}
410 	}
411 }
412 
pthreadpool_parallelize_6d_tile_1d(pthreadpool_t threadpool,pthreadpool_task_6d_tile_1d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,size_t range_n,size_t tile_n,uint32_t flags)413 void pthreadpool_parallelize_6d_tile_1d(
414 	pthreadpool_t threadpool,
415 	pthreadpool_task_6d_tile_1d_t task,
416 	void* argument,
417 	size_t range_i,
418 	size_t range_j,
419 	size_t range_k,
420 	size_t range_l,
421 	size_t range_m,
422 	size_t range_n,
423 	size_t tile_n,
424 	uint32_t flags)
425 {
426 	for (size_t i = 0; i < range_i; i++) {
427 		for (size_t j = 0; j < range_j; j++) {
428 			for (size_t k = 0; k < range_k; k++) {
429 				for (size_t l = 0; l < range_l; l++) {
430 					for (size_t m = 0; m < range_m; m++) {
431 						for (size_t n = 0; n < range_n; n += tile_n) {
432 							task(argument, i, j, k, l, m, n, min(range_n - n, tile_n));
433 						}
434 					}
435 				}
436 			}
437 		}
438 	}
439 }
440 
pthreadpool_parallelize_6d_tile_2d(pthreadpool_t threadpool,pthreadpool_task_6d_tile_2d_t task,void * argument,size_t range_i,size_t range_j,size_t range_k,size_t range_l,size_t range_m,size_t range_n,size_t tile_m,size_t tile_n,uint32_t flags)441 void pthreadpool_parallelize_6d_tile_2d(
442 	pthreadpool_t threadpool,
443 	pthreadpool_task_6d_tile_2d_t task,
444 	void* argument,
445 	size_t range_i,
446 	size_t range_j,
447 	size_t range_k,
448 	size_t range_l,
449 	size_t range_m,
450 	size_t range_n,
451 	size_t tile_m,
452 	size_t tile_n,
453 	uint32_t flags)
454 {
455 	for (size_t i = 0; i < range_i; i++) {
456 		for (size_t j = 0; j < range_j; j++) {
457 			for (size_t k = 0; k < range_k; k++) {
458 				for (size_t l = 0; l < range_l; l++) {
459 					for (size_t m = 0; m < range_m; m += tile_m) {
460 						for (size_t n = 0; n < range_n; n += tile_n) {
461 							task(argument, i, j, k, l, m, n,
462 								min(range_m - m, tile_m), min(range_n - n, tile_n));
463 						}
464 					}
465 				}
466 			}
467 		}
468 	}
469 }
470 
pthreadpool_destroy(struct pthreadpool * threadpool)471 void pthreadpool_destroy(struct pthreadpool* threadpool) {
472 }
473