Implementing POSIX Barriers and MPI Custom Data Types
Custom POSIX Barrier Implementation
This structure defines a custom barrier for thread synchronization:
typedef struct {
int total; // Expected threads
int count; // Arrived threads
int phase; // Current phase (for reuse)
pthread_mutex_t mutex;
pthread_cond_t cond;
} barrier_t;Barrier Initialization and Destruction
- Init: Sets the total thread count and initializes synchronization primitives.
- Destroy: Cleans up mutex and condition variables.
Barrier Wait Logic
The barrier_wait function ensures all threads reach the synchronization point before proceeding, utilizing a phase-based approach to allow barrier reuse.
MPI Vector Types for Chessboard Patterns
Using MPI_Type_vector to handle non-contiguous memory layouts in odd-sized matrices (N=111).
#define N 111
int rank;
int mat[N][N];The exchChess function demonstrates how to create a derived data type to exchange alternating elements between processes efficiently.
Von Neumann Neighborhood (Radius 2)
Optimized boundary exchange using a single MPI_Send/Recv operation:
- Top/Bottom Edges:
MPI_Type_vector(2, NCOLS/C_PARTITIONS, NCOLS/C_PARTITIONS+4, MPI_INT, &UpDownDataType); - Left/Right Edges:
MPI_Type_vector(NROWS/R_PARTITIONS, 2, NCOLS/C_PARTITIONS+4, MPI_INT, &LeftRightDataType);
Moore Neighborhood on 2D Grids
Defining custom data types for generic radius-based communication:
- Vertical Edges (Columns):
MPI_Type_vector(N/numPartition, radius, N + 2*radius, MPI_INT, &colDT); - Horizontal Edges (Rows):
MPI_Type_vector(radius, N + 2*radius, N + 2*radius, MPI_INT, &rowDT);
English with a size of 1.96 KB