Actual source code: pthreadimpl.h
petsc-3.3-p7 2013-05-11
5: #if defined(PETSC_HAVE_PTHREAD_H)
6: #include <pthread.h>
7: #elif defined(PETSC_HAVE_WINPTHREADS_H)
8: #include "winpthreads.h" /* http://locklessinc.com/downloads/winpthreads.h */
9: #endif
10: #if defined(PETSC_HAVE_SYS_SYSINFO_H)
11: #include <sys/sysinfo.h>
12: #endif
13: #if defined(PETSC_HAVE_UNISTD_H)
14: #include <unistd.h>
15: #endif
16: #if defined(PETSC_HAVE_STDLIB_H)
17: #include <stdlib.h>
18: #endif
19: #if defined(PETSC_HAVE_MALLOC_H)
20: #include <malloc.h>
21: #endif
22: #if defined(PETSC_HAVE_VALGRIND)
23: #include <valgrind/valgrind.h>
24: #endif
25: #if defined(PETSC_HAVE_SYS_SYSCTL_H)
26: #include <sys/sysctl.h>
27: #endif
29: extern PetscBool PetscThreadGo; /* Flag to keep the threads spinning in a loop */
30: extern PetscMPIInt PetscMaxThreads; /* Max. threads created */
31: extern pthread_t* PetscThreadPoint; /* Pointer to thread ids */
32: extern PetscInt* PetscThreadsCoreAffinities; /* Core affinity of threads, includes the main thread affinity
33: if the main thread is also a work thread */
34: extern PetscInt PetscMainThreadShareWork; /* Is the main thread also a worker? 1 = Yes (Default)*/
35: extern PetscInt PetscMainThreadCoreAffinity; /* Core affinity of the main thread */
36: extern PetscBool PetscThreadsInitializeCalled; /* Check whether PetscThreadsInitialize has been called */
37: #if defined(PETSC_PTHREAD_LOCAL)
38: extern PETSC_PTHREAD_LOCAL PetscInt PetscThreadRank; /* Rank of the thread ... thread local variable */
39: #else
40: extern pthread_key_t PetscThreadsRankkey;
41: #endif
42: extern PetscInt* PetscThreadRanks; /* Thread ranks - if main thread is a worker then main thread
43: rank is 0 and ranks for other threads start from 1,
44: otherwise the thread ranks start from 0 */
46: /*
47: PetscThreadsSynchronizationType - Type of thread synchronization for pthreads
49: $ THREADSYNC_NOPOOL - threads created and destroyed as and when required.
50: The following synchronization scheme uses a thread pool mechanism and uses
51: mutex and condition variables for thread synchronization.
52: $ THREADSYNC_MAINPOOL - Individual threads are woken up sequentially by main thread.
53: $ THREADSYNC_TRUEPOOL - Uses a broadcast to wake up all threads
54: $ THREADSYNC_CHAINPOOL - Uses a chain scheme for waking up threads.
55: $ THREADSYNC_TREEPOOL - Uses a tree scheme for waking up threads.
56: The following synchronization scheme uses GCC's atomic function __sync_bool_compare_and_swap
57: for thread synchronization
58: $ THREADSYNC_LOCKFREE - A lock-free variant of the MAINPOOL model.
60: Level: developer
61: */
62: typedef enum {THREADSYNC_NOPOOL,THREADSYNC_MAINPOOL,THREADSYNC_TRUEPOOL,THREADSYNC_CHAINPOOL,THREADSYNC_TREEPOOL,THREADSYNC_LOCKFREE} PetscThreadsSynchronizationType;
64: /*
65: PetscThreadsAffinityPolicyType - Core affinity policy for pthreads
67: $ THREADAFFINITYPOLICY_ALL - threads can run on any core.
68: $ THREADAFFINITYPOLICY_ONECORE - threads can run on only one core
69: $ THREADAFFINITYPOLICY_NONE - No affinity policy
70: Level: developer
71: */
72: typedef enum {THREADAFFINITYPOLICY_ALL,THREADAFFINITYPOLICY_ONECORE,THREADAFFINITYPOLICY_NONE} PetscThreadsAffinityPolicyType;
74: /* Base function pointers */
75: extern void* (*PetscThreadFunc)(void*);
76: extern PetscErrorCode (*PetscThreadsSynchronizationInitialize)(PetscInt);
77: extern PetscErrorCode (*PetscThreadsSynchronizationFinalize)(void);
78: extern void* (*PetscThreadsWait)(void*);
79: extern PetscErrorCode (*PetscThreadsRunKernel)(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
81: #if defined(PETSC_HAVE_SCHED_CPU_SET_T)
82: extern void PetscThreadsDoCoreAffinity(void);
83: #else
84: #define PetscThreadsDoCoreAffinity()
85: #endif
86: extern PetscErrorCode PetscThreadsFinish(void*);
88: extern PetscErrorCode PetscThreadsInitialize(PetscInt);
89: extern PetscErrorCode PetscThreadsFinalize(void);
91: #if 0 /* Disabling all the thread thread pools except lockfree */
92: /* Tree Thread Pool Functions */
93: extern void* PetscThreadFunc_Tree(void*);
94: extern PetscErrorCode PetscThreadsSynchronizationInitialize_Tree(PetscInt);
95: extern PetscErrorCode PetscThreadsSynchronizationFinalize_Tree(void);
96: extern void* PetscThreadsWait_Tree(void*);
97: extern PetscErrorCode PetscThreadsRunKernel_Tree(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
99: /* Main Thread Pool Functions */
100: extern void* PetscThreadFunc_Main(void*);
101: extern PetscErrorCode PetscThreadsSynchronizationInitialize_Main(PetscInt);
102: extern PetscErrorCode PetscThreadsSynchronizationFinalize_Main(void);
103: extern void* PetscThreadsWait_Main(void*);
104: extern PetscErrorCode PetscThreadsRunKernel_Main(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
106: /* Chain Thread Pool Functions */
107: extern void* PetscThreadFunc_Chain(void*);
108: extern PetscErrorCode PetscThreadsSynchronizationInitialize_Chain(PetscInt);
109: extern PetscErrorCode PetscThreadsSynchronizationFinalize_Chain(void);
110: extern void* PetscThreadsWait_Chain(void*);
111: extern PetscErrorCode PetscThreadsRunKernel_Chain(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
113: /* True Thread Pool Functions */
114: extern void* PetscThreadFunc_True(void*);
115: extern PetscErrorCode PetscThreadsSynchronizationInitialize_True(PetscInt);
116: extern PetscErrorCode PetscThreadsSynchronizationFinalize_True(void);
117: extern void* PetscThreadsWait_True(void*);
118: extern PetscErrorCode PetscThreadsRunKernel_True(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
120: /* NO Thread Pool Functions */
121: extern void* PetscThreadFunc_None(void*);
122: extern void* PetscThreadsWait_None(void*);
123: extern PetscErrorCode PetscThreadsRunKernel_None(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
124: #endif
126: /* Lock free Functions */
127: extern void* PetscThreadFunc_LockFree(void*);
128: extern PetscErrorCode PetscThreadsSynchronizationInitialize_LockFree(PetscInt);
129: extern PetscErrorCode PetscThreadsSynchronizationFinalize_LockFree(void);
130: extern void* PetscThreadsWait_LockFree(void*);
131: extern PetscErrorCode PetscThreadsRunKernel_LockFree(PetscErrorCode (*pFunc)(void*),void**,PetscInt,PetscInt*);
133: #endif