Click here to Skip to main content
14,326,246 members
Rate this:
Please Sign up or sign in to vote.
See more:

If i am using MPI_Send, I am not getting any problem but when I am using both MPI_Send and MPI_Recv my program blocks.
#include <iostream>
#include <fstream>
#include <cmath>
#include <mpi.h>
#include <ctime>
#include <vector>

//using namespace std;

// Sorts the input row into chunks to be scattered two all the processors.
//void sortByProcess(std::vector<double> list1, double* list2, int count);

// Swaps two rows.
//void swap(double** list, int count, int row1, int row2);

//int N=32;
int rank, size;
int tag = 99;
int main(int argc, char * argv[])
  double sTime, eTime, rTime;
  std::ifstream inFile;
  int num_rows = 3200;
  int num_cols = 3200;
  int cur_control = 0;
  double * send_buffer = NULL;
  double * recv_buffer = NULL;
  double ** data = NULL;
  double determinant;
  MPI_Status stat;
  std::vector<double> file_buffer;
  // Just get the initialization of the program going.
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  // If the input file is not given, print message and exit.
  if(argc < 2)
    std::cout << "No input file given." << std::endl;
    return 0;
  // If the root node (0), then open the input file and read in the
  // number of rows.
    inFile >> num_rows;

  send_buffer = new double[num_rows];
  // Broadcasts p2p the number of rows to each processor.
  //MPI_Bcast (&num_rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
  if(!rank) {
  for(int i=1; i<size; ++i) {
     MPI_Send(&num_rows, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
  for(int i=1; i<size; ++i) {
     MPI_Recv(&num_rows, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &stat);
  num_cols = num_rows / size;

  delete [] send_buffer;
  return 0;

Some body please guide me how to correct this error?


What I have tried:

I have checked the syntax of MPI_recv(...). i can't understand any reason for blocking.

MPI_Recv(...) is a blocking call but it must get the data.
Updated 2-May-19 7:51am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Have you tried inserting a MPI_Barrier(MPI_COMM_WORLD); call in there?

See this Q & A[^]
Rate this:
Please Sign up or sign in to vote.

Solution 2

Sending and Receiving are concurrent tasks and so have always some side effects and they need some time (in milliseconds). You need to syncronize receive and send calls.

The first problem I see in your code is that you dont check the return codes of the functions.
zak100 2-May-19 13:54pm
I tried the following program. It does not have any Barrier but its working fine:
#include <mpi.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

int main(int argc, char **argv)
char idstr[100];
char buff[128];
int numprocs;
int myid;
int i;
MPI_Status stat;
Begin program
MPI_Init(&argc,&argv); // Initialize
MPI_Comm_size(MPI_COMM_WORLD, &numprocs); // Get # processors
MPI_Comm_rank(MPI_COMM_WORLD, &myid); // Get my rank (id)
if( myid == 0 )
{ // Master
printf("WE have %d processors\n", numprocs);
for( i = 1; i < numprocs; i++)
sprintf(buff, "Hello %d", i);
MPI_Send(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD);
for( i = 1; i < numprocs; i++)
MPI_Recv(buff, 128, MPI_CHAR, i, 0, MPI_COMM_WORLD, &stat);
printf("%s\n", buff);
{ // Slave
MPI_Recv(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &stat);
sprintf(idstr, "Hello 0, Processor %d is present and accounted for !",myid);
MPI_Send(buff, 128, MPI_CHAR, 0, 0, MPI_COMM_WORLD);

$ mpicc -o SR sendRec.c
$ mpirun -np 4 ./SR
WE have 4 processors
Hello 1
Hello 2
Hello 3
zak100 2-May-19 13:55pm
Is else part necessary?


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100