2: #ifndef __PTHREADIMPLH
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_LEADER,PTHREADPOOLSPARK_CHAIN} 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: PetscBool ismainworker; /* Is the main thread also a work thread?*/
45: PetscInt *granks; /* Thread ranks - if main thread is a worker then main thread
46: rank is 0 and ranks for other threads start from 1,
47: otherwise the thread ranks start from 0.
48: These ranks are with respect to the first initialized thread pool */
49: PetscInt thread_num_start; /* index for the first created thread (= 1 if the main thread is a worker
50: else 0) */
51: PetscInt *ngranks; /* Stores the rank of the next thread to be sparked by this thread
52: Only used for CHAIN PoolSpark type */
53: PetscPThreadCommSynchronizationType sync; /* Synchronization type */
54: PetscPThreadCommAffinityPolicyType aff; /* affinity policy */
55: PetscPThreadCommPoolSparkType spark; /* Type for sparking threads */
56: PetscBool synchronizeafter; /* Whether the main thread should be blocked till all threads complete the given kernel */
57: PetscErrorCode (*initialize)(PetscThreadComm);
58: PetscErrorCode (*finalize)(PetscThreadComm);
59: };
61: typedef struct _p_PetscThreadComm_PThread *PetscThreadComm_PThread;
63: #if defined(PETSC_PTHREAD_LOCAL)
64: extern PETSC_PTHREAD_LOCAL PetscInt PetscPThreadRank; /* Rank of the calling thread ... thread local variable */
65: #else
66: extern pthread_key_t PetscPThreadRankkey;
67: #endif
70: EXTERN_C_BEGIN 71: extern PetscErrorCode PetscThreadCommCreate_PThread(PetscThreadComm);
72: EXTERN_C_END 74: extern PetscErrorCode PetscPThreadCommInitialize_LockFree(PetscThreadComm);
75: extern PetscErrorCode PetscPThreadCommFinalize_LockFree(PetscThreadComm);
76: extern PetscErrorCode PetscThreadCommRunKernel_PThread_LockFree(MPI_Comm,PetscThreadCommJobCtx);
77: extern PetscErrorCode PetscThreadCommBarrier_PThread_LockFree(PetscThreadComm);
79: #if defined(PETSC_HAVE_SCHED_CPU_SET_T)
80: extern void PetscPThreadCommDoCoreAffinity();
81: #endif
84: #endif