+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1initbc9a89f

master
1.4 KiB41 linesraw
1// Author: Wes Kendall
2// Copyright 2011 www.mpitutorial.com
3// This code is provided freely with the tutorials on mpitutorial.com. Feel
4// free to modify it for your own use. Any distribution of the code must
5// either provide a link to www.mpitutorial.com or keep this header intact.
6//
7// MPI_Send, MPI_Recv example. Communicates the number -1 from process 0
8// to process 1.
9//
10#include <mpi.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14int main(int argc, char** argv) {
15  // Initialize the MPI environment
16  MPI_Init(NULL, NULL);
17  // Find out rank, size
18  int world_rank;
19  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
20  int world_size;
21  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
22
23  // We are assuming at least 2 processes for this task
24  if (world_size < 2) {
25    fprintf(stderr, "World size must be greater than 1 for %s\n", argv[0]);
26    MPI_Abort(MPI_COMM_WORLD, 1);
27  }
28
29  int number;
30  if (world_rank == 0) {
31    // If we are rank 0, set the number to -1 and send it to process 1
32    number = -1;
33    /* MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) */
34    MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
35  } else if (world_rank == 1) {
36    /* MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status status) */
37    MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
38    printf("Process 1 received number %d from process 0\n", number);
39  }
40  MPI_Finalize();
41}