File: threading.h

package info (click to toggle)
mpsolve 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,100 kB
  • sloc: ansic: 25,748; sh: 4,925; cpp: 3,155; makefile: 914; python: 407; yacc: 158; lex: 85; xml: 41
file content (421 lines) | stat: -rw-r--r-- 10,579 bytes parent folder | download
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
/*
 * This file is part of MPSolve 3.2.2
 *
 * Copyright (C) 2001-2020, Dipartimento di Matematica "L. Tonelli", Pisa.
 * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
 *
 * Authors:
 *   Leonardo Robol <leonardo.robol@unipi.it>
 */

/**
 * @file
 * @brief Multithreading iterations for MPSolve.
 */

#include <pthread.h>
#include <semaphore.h>
#include <mps/mps.h>

MPS_BEGIN_DECLS

#ifndef MPS_THREADING_H_
#define MPS_THREADING_H_

#define MPS_THREAD_JOB_EXCEP -1
#define MPS_MAX_CORES 8192

#define mps_with_lock(pmutex, code) { \
    pthread_mutex_lock (&pmutex); \
    code \
    pthread_mutex_unlock (&pmutex); \
}

/**
 * @brief A generic routine that can be performed by a <code>mps_thread</code>.
 */
typedef void * (*mps_thread_work)(void *);

/**
 * @brief A new job for <code>mps_thread_fsolve()</code>,
 * <code>mps_thread_dsolve()</code> or <code>mps_thread_msolve()</code>.
 *
 */
struct mps_thread_job {
  /**
   * @brief The index if the root to iterate on.
   */
  int i;

  /**
   * @brief The iteration that will be performed on this root.
   */
  int iter;

  /**
   * @brief cluster_item The cluster element of <code>s->clusterization
   * that we are iterating on.
   */
  mps_cluster_item * cluster_item;
};


/**
 * @brief Struct holding a job queue.
 *
 * This structure can be used to coordinate the work in the different
 * thread during multithread computation in MPSolve.
 *
 * It must be allocated using <code>mps_thread_job_queue_new()</code>
 * and freed with <code>mps_thread_job_queue_free()</code>.
 * A new job can be requested with the routine
 * <code>mps_thread_job_queue_next()</code>.
 *
 * @see mps_thread_job_queue_next()
 */
struct mps_thread_job_queue {
  /**
   * @brief Maximum number of iteration to perform before
   *  raising an exeption.
   */
  unsigned int max_iter;

  /**
   * @brief Number of the roots of this problem (i.e. degree of
   * the polynomial).
   */
  unsigned int n_roots;

  /**
   * @brief Iterations that is being performed right now.
   */
  int iter;

  /**
   * @brief Next root to iterate on.
   */
  mps_root * root;

  /**
   * @brief Element of <code>s->clusterization</code> that
   * we are iterating on.
   */
  mps_cluster_item * cluster_item;

  /**
   * @brief Internal mutex of the queue used to guarantee
   * exclusive access.
   */
  pthread_mutex_t mutex;
};

/**
 * @brief Data packed to be passed to a new thread that will
 * perform floating point, dpe or multiprecision iterations.
 */
struct mps_thread_worker_data {
  /**
   * @brief Pointer to the integer that holds the number of zeros
   *  computed until now.
   */
  volatile int *nzeros;

  /**
   * @brief The number of well approximated roots required to stop iteration
   * packet.
   */
  int required_zeros;

  /**
   * @brief Pointer to the integer that holds the number of iterations
   *  performed until now.
   */
  volatile int *it;

  /**
   * @brief  The pointer to the <code>mps_context</code> struct.
   */
  mps_context *s;

  /**
   * @brief The index of this thread.
   */
  int thread;

  /**
   * @brief The total number of threads.
   */
  int n_threads;

  /**
   * @brief Pointer to the boolean excep value. Setting this to true
   *  cause the iteration to enter exception state.
   *
   *  If this state is reached all threads returns because no more
   *  iteration are needed / useful.
   */
  volatile mps_boolean *excep;

  /**
   * @brief Array of <code>n</code> mutexes where <code>n = s->n</code>, i.e.
   * is the total number of roots of the polynomial.
   *
   * The mutex in position <code>i</code> gets locked when a
   * thread needs to read and/or write from/to
   * the i-th root.
   */
  pthread_mutex_t *aberth_mutex;

  /**
   * @brief Global aberth mutex used to coordinate all aberth
   * computations.
   */
  pthread_mutex_t *global_aberth_mutex;

  /**
   * @brief Array of <code>n</code> mutexes that gets locked when a thread
   * start to iterate over a root. This is done to ensure that only a thread
   * at a time is iterating over a root.
   */
  pthread_mutex_t *roots_mutex;

  /**
   * @brief Global state mute used to synchronize some (hopefully not so many)
   * global operation.
   */
  pthread_mutex_t *gs_mutex;

  /**
   * @brief Pointer to the <code>mps_thread_job_queue</code> that the thread
   * may query for other work.
   */
  mps_thread_job_queue *queue;
};

/**
 * @brief A thread that is part of a thread pool.
 */
struct mps_thread {
  /**
   * @brief Pool of which this thread is part.
   */
  mps_thread_pool * pool;

  /**
   * @brief The pthread_t assigned to the worked.
   */
  pthread_t * thread;

  /**
   * @brief The next thread in the pool, or NULL if this
   * is the last thread contained in it.
   */
  mps_thread * next;

  /**
   * @brief The data assigned to this thread, that sets
   * the worker that he has to do.
   */
  mps_thread_worker_data * data;

  /**
   * @brief True if the thread is busy.
   */
  mps_boolean busy;

