c:mpi:wave-algorithm
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| c:mpi:wave-algorithm [2024/01/19 11:42] – removed - external edit (Unknown date) 127.0.0.1 | c:mpi:wave-algorithm [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== MPI Wave Algorithm ====== | ||
| + | |||
| + | Implementing a wave algorithm in MPI involves creating a pattern where each process communicates with its neighbors in a specific sequence. | ||
| + | |||
| + | This can be used to model wave-like propagation of information across the processes. | ||
| + | |||
| + | In this example, let's consider a simple scenario where each process sends a message to its next neighbor, waits to receive a message from its previous neighbor, and then sends a confirmation back. | ||
| + | |||
| + | This pattern creates a " | ||
| + | |||
| + | <code c mpi-wave-algorithm.c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | int main(int argc, char** argv) { | ||
| + | MPI_Init(& | ||
| + | |||
| + | int world_rank, world_size; | ||
| + | MPI_Comm_rank(MPI_COMM_WORLD, | ||
| + | MPI_Comm_size(MPI_COMM_WORLD, | ||
| + | |||
| + | int token; | ||
| + | |||
| + | // Create a wave of communication from the first to the last process | ||
| + | if (world_rank != 0) { | ||
| + | MPI_Recv(& | ||
| + | printf(" | ||
| + | } else { | ||
| + | // Start the wave | ||
| + | token = -1; | ||
| + | } | ||
| + | |||
| + | MPI_Barrier(MPI_COMM_WORLD); | ||
| + | |||
| + | if (world_rank != world_size - 1) { | ||
| + | token++; | ||
| + | MPI_Send(& | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | // Create a wave of confirmation back from the last to the first process | ||
| + | if (world_rank != world_size - 1) { | ||
| + | MPI_Recv(& | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | MPI_Barrier(MPI_COMM_WORLD); | ||
| + | |||
| + | if (world_rank != 0) { | ||
| + | MPI_Send(& | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | MPI_Finalize(); | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | The program starts with the first process initializing the wave by sending a token to the next process. | ||
| + | |||
| + | Each process waits to receive the token from its previous neighbor, prints a message, and then sends the token to its next neighbor. | ||
| + | |||
| + | After reaching the last process, a confirmation wave is sent back in the reverse order. | ||
| + | |||
| + | Each process in the reverse wave receives a confirmation token from its next neighbor and sends it to its previous neighbor. | ||
| + | |||
| + | **MPI_Barrier** is used to synchronize processes between the forward and reverse waves. | ||
| + | |||
| + | This implementation demonstrates a basic wave algorithm where communication flows from the first to the last process and then back. The output will show the flow of tokens and confirmations through the processes. | ||
