• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef PTHREADPOOL_H_
2 #define PTHREADPOOL_H_
3 
4 #include <stddef.h>
5 #include <stdint.h>
6 
7 typedef struct pthreadpool* pthreadpool_t;
8 
9 typedef void (*pthreadpool_task_1d_t)(void*, size_t);
10 typedef void (*pthreadpool_task_1d_tile_1d_t)(void*, size_t, size_t);
11 typedef void (*pthreadpool_task_2d_t)(void*, size_t, size_t);
12 typedef void (*pthreadpool_task_2d_tile_1d_t)(void*, size_t, size_t, size_t);
13 typedef void (*pthreadpool_task_2d_tile_2d_t)(void*, size_t, size_t, size_t, size_t);
14 typedef void (*pthreadpool_task_3d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t);
15 typedef void (*pthreadpool_task_4d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t);
16 typedef void (*pthreadpool_task_5d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
17 typedef void (*pthreadpool_task_6d_tile_2d_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t);
18 
19 
20 #define PTHREADPOOL_FLAG_DISABLE_DENORMALS 0x00000001
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * Creates a thread pool with the specified number of threads.
28  *
29  * @param[in]  threads_count  The number of threads in the thread pool.
30  *    A value of 0 has special interpretation: it creates a thread for each
31  *    processor core available in the system.
32  *
33  * @returns  A pointer to an opaque thread pool object.
34  *    On error the function returns NULL and sets errno accordingly.
35  */
36 pthreadpool_t pthreadpool_create(size_t threads_count);
37 
38 /**
39  * Queries the number of threads in a thread pool.
40  *
41  * @param[in]  threadpool  The thread pool to query.
42  *
43  * @returns  The number of threads in the thread pool.
44  */
45 size_t pthreadpool_get_threads_count(pthreadpool_t threadpool);
46 
47 /**
48  * Processes items in parallel using threads from a thread pool.
49  *
50  * When the call returns, all items have been processed and the thread pool is
51  * ready for a new task.
52  *
53  * @note If multiple threads call this function with the same thread pool, the
54  *    calls are serialized.
55  *
56  * @param[in]  threadpool  The thread pool to use for parallelisation.
57  * @param[in]  function    The function to call for each item.
58  * @param[in]  argument    The first argument passed to the @a function.
59  * @param[in]  items       The number of items to process. The @a function
60  *    will be called once for each item.
61  */
62 void pthreadpool_parallelize_1d(
63 	pthreadpool_t threadpool,
64 	pthreadpool_task_1d_t function,
65 	void* argument,
66 	size_t range,
67 	uint32_t flags);
68 
69 void pthreadpool_parallelize_1d_tile_1d(
70 	pthreadpool_t threadpool,
71 	pthreadpool_task_1d_tile_1d_t function,
72 	void* argument,
73 	size_t range,
74 	size_t tile,
75 	uint32_t flags);
76 
77 void pthreadpool_parallelize_2d(
78 	pthreadpool_t threadpool,
79 	pthreadpool_task_2d_t function,
80 	void* argument,
81 	size_t range_i,
82 	size_t range_j,
83 	uint32_t flags);
84 
85 void pthreadpool_parallelize_2d_tile_1d(
86 	pthreadpool_t threadpool,
87 	pthreadpool_task_2d_tile_1d_t function,
88 	void* argument,
89 	size_t range_i,
90 	size_t range_j,
91 	size_t tile_j,
92 	uint32_t flags);
93 
94 void pthreadpool_parallelize_2d_tile_2d(
95 	pthreadpool_t threadpool,
96 	pthreadpool_task_2d_tile_2d_t function,
97 	void* argument,
98 	size_t range_i,
99 	size_t range_j,
100 	size_t tile_i,
101 	size_t tile_j,
102 	uint32_t flags);
103 
104 void pthreadpool_parallelize_3d_tile_2d(
105 	pthreadpool_t threadpool,
106 	pthreadpool_task_3d_tile_2d_t function,
107 	void* argument,
108 	size_t range_i,
109 	size_t range_j,
110 	size_t range_k,
111 	size_t tile_j,
112 	size_t tile_k,
113 	uint32_t flags);
114 
115 void pthreadpool_parallelize_4d_tile_2d(
116 	pthreadpool_t threadpool,
117 	pthreadpool_task_4d_tile_2d_t function,
118 	void* argument,
119 	size_t range_i,
120 	size_t range_j,
121 	size_t range_k,
122 	size_t range_l,
123 	size_t tile_k,
124 	size_t tile_l,
125 	uint32_t flags);
126 
127 void pthreadpool_parallelize_5d_tile_2d(
128 	pthreadpool_t threadpool,
129 	pthreadpool_task_5d_tile_2d_t function,
130 	void* argument,
131 	size_t range_i,
132 	size_t range_j,
133 	size_t range_k,
134 	size_t range_l,
135 	size_t range_m,
136 	size_t tile_l,
137 	size_t tile_m,
138 	uint32_t flags);
139 
140 void pthreadpool_parallelize_6d_tile_2d(
141 	pthreadpool_t threadpool,
142 	pthreadpool_task_6d_tile_2d_t function,
143 	void* argument,
144 	size_t range_i,
145 	size_t range_j,
146 	size_t range_k,
147 	size_t range_l,
148 	size_t range_m,
149 	size_t range_n,
150 	size_t tile_m,
151 	size_t tile_n,
152 	uint32_t flags);
153 
154 /**
155  * Terminates threads in the thread pool and releases associated resources.
156  *
157  * @warning  Accessing the thread pool after a call to this function constitutes
158  *    undefined behaviour and may cause data corruption.
159  *
160  * @param[in,out]  threadpool  The thread pool to destroy.
161  */
162 void pthreadpool_destroy(pthreadpool_t threadpool);
163 
164 
165 #ifndef PTHREADPOOL_NO_DEPRECATED_API
166 
167 /* Legacy API for compatibility with pre-existing users (e.g. NNPACK) */
168 #if defined(__GNUC__)
169 	#define PTHREADPOOL_DEPRECATED __attribute__((__deprecated__))
170 #else
171 	#define PTHREADPOOL_DEPRECATED
172 #endif
173 
174 typedef void (*pthreadpool_function_1d_t)(void*, size_t) PTHREADPOOL_DEPRECATED;
175 typedef void (*pthreadpool_function_1d_tiled_t)(void*, size_t, size_t) PTHREADPOOL_DEPRECATED;
176 typedef void (*pthreadpool_function_2d_t)(void*, size_t, size_t) PTHREADPOOL_DEPRECATED;
177 typedef void (*pthreadpool_function_2d_tiled_t)(void*, size_t, size_t, size_t, size_t) PTHREADPOOL_DEPRECATED;
178 typedef void (*pthreadpool_function_3d_tiled_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t) PTHREADPOOL_DEPRECATED;
179 typedef void (*pthreadpool_function_4d_tiled_t)(void*, size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t) PTHREADPOOL_DEPRECATED;
180 
181 void pthreadpool_compute_1d(
182 	pthreadpool_t threadpool,
183 	pthreadpool_function_1d_t function,
184 	void* argument,
185 	size_t range) PTHREADPOOL_DEPRECATED;
186 
187 void pthreadpool_compute_1d_tiled(
188 	pthreadpool_t threadpool,
189 	pthreadpool_function_1d_tiled_t function,
190 	void* argument,
191 	size_t range,
192 	size_t tile) PTHREADPOOL_DEPRECATED;
193 
194 void pthreadpool_compute_2d(
195 	pthreadpool_t threadpool,
196 	pthreadpool_function_2d_t function,
197 	void* argument,
198 	size_t range_i,
199 	size_t range_j) PTHREADPOOL_DEPRECATED;
200 
201 void pthreadpool_compute_2d_tiled(
202 	pthreadpool_t threadpool,
203 	pthreadpool_function_2d_tiled_t function,
204 	void* argument,
205 	size_t range_i,
206 	size_t range_j,
207 	size_t tile_i,
208 	size_t tile_j) PTHREADPOOL_DEPRECATED;
209 
210 void pthreadpool_compute_3d_tiled(
211 	pthreadpool_t threadpool,
212 	pthreadpool_function_3d_tiled_t function,
213 	void* argument,
214 	size_t range_i,
215 	size_t range_j,
216 	size_t range_k,
217 	size_t tile_i,
218 	size_t tile_j,
219 	size_t tile_k) PTHREADPOOL_DEPRECATED;
220 
221 void pthreadpool_compute_4d_tiled(
222 	pthreadpool_t threadpool,
223 	pthreadpool_function_4d_tiled_t function,
224 	void* argument,
225 	size_t range_i,
226 	size_t range_j,
227 	size_t range_k,
228 	size_t range_l,
229 	size_t tile_i,
230 	size_t tile_j,
231 	size_t tile_k,
232 	size_t tile_l) PTHREADPOOL_DEPRECATED;
233 
234 #endif /* PTHREADPOOL_NO_DEPRECATED_API */
235 
236 #ifdef __cplusplus
237 } /* extern "C" */
238 #endif
239 
240 #endif /* PTHREADPOOL_H_ */
241