1
votes

I have implemented a cartesian topology modelling a 3x3 grid:

enter image description here

Where P0, P1 and etc stands for Process 0, Process 1 and so on. Therefore, each slot in the topology represents a process. Val represents the value that the process contains.

What I'm trying to do now is to check the sum of each process's neighbours' value to see if it sums up to 300. If it does, it outputs:

Process 1 neighbour values sums up to 300.
Process 3 neighbour values sums up to 300.
Process 5 neighbour values sums up to 300.
.
.

The neighbours are either left top bottom or right. I've written the code to simulate the above diagram:

#include <stdio.h>
#include "mpi.h"
int main(int argc, char *argv[])
{
  int value;
  int sum;
  int rank;
  int size;
  int coordinates[2];

  MPI_Comm grid_comm;

  int dim[2],false = 0,reorder;
  int top,bottom,right,left; 

  MPI_Status status;

  MPI_Init(&argc,&argv); 
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  MPI_Comm_size(MPI_COMM_WORLD,&size);

  dim[0] = 3;
  dim[1] = 3;
  reorder = 0;
  MPI_Cart_create(MPI_COMM_WORLD,2,dim,&false,reorder,&grid_comm);

  MPI_Cart_shift(grid_comm,0,1,&top,&bottom);
  MPI_Cart_shift(grid_comm,1,1,&left,&right);

  MPI_Comm_rank(grid_comm,&rank);

  MPI_Cart_coords(grid_comm,rank,2,coordinates);
  MPI_Cart_rank(grid_comm,coordinates,&rank);

  if (rank % 2 == 0) {
      value *=100;
  }
  else {
      value *=200;
  }

  MPI_Finalize();
  return 0;
}

The problem I'm facing now is on accessing the neighbours value in the cartesian grid. Would like some advice on how I'm able to do so.

1

1 Answers

1
votes

You can think of MPI communication as occurring in phases. You can do what you need with four phases. In each phase, every node sends its value to its neighbor on the {right,left,top,bottom} (unless there is no such neighbor).

One way is to use MPI_Isend, which is non-blocking. This means in the first phase, every node sends to the right then receives from the left.