  /**
   * @brief Busy mutex of the thread. This is locked when the thread
   * is doing something, se we can emulate a join on it by
   * try to lock and unlock this mutex.
   */
  pthread_mutex_t busy_mutex;

  /**
   * @brief Condition that allow the thread to run. Before the thread
   * finish the busy state (unlocking the busy mutex) or when it is
   * created, it waits for the start condition to be true before
   * doing anything.
   */
  pthread_cond_t start_condition;

  /**
   * @brief A boolean value that is true if the thread must continue to
   * poll, or false if it is required to exit. Since the thread
   * may be waiting for work a call to pthread_cond_signal on
   * start condition may be required to make it exit after setting
   * this variable.
   */
  mps_boolean alive;

  /**
   * @brief The routine that must be called when the thread starts.
   */
  mps_thread_work work;

  /**
   * @brief The argument to be passed to the thread.
   */
  void * args;
};

/**
 * @brief An item that can be inserted and/or extracted from
 * a mps_thread_pool_queue.
 */
struct mps_thread_pool_queue_item {
  /**
   * @brief The actual job that should be performed.
   */
  mps_thread_work work;

  /**
   * @brief The args that shall be passed to the work function.
   */
  void * args;

  /**
   * @brief The next item in the queue.
   */
  mps_thread_pool_queue_item * next;
};

/**
 * @brief A queue of work items that thread can consume.
 */
struct mps_thread_pool_queue {
  /**
   * @brief Pointer to the first item of the queue, or NULL if
   * the queue is empty.
   */
  mps_thread_pool_queue_item * first;

  /**
   * @brief Pointer to the last item of the queue, or NULL if
   * the queue is empty.
   */
  mps_thread_pool_queue_item * last;
};

/**
 * @brief A thread pool that contains a set of <code>mps_thread</code>
 * and allow to manage them as a set of worker.
 */
struct mps_thread_pool {
  /**
   * @brief The numer of thread in the thread pool.
   */
  unsigned int n;

  /**
   * @brief Limit to the maximum spawnable number
   * of threads. This can be set to 0 that means
   * "No limit". It is useful when less concurrency
   * is desired without deleting and recreating threads.
   *
   * This variables MUST be updated using the accessor function
   * mps_thread_pool_set_
   */
  unsigned int concurrency_limit;

  /**
   * @brief A pointer to the first thread in the thread pool.
   */
  mps_thread * first;

  /**
   * @brief Queue of the work that shall be consumed by the threads.
   */
  mps_thread_pool_queue * queue;

  /**
   * @brief Mutex associated to the queue_changed condition.
   */
  pthread_mutex_t queue_changed_mutex;

  /**
   * @brief Condition that is notified when the queue changes.
   */
  pthread_cond_t queue_changed;

  pthread_mutex_t work_completed_mutex;
  pthread_cond_t work_completed_cond;
  int busy_counter;
  
  /**
   * @brief When this vaulue is set to true every call to mps_assign_job
   * returns immediately. 
   *
   * When it is set to false the calls to mps_thread_pool_assign() when the number
   * of thread is set to 1 will immediately perform the work, instead
   * of delegating it to a background thread. This is done to ensure reasonable
   * performance for the cases where only 1 CPU is available on the PC. 
   */
  mps_boolean strict_async;
};

/* EXPORTED ROUTINES */

void * mps_thread_mainloop (void * thread_ptr);

void mps_thread_start_mainloop (mps_context * s, mps_thread * thread);

mps_thread * mps_thread_new (mps_context * s, mps_thread_pool * pool);

void mps_thread_free (mps_context * s, mps_thread * thread);

void mps_thread_pool_set_concurrency_limit (mps_context * s, mps_thread_pool * pool,
                                            unsigned int concurrency_limit);

void mps_thread_pool_assign (mps_context * s, mps_thread_pool * pool, mps_thread_work work, void * args);

void mps_thread_pool_insert_new_thread (mps_context * s, mps_thread_pool * pool);

void mps_thread_pool_wait (mps_context * s, mps_thread_pool * pool);

mps_thread_pool * mps_thread_pool_get_system_pool (mps_context * s);

void mps_thread_pool_set_strict_async (mps_thread_pool * pool, mps_boolean strict_async);

mps_thread_pool * mps_thread_pool_new (mps_context * s, int n_threads);

void mps_thread_pool_free (mps_context * s, mps_thread_pool * pool);

mps_thread_job_queue * mps_thread_job_queue_new (mps_context * s);

void mps_thread_job_queue_free (mps_thread_job_queue * q);

mps_thread_job mps_thread_job_queue_next (mps_context * s, mps_thread_job_queue * q);

void mps_thread_fpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);

void mps_thread_mpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);

void mps_thread_dpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);

int mps_thread_get_core_number (mps_context * s);

int mps_thread_get_id (mps_context * s, mps_thread_pool * pool);

/* MACROS */

/**
 * @brief Get a pointer to an array of n+2 booleans
 * that is local to the thread.
 */
#define mps_thread_get_spar2(s, n_thread) (s->spar2 + (s->deg + 2) * (n_thread))

/**
 * @brief Get a pointer to an array of n+1 multiprecision
 * that is local to the thread.
 */
#define mps_thread_get_mfpc2(s, n_thread) (s->mfpc2 + (s->deg + 1) * (n_thread))

/**
 * @brief Get a pointer to an array of n+2 DPE
 * that is local to the thread.
 */
#define mps_thread_get_dap2(s, n_thread) (s->dap2 + (s->deg + 2) * (n_thread))

MPS_END_DECLS

#endif                          /* MPS_THREADING_H_ */