>

COSC330/530 Parallel and Distributed Computing

Lecture 14 - Interprocess Communication with MPI

Dr. Mitchell Welch


Reading


Summary


MPI IPC Functions


MPI IPC Functions


MPI IPC Functions

#include "mpi.h"
int MPI_Send(void *buf, 
    int count,
    MPI_Datatype datatype,
    int dest,
    int tag,
    MPI_Comm comm )

Performs a basic message send.This routine may block until the message is received. * buf is the address of send buffer. * count is the number of elements in send buffer. * datatype is the datatype of each send buffer element * dest is the rank of destination * tag is the message tag. * comm is the communicator.


MPI IPC Functions


MPI IPC Functions

#include "mpi.h"
int MPI_Recv(void *buf, 
              int count, 
     MPI_Datatype datatype, 
              int source, 
              int tag, 
         MPI_Comm comm, 
       MPI_Status *status )

MPI IPC Functions


MPI IPC Functions

MPI Type Description
MPI_CHAR corresponds to C's signed char
MPI_SHORT corresponds to C's signed short
MPI_INT corresponds to C's signed int
MPI_LONG corresponds to C's signed long int
MPI_FLOAT corresponds to C's float
MPI_DOUBLE corresponds to C's double

MPI IPC Functions

#include "mpi.h"
int MPI_Get_count(MPI_Status *status, 
                  MPI_Datatype datatype, 
                  int *count)

MPI IPC Functions


#include <stdio.h> #include "mpi.h" #define TOKTAG 5 #define VERBOSE 1 int main(int argc, char** argv) { int me, nproc, src, dst, token; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &me); MPI_Comm_size(MPI_COMM_WORLD, &nproc); src = (me == nproc - 1) ? 0 : me + 1; dst = (me == 0) ? nproc - 1 : me - 1; ....

MPI IPC Functions

  ....

  if (me == 0) {
    token = 0;
    fprintf(stderr, "Token = %i  sent\n", token);
    MPI_Send(&token, 1, MPI_INT, dst, TOKTAG, MPI_COMM_WORLD);
    MPI_Recv(&token, 1, MPI_INT, src, TOKTAG, MPI_COMM_WORLD, &status);
    fprintf(stderr, "Token = %i  arrived back\n", token);
  } else {
    MPI_Recv(&token, 1, MPI_INT, src, TOKTAG, MPI_COMM_WORLD, &status);
    if(VERBOSE)fprintf(stderr, "Node %d passing token = %i\n", me, token);
    token++;
    MPI_Send(&token, 1, MPI_INT, dst, TOKTAG, MPI_COMM_WORLD);
  }
  MPI_Finalize();
  return 0;
}

Dealing with I/O


Dealing with I/O


Dealing with I/O

void get_input( int my_rank, int comm_sz, double∗ a_p, double∗ b_p, int∗ np){
    int dest;
    if(myrank==0){
        printf("Enter a,b and n\n");
        scanf("%lf %lf %d", a_p, b_p, n_p);
        for (dest = 1; dest < comm sz; dest++) {
            MPI Send(a_p, 1, MPI DOUBLE, dest, 0, MPI COMM WORLD); 
            MPI Send(b_p, 1, MPI DOUBLE, dest, 0, MPI COMM WORLD); 
            MPI Send(n_p, 1, MPI INT, dest, 0, MPI COMM WORLD);
        }
    } else { /*my rank != 0*/
        MPI Recv(a_p, 1, MPI DOUBLE, 0, 0, MPI COMM WORLD, MPI STATUS IGNORE);
        MPI Recv(b_p, 1, MPI DOUBLE, 0, 0, MPI COMM WORLD, MPI STATUS IGNORE);
        MPI Recv(n_p, 1, MPI INT, 0, 0, MPI COMM WORLD, MPI STATUS IGNORE);
    }
}



Advanced MPI


Advanced MPI


Advanced MPI


Advanced MPI

#include "mpi.h"
int MPI_Barrier(MPI_Comm comm)

Integral Estimation - The Trapezoidal rule


intergral

for some fixed f


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


intergral


Integral Estimation - The Trapezoidal rule


intergral

Integral Estimation - The Trapezoidal rule


integral


integral


integral


Integral Estimation - The Trapezoidal rule


integral

float f(float x){
    return x*x;
}
float Trap(float a, float b, int n, float h){ 
    float integral, x;
    int i; 
    integral = (f(a) + f(b))/2.0; 
    x = a; 
    for (i = 1; i <= n-1; i++) { 
        x += h; 
        integral += f(x); } 
    return integral * h;
}


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule

#include <stdio.h>
#include "trap.h"
int main(int argc, char** argv) {
  float h, a = 0.0, b = 1.0, integral = 0, local_a, local_b;    
  int i, n = 1000, nproc = 10, local_n = n/nproc;
  h = (b-a)/n;
  for(i = 0; i < nproc; i++){
    local_a = a + i*local_n*h;
    local_b = a + (i + 1)*local_n*h;
    integral += Trap(local_a, local_b, local_n, h);
  }
  printf("With n = %d trapezoids\n", n);
  printf("The integral from %f to %f is approx %f\n", 
         a, b, integral); 
  return 0;
}


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule

#include "mpi.h"
int MPI_Bcast(void*         buffer, 
              int           count,  
              MPI_Datatype  datatyp, 
              int           root, 
              MPI_Comm      comm)

Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule

#include "mpi.h"
int MPI_Reduce (void*        sndbuf, 
                void*        rcvbuf, 
                int          count, 
                MPI_Datatype datatyp, 
                MPI_Op       op, 
                int          root, 
                MPI_Comm     comm)

Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Integral Estimation - The Trapezoidal rule


Summary


Reading