【问题标题】:Distributing array components over processors using Open MPI with C++使用 Open MPI 和 C++ 在处理器上分发数组组件
【发布时间】:2016-07-31 18:09:23
【问题描述】:

我正在尝试编写一个简单的 C++ 程序,它接受一个数组,将其相等的部分发送到不同的处理器,这些处理器对组件进行计算,然后将数组的部分发送回主处理器进行组合在最终数组中。

我从一个简单的例子开始,我有一个大小为 2 的数组,第一个组件由进程 1 加 1。第二个组件由进程 2 加 2。

这是我所拥有的:

# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <ctime>
#include <fstream>
# include "mpi.h"

using namespace std;

ofstream debug("DEBUG");
ofstream debug1("DEBUG1");
ofstream debug2("DEBUG2");

// Declare the array
double arr[2];

int main(int argc, char *argv[])
{
 MPI::Init(argc, argv);

// Make the array
arr[0] = 1;
arr[1] = 2; 

int rank = MPI::COMM_WORLD.Get_rank();
int npes = MPI::COMM_WORLD.Get_size();

if ( rank == 0 ) {
  cout << "Running on "<< npes << " Processes "<< endl;
  double arr1;
  double arr2;
  MPI::COMM_WORLD.Recv(&arr1, 1, MPI::DOUBLE, 0, 0);
  debug << "arr1: " << arr1 << endl;

  /*... Program freezes here. I'd like to combine arr1 and arr2 into
arr*/
}

if ( rank == 1){
  debug1 << "This is process " << rank << endl;
  double arr1 = arr[0];
  debug1 << "arr1: " << arr1 << endl;
  arr1 = arr1 + 1;
  debug1 << "arr1+1: " << arr1 << endl;
  MPI::COMM_WORLD.Send(&arr1, 1, MPI::DOUBLE, 0, 0);
}

if ( rank == 2){
  debug2 << "This is process " << rank << endl;
  double arr2 = arr[1];
  debug2 << "arr2: " << arr2 << endl;
  arr2 = arr2 + 2;
  debug2 << "arr2+2: " << arr2 << endl;
}

cout << "Greetings from process " << rank << endl;
MPI::Finalize();
}

我正在编译

mpiCC test.cpp -o test

并与

一起运行

mpirun -np 3 test

因为我希望使用 2 个处理器在 arr 上运行,并使用 1 个处理器(进程 0)来收集组件。

我的问题是程序在使用时冻结

MPI::COMM_WORLD.Recv(&amp;arr1, 1, MPI::DOUBLE, 0, 0);

在进程 0 上。

有人知道为什么会这样吗?我只是想通过处理器在数组上分配计算,并认为这将是一个很好的例子。

【问题讨论】:

  • 首先,不推荐使用 MPI C++ 集,因此建议使用 C 绑定。其次,所有数据是否总是要回到主控,即主控是否包含完整的数组?
  • rank=2 的处理器不应该发送一半的数组吗?
  • 不幸的是,由于与我正在使用的其他现有代码的兼容性,我不得不使用 C++。
  • 是的,rank=2 的处理器应该发送数组的一半,但我似乎无法让 rank=1 的处理器正确发送它的一半。

标签: c++ parallel-processing openmpi


【解决方案1】:

当您使用 MPI 时,有一些功能专为此类任务而设计。 MPI_Scatter 和 MPI_Reduce。它允许您将数组划分为 n 个子节点进行计算并将结果返回给协调器。

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2017-05-20
    • 2017-01-25
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多