5: #include <petsc-private/threadcommimpl.h>
7: #if defined(PETSC_HAVE_PTHREAD_H)
8: #include <pthread.h>
9: #elif defined(PETSC_HAVE_WINPTHREADS_H)
10: #include "winpthreads.h" /* http://locklessinc.com/downloads/winpthreads.h */
11: #endif
13: /*
14: PetscPThreadCommSynchronizationType - Type of thread synchronization for pthreads communicator.
16: $ PTHREADSYNC_LOCKFREE - A lock-free variant.
18: */
19: typedef enum {PTHREADSYNC_LOCKFREE} PetscPThreadCommSynchronizationType;
20: extern const char *const PetscPThreadCommSynchronizationTypes[];
22: /*
23: PetscPThreadCommAffinityPolicy - Core affinity policy for pthreads
25: $ PTHREADAFFPOLICY_ALL - threads can run on any core. OS decides thread scheduling
26: $ PTHREADAFFPOLICY_ONECORE - threads can run on only one core.
27: $ PTHREADAFFPOLICY_NONE - No set affinity policy. OS decides thread scheduling
28: */
29: typedef enum {PTHREADAFFPOLICY_ALL,PTHREADAFFPOLICY_ONECORE,PTHREADAFFPOLICY_NONE} PetscPThreadCommAffinityPolicyType;
30: extern const char *const PetscPTheadCommAffinityPolicyTypes[];
32: typedef enum {PTHREADPOOLSPARK_SELF} PetscPThreadCommPoolSparkType;
33: extern const char *const PetscPThreadCommPoolSparkTypes[];
35: /*
36: PetscThreadComm_PThread - The main data structure to manage the thread
37: communicator using pthreads. This data structure is shared by NONTHREADED
38: and PTHREAD threading models. For NONTHREADED threading model, no extra
39: pthreads are created
40: */
41: struct _p_PetscThreadComm_PThread {
42: PetscInt nthreads; /* Number of threads created */
43: pthread_t *tid; /* thread ids */
44: pthread_attr_t *attr; /* thread attributes */
45: #if defined(PETSC_HAVE_SCHED_CPU_SET_T)
46: cpu_set_t *cpuset;
47: #endif
48: PetscBool ismainworker; /* Is the main thread also a work thread?*/
49: PetscInt *granks; /* Thread ranks - if main thread is a worker then main thread
50: rank is 0 and ranks for other threads start from 1,
51: otherwise the thread ranks start from 0.
52: These ranks are with respect to the first initialized thread pool */
53: PetscInt thread_num_start; /* index for the first created thread (= 1 if the main thread is a worker
54: else 0) */
55: PetscPThreadCommSynchronizationType sync; /* Synchronization type */
56: PetscPThreadCommAffinityPolicyType aff; /* affinity policy */
57: PetscPThreadCommPoolSparkType spark; /* Type for sparking threads */
58: PetscBool synchronizeafter; /* Whether the main thread should be blocked till all threads complete the given kernel */
60: PetscErrorCode (*initialize)(PetscThreadComm);
61: PetscErrorCode (*finalize)(PetscThreadComm);
62: };
64: typedef struct _p_PetscThreadComm_PThread *PetscThreadComm_PThread;
66: #if defined(PETSC_PTHREAD_LOCAL)
67: extern PETSC_PTHREAD_LOCAL PetscInt PetscPThreadRank; /* Rank of the calling thread ... thread local variable */
68: #else
69: extern pthread_key_t PetscPThreadRankkey;
70: #endif
72: #if defined(PETSC_CPU_RELAX)
73: #define PetscCPURelax() do {PETSC_CPU_RELAX();} while (0) 74: #else
75: #define PetscCPURelax() do { } while (0) 76: #endif
78: PETSC_EXTERN PetscErrorCode PetscThreadCommCreate_PThread(PetscThreadComm);
80: extern PetscErrorCode PetscPThreadCommInitialize_LockFree(PetscThreadComm);
81: extern PetscErrorCode PetscPThreadCommFinalize_LockFree(PetscThreadComm);
82: extern PetscErrorCode PetscThreadCommRunKernel_PThread_LockFree(PetscThreadComm,PetscThreadCommJobCtx);
83: extern PetscErrorCode PetscThreadCommBarrier_PThread_LockFree(PetscThreadComm);
85: #if defined(PETSC_HAVE_SCHED_CPU_SET_T)
86: extern void PetscPThreadCommDoCoreAffinity();
87: #endif
90: #endif