MPI_recv在一个小代码中阻塞 [英] MPI_recv blocks in a small code

查看:187
本文介绍了MPI_recv在一个小代码中阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如果我正在使用MPI_Send,我没有遇到任何问题,但当我同时使用MPI_Send和MPI_Recv我的程序块时。

Hi,

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;
    MPI_Finalize();
    return 0;
  }
  // If the root node (0), then open the input file and read in the
  // number of rows.
  if(!rank)
  {
    inFile.open(argv[1]);
    inFile >> num_rows;
    file_buffer.resize(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;
  MPI_Finalize();
  return 0;



有些人请指导我如何纠正这个错误?



Zulfi。



我是什么尝试过:



我检查了MPI_recv(...)的语法。我无法理解任何阻止的原因。


MPI_Recv(...)是一个阻塞调用,但它必须获取数据。


Some body please guide me how to correct this error?

Zulfi.

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.

推荐答案

您是否尝试过插入MPI_Barrier(MPI_COMM_WORLD);在那里打电话?



这个Q& A [ ^ ]
Have you tried inserting a MPI_Barrier(MPI_COMM_WORLD); call in there?

See this Q & A[^]


发送和接收是并发任务,因此总是有一些副作用,它们需要一些时间(以毫秒为单位)。你需要同步接收和发送电话。



我在你的代码中看到的第一个问题是你不检查函数的返回码。
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.


这篇关于MPI_recv在一个小代码中阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