MPSolve  3.2.1
threading.h
Go to the documentation of this file.
1 /*
2  * This file is part of MPSolve 3.2.1
3  *
4  * Copyright (C) 2001-2020, Dipartimento di Matematica "L. Tonelli", Pisa.
5  * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
6  *
7  * Authors:
8  * Leonardo Robol <leonardo.robol@unipi.it>
9  */
10 
16 #include <pthread.h>
17 #include <semaphore.h>
18 #include <mps/mps.h>
19 
20 MPS_BEGIN_DECLS
21 
22 #ifndef MPS_THREADING_H_
23 #define MPS_THREADING_H_
24 
25 #define MPS_THREAD_JOB_EXCEP -1
26 #define MPS_MAX_CORES 8192
27 
28 #define mps_with_lock(pmutex, code) { \
29  pthread_mutex_lock (&pmutex); \
30  code \
31  pthread_mutex_unlock (&pmutex); \
32 }
33 
37 typedef void * (*mps_thread_work)(void *);
38 
48  int i;
49 
53  int iter;
54 
60 };
61 
62 
81  unsigned int max_iter;
82 
87  unsigned int n_roots;
88 
92  int iter;
93 
98 
104 
109  pthread_mutex_t mutex;
110 };
111 
121  volatile int *nzeros;
122 
128 
133  volatile int *it;
134 
139 
143  int thread;
144 
149 
157  volatile mps_boolean *excep;
158 
167  pthread_mutex_t *aberth_mutex;
168 
173  pthread_mutex_t *global_aberth_mutex;
174 
180  pthread_mutex_t *roots_mutex;
181 
186  pthread_mutex_t *gs_mutex;
187 
193 };
194 
198 struct mps_thread {
203 
207  pthread_t * thread;
208 
214 
220 
224  mps_boolean busy;
225 
231  pthread_mutex_t busy_mutex;
232 
239  pthread_cond_t start_condition;
240 
248  mps_boolean alive;
249 
254 
258  void * args;
259 };
260 
270 
274  void * args;
275 
280 };
281 
291 
297 };
298 
307  unsigned int n;
308 
318  unsigned int concurrency_limit;
319 
324 
329 
333  pthread_mutex_t queue_changed_mutex;
334 
338  pthread_cond_t queue_changed;
339 
340  pthread_mutex_t work_completed_mutex;
341  pthread_cond_t work_completed_cond;
342  int busy_counter;
343 
353  mps_boolean strict_async;
354 };
355 
356 /* EXPORTED ROUTINES */
357 
358 void * mps_thread_mainloop (void * thread_ptr);
359 
361 
363 
364 void mps_thread_free (mps_context * s, mps_thread * thread);
365 
367  unsigned int concurrency_limit);
368 
369 void mps_thread_pool_assign (mps_context * s, mps_thread_pool * pool, mps_thread_work work, void * args);
370 
372 
374 
376 
377 void mps_thread_pool_set_strict_async (mps_thread_pool * pool, mps_boolean strict_async);
378 
379 mps_thread_pool * mps_thread_pool_new (mps_context * s, int n_threads);
380 
382 
384 
385 void mps_thread_job_queue_free (mps_thread_job_queue * q);
386 
388 
389 void mps_thread_fpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);
390 
391 void mps_thread_mpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);
392 
393 void mps_thread_dpolzer (mps_context * s, int *nit, mps_boolean * excep, int required_zeros);
394 
396 
397 int mps_thread_get_id (mps_context * s, mps_thread_pool * pool);
398 
399 /* MACROS */
400 
405 #define mps_thread_get_spar2(s, n_thread) (s->spar2 + (s->deg + 2) * (n_thread))
406 
411 #define mps_thread_get_mfpc2(s, n_thread) (s->mfpc2 + (s->deg + 1) * (n_thread))
412 
417 #define mps_thread_get_dap2(s, n_thread) (s->dap2 + (s->deg + 2) * (n_thread))
418 
419 MPS_END_DECLS
420 
421 #endif /* MPS_THREADING_H_ */
mps_thread_worker_data::queue
mps_thread_job_queue * queue
Pointer to the mps_thread_job_queue that the thread may query for other work.
Definition: threading.h:192
mps.h
Header file for libmps.
mps_thread_worker_data::thread
int thread
The index of this thread.
Definition: threading.h:143
mps_thread_pool_queue::last
mps_thread_pool_queue_item * last
Pointer to the last item of the queue, or NULL if the queue is empty.
Definition: threading.h:296
mps_cluster_item
Cluster held in a mps_clusterization.
Definition: cluster.h:72
mps_thread_worker_data::n_threads
int n_threads
The total number of threads.
Definition: threading.h:148
mps_thread_job_queue::cluster_item
mps_cluster_item * cluster_item
Element of s->clusterization that we are iterating on.
Definition: threading.h:103
mps_thread_job::iter
int iter
The iteration that will be performed on this root.
Definition: threading.h:53
mps_thread::data
mps_thread_worker_data * data
The data assigned to this thread, that sets the worker that he has to do.
Definition: threading.h:219
mps_thread_pool_insert_new_thread
void mps_thread_pool_insert_new_thread(mps_context *s, mps_thread_pool *pool)
Create a new thread and add it to the specified thread pool.
Definition: threading.c:410
mps_thread_dpolzer
void mps_thread_dpolzer(mps_context *s, int *nit, mps_boolean *excep, int required_zeros)
Multithread version of mps_dpolzer ().
Definition: monomial-threading.c:300
mps_root
This struct represent a root inside of a mps_cluster.
Definition: cluster.h:30
mps_thread_pool_get_system_pool
mps_thread_pool * mps_thread_pool_get_system_pool(mps_context *s)
Obtain a pointer to the default shared thread pool on this system.
Definition: threading.c:427
mps_thread_pool_queue::first
mps_thread_pool_queue_item * first
Pointer to the first item of the queue, or NULL if the queue is empty.
Definition: threading.h:290
mps_thread_job_queue::root
mps_root * root
Next root to iterate on.
Definition: threading.h:97
mps_thread_pool_wait
void mps_thread_pool_wait(mps_context *s, mps_thread_pool *pool)
Wait for a thread pool to complete its jobs.
Definition: threading.c:339
mps_thread::pool
mps_thread_pool * pool
Pool of which this thread is part.
Definition: threading.h:202
mps_context
this struct holds the state of the mps computation
Definition: context.h:55
mps_thread::busy_mutex
pthread_mutex_t busy_mutex
Busy mutex of the thread. This is locked when the thread is doing something, se we can emulate a join...
Definition: threading.h:231
mps_thread_pool::queue_changed_mutex
pthread_mutex_t queue_changed_mutex
Mutex associated to the queue_changed condition.
Definition: threading.h:333
mps_thread_pool::concurrency_limit
unsigned int concurrency_limit
Limit to the maximum spawnable number of threads. This can be set to 0 that means "No limit"....
Definition: threading.h:318
mps_thread_job_queue_next
mps_thread_job mps_thread_job_queue_next(mps_context *s, mps_thread_job_queue *q)
Obtain iter and i for the next available job.
Definition: threading.c:134
mps_thread_worker_data
Data packed to be passed to a new thread that will perform floating point, dpe or multiprecision iter...
Definition: threading.h:116
mps_thread_job::cluster_item
mps_cluster_item * cluster_item
cluster_item The cluster element of s->clusterization that we are iterating on.
Definition: threading.h:59
mps_thread_free
void mps_thread_free(mps_context *s, mps_thread *thread)
Free a thread asking it to stop.
Definition: threading.c:388
mps_thread_job
A new job for mps_thread_fsolve(), mps_thread_dsolve() or mps_thread_msolve().
Definition: threading.h:44
mps_thread::alive
mps_boolean alive
A boolean value that is true if the thread must continue to poll, or false if it is required to exit....
Definition: threading.h:248
mps_thread_pool_new
mps_thread_pool * mps_thread_pool_new(mps_context *s, int n_threads)
Allocate a new thread pool and return a pointer to it, with a number of threads suitable for this sys...
Definition: threading.c:458
mps_thread_job_queue::max_iter
unsigned int max_iter
Maximum number of iteration to perform before raising an exeption.
Definition: threading.h:81
mps_thread_pool_queue_item::next
mps_thread_pool_queue_item * next
The next item in the queue.
Definition: threading.h:279
mps_thread_fpolzer
void mps_thread_fpolzer(mps_context *s, int *nit, mps_boolean *excep, int required_zeros)
Drop-in replacement for the stock fpolzer routine. This version adds multithread support.
Definition: monomial-threading.c:134
mps_thread_pool::strict_async
mps_boolean strict_async
When this vaulue is set to true every call to mps_assign_job returns immediately.
Definition: threading.h:353
mps_thread_pool::queue_changed
pthread_cond_t queue_changed
Condition that is notified when the queue changes.
Definition: threading.h:338
mps_thread_worker_data::aberth_mutex
pthread_mutex_t * aberth_mutex
Array of n mutexes where n = s->n, i.e. is the total number of roots of the polynomial.
Definition: threading.h:167
mps_thread_new
mps_thread * mps_thread_new(mps_context *s, mps_thread_pool *pool)
Allocate a new mps_thread and start its mainloop.
Definition: threading.c:361
mps_thread_job::i
int i
The index if the root to iterate on.
Definition: threading.h:48
mps_thread_pool::first
mps_thread * first
A pointer to the first thread in the thread pool.
Definition: threading.h:323
mps_thread_work
void *(* mps_thread_work)(void *)
A generic routine that can be performed by a mps_thread.
Definition: threading.h:37
mps_thread_worker_data::it
volatile int * it
Pointer to the integer that holds the number of iterations performed until now.
Definition: threading.h:133
mps_thread_get_core_number
int mps_thread_get_core_number(mps_context *s)
Get number of logic cores on the local machine, or 0 if that information is not available with the me...
Definition: threading.c:49
mps_thread_pool_free
void mps_thread_pool_free(mps_context *s, mps_thread_pool *pool)
Free a thread pool and all its threads, waiting for them to terminate.
Definition: threading.c:497
mps_thread_mpolzer
void mps_thread_mpolzer(mps_context *s, int *nit, mps_boolean *excep, int required_zeros)
Drop-in threaded replacement for the stock mpolzer.
Definition: monomial-threading.c:526
mps_thread_pool::queue
mps_thread_pool_queue * queue
Queue of the work that shall be consumed by the threads.
Definition: threading.h:328
mps_thread_pool::n
unsigned int n
The numer of thread in the thread pool.
Definition: threading.h:307
mps_thread_worker_data::roots_mutex
pthread_mutex_t * roots_mutex
Array of n mutexes that gets locked when a thread start to iterate over a root. This is done to ensur...
Definition: threading.h:180
mps_thread_pool_queue_item::work
mps_thread_work work
The actual job that should be performed.
Definition: threading.h:269
mps_thread_pool_queue_item
An item that can be inserted and/or extracted from a mps_thread_pool_queue.
Definition: threading.h:265
mps_thread_worker_data::required_zeros
int required_zeros
The number of well approximated roots required to stop iteration packet.
Definition: threading.h:127
mps_thread
A thread that is part of a thread pool.
Definition: threading.h:198
mps_thread_job_queue::n_roots
unsigned int n_roots
Number of the roots of this problem (i.e. degree of the polynomial).
Definition: threading.h:87
mps_thread_job_queue::mutex
pthread_mutex_t mutex
Internal mutex of the queue used to guarantee exclusive access.
Definition: threading.h:109
mps_thread_start_mainloop
void mps_thread_start_mainloop(mps_context *s, mps_thread *thread)
Start the thread mainloop.
Definition: threading.c:251
mps_thread_pool_queue_item::args
void * args
The args that shall be passed to the work function.
Definition: threading.h:274
mps_thread::start_condition
pthread_cond_t start_condition
Condition that allow the thread to run. Before the thread finish the busy state (unlocking the busy m...
Definition: threading.h:239
mps_thread_pool_set_concurrency_limit
void mps_thread_pool_set_concurrency_limit(mps_context *s, mps_thread_pool *pool, unsigned int concurrency_limit)
Limit the maximum number of threads that can be used in the thread pool.
Definition: threading.c:259
mps_thread_job_queue_new
mps_thread_job_queue * mps_thread_job_queue_new(mps_context *s)
Create a new mps_thread_job_queue that can handle at most max_iter iterations for n_roots roots.
Definition: threading.c:101
mps_thread::busy
mps_boolean busy
True if the thread is busy.
Definition: threading.h:224
mps_thread_job_queue::iter
int iter
Iterations that is being performed right now.
Definition: threading.h:92
mps_thread_worker_data::nzeros
volatile int * nzeros
Pointer to the integer that holds the number of zeros computed until now.
Definition: threading.h:121
mps_thread::thread
pthread_t * thread
The pthread_t assigned to the worked.
Definition: threading.h:207
mps_thread_worker_data::s
mps_context * s
The pointer to the mps_context struct.
Definition: threading.h:138
mps_thread::work
mps_thread_work work
The routine that must be called when the thread starts.
Definition: threading.h:253
mps_thread::next
mps_thread * next
The next thread in the pool, or NULL if this is the last thread contained in it.
Definition: threading.h:213
mps_thread::args
void * args
The argument to be passed to the thread.
Definition: threading.h:258
mps_thread_worker_data::global_aberth_mutex
pthread_mutex_t * global_aberth_mutex
Global aberth mutex used to coordinate all aberth computations.
Definition: threading.h:173
mps_thread_worker_data::gs_mutex
pthread_mutex_t * gs_mutex
Global state mute used to synchronize some (hopefully not so many) global operation.
Definition: threading.h:186
mps_thread_pool_set_strict_async
void mps_thread_pool_set_strict_async(mps_thread_pool *pool, mps_boolean strict_async)
Set the value of the internal field strict_async of the pool.
Definition: threading.c:448
mps_thread_pool
A thread pool that contains a set of mps_thread and allow to manage them as a set of worker.
Definition: threading.h:303
mps_thread_pool_queue
A queue of work items that thread can consume.
Definition: threading.h:285
mps_thread_job_queue
Struct holding a job queue.
Definition: threading.h:76
mps_thread_worker_data::excep
volatile mps_boolean * excep
Pointer to the boolean excep value. Setting this to true cause the iteration to enter exception state...
Definition: threading.h:157