【问题标题】:how to fix this MPI code program如何修复这个 MPI 代码程序
【发布时间】:2011-09-24 04:12:01
【问题描述】:

这个程序演示了一个不安全的程序,因为有时它会执行得很好,而有时它会失败。程序失败或挂起的原因是由于接收任务端的缓冲区耗尽,这是由于 MPI 库为特定大小的消息实现了急切协议的方式。一种可能的解决方案是在发送和接收循环中都包含一个 MPI_Barrier 调用。

它的程序代码怎么正确???

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

#define MSGSIZE 2000

int main (int argc, char *argv[])
{
int        numtasks, rank, i, tag=111, dest=1, source=0, count=0;
char       data[MSGSIZE];
double     start, end, result;
MPI_Status status;

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

if (rank == 0) {
  printf ("mpi_bug5 has started...\n");
  if (numtasks > 2) 
    printf("INFO: Number of tasks= %d. Only using 2 tasks.\n", numtasks);
  }

/******************************* Send task **********************************/
if (rank == 0) {

  /* Initialize send data */
  for(i=0; i<MSGSIZE; i++)
     data[i] =  'x';

  start = MPI_Wtime();
  while (1) {
    MPI_Send(data, MSGSIZE, MPI_BYTE, dest, tag, MPI_COMM_WORLD);
    count++;
    if (count % 10 == 0) {
      end = MPI_Wtime();
      printf("Count= %d  Time= %f sec.\n", count, end-start);
      start = MPI_Wtime();
      }
    }
  }

/****************************** Receive task ********************************/

if (rank == 1) {
  while (1) {
    MPI_Recv(data, MSGSIZE, MPI_BYTE, source, tag, MPI_COMM_WORLD, &status);
    /* Do some work  - at least more than the send task */
    result = 0.0;
    for (i=0; i < 1000000; i++) 
      result = result + (double)random();
    }
  }

MPI_Finalize();
}

【问题讨论】:

  • 请正确格式化您的代码,请参阅Meta

标签: debugging mpi


【解决方案1】:

改进此代码的方法包括:

  • 同步 - 您提到了 MPI_Barrier,但即使使用 MPI_Ssend 而不是 MPI_Send 也可以。
  • 显式缓冲 - 使用 MPI_Bsend 或 Brecv 确保存在足够的缓冲。
  • 已发布接收 - 接收进程在开始工作之前发布 IRecvs,以确保将消息接收到用于保存数据的缓冲区,而不是系统缓冲区。

在这个教学案例中,由于消息的数量是无限的,因此只有第一个(同步)可以可靠地工作。

【讨论】:

  • 谢谢先生..你能给我它的程序代码行是正确的..!请帮我!谢谢关注
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2019-09-14
相关资源
最近更新 更多