Untitled

 avatar
unknown
plain_text
2 years ago
975 B
4
Indexable
#include <stdio.h>
#include <mpi.h>

int main(int argc, char *argv[]) {
    int rank, size, number;

    MPI_Init(&argc, &argv); // Initialize MPI environment
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get rank of current process
    MPI_Comm_size(MPI_COMM_WORLD, &size); // Get total number of processes

    if (size != 2) {
        printf("This program requires exactly 2 processes.\n");
        MPI_Finalize(); // Finalize MPI environment
        return 0;
    }

    if (rank == 0) {
        number = 23;
        MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // Send number to process 1
        printf("Process %d sent number %d to process %d\n", rank, number, 1);
    } else if (rank == 1) {
        MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receive number from process 0
        printf("Process %d received number %d from process %d\n", rank, number, 0);
    }

    MPI_Finalize(); // Finalize MPI environment
    return 0;
}
Editor is loading...