0

在学习 MPI-2 单向通信的(一系列)尝试中,我正在测试以下代码,其中我将一个基本类型值存储int在主进程中,并将其公开给所有其他进程。现在我对整数所做的事情很简单。我让每个进程迭代地递增它,直到共享整数达到最大值。每个进程都会在打印出共享整数之前进行隔离,如下所示(完整代码在底部):

  for (int i = 0; i < 10; i++) {
    mpi_val_t<int>::inc_val(val,1);
    if (mpi_val_t<int>::get_val(val) >= 25)
      break;
  }
  MPI_Win_fence(0,val->win);
  std::cout << "val = " << mpi_val_t<int>::get_val(val) << std::endl;

我希望每个进程25在退出时都会打印相同的值 ()。但我有时会得到这样的输出:

$ mpiexec.exe -n 4 a.exe
val = 17
val = 22
val = 25
val = 25

有人可以解释一下这里发生了什么以及如何正确同步它吗?

谢谢,


代码:

#include <mpi.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>

template <typename T>
inline MPI_Datatype mpi_type();
template <> inline MPI_Datatype mpi_type<int>() { return MPI_INT; }
template <> inline MPI_Datatype mpi_type<double>() { return MPI_DOUBLE; }

template <typename T>
class mpi_val_t {
public:
  MPI_Win win;
  int  hostrank;  //id of the process that host the value to be exposed to all processes
  int  rank;      //process id
  int  size;      //number of processes
  T    val;       //the shared value

  static struct mpi_val_t *create_val(int hostrank, T v) {
      struct mpi_val_t *val;

      val = (struct mpi_val_t *)malloc(sizeof(struct mpi_val_t));
      val->hostrank = hostrank;
      MPI_Comm_rank(MPI_COMM_WORLD, &(val->rank));
      MPI_Comm_size(MPI_COMM_WORLD, &(val->size));

      if (val->rank == hostrank) {
          MPI_Alloc_mem(sizeof(T), MPI_INFO_NULL, &(val->val));
          val -> val = v;
          MPI_Win_create(&val->val, sizeof(T), sizeof(T),
                         MPI_INFO_NULL, MPI_COMM_WORLD, &(val->win));
      }
      else {
          MPI_Win_create(&val->val, 0, 1,
                         MPI_INFO_NULL, MPI_COMM_WORLD, &(val->win));
      }
      return val;
  }

  static void delete_val(struct mpi_val_t **val) {
      MPI_Win_free(&((*val)->win));
      free((*val));
      *val = NULL;
      return;
  }

  static T get_val(struct mpi_val_t *val) {
      T ret;
      MPI_Win_lock(MPI_LOCK_SHARED, val->hostrank, 0, val->win);
      MPI_Get(&ret, 1 , mpi_type<T>(), val->hostrank, 0, 1, mpi_type<T>(), val->win);
      MPI_Win_unlock(0, val->win);
      return ret;
  }

  static void inc_val(struct mpi_val_t *val, T inc) {
      MPI_Win_lock(MPI_LOCK_EXCLUSIVE, val->hostrank, 0, val->win);
      MPI_Accumulate(&inc, 1, mpi_type<T>(), val->hostrank, 0, 1, mpi_type<T>(), MPI_SUM,val->win);
      MPI_Win_unlock(0, val->win);
  }

}; //mpi_val_t

int main(int argc, char* argv[])
{
  MPI_Init(&argc, &argv);
  mpi_val_t<int>* val = mpi_val_t<int>::create_val(0,0);
  for (int i = 0; i < 10; i++) {
    mpi_val_t<int>::inc_val(val,1);
    if (mpi_val_t<int>::get_val(val) >= 25)
      break;
  }
  MPI_Win_fence(0,val->win);
  std::cout << "val = " << mpi_val_t<int>::get_val(val) << std::endl;
  mpi_val_t<int>::delete_val(&val);
  MPI_Finalize();
}
4

1 回答 1

2

MPI 的 RMA 中的栅栏调用应该成对出现——第一个开始访问/曝光时期,第二个完成它:

 MPI_Win_fence(0, win);
 ...
 MPI_Win_fence(0, win);

该标准明确警告不要使用栅栏调用而不是障碍:

但是,MPI_WIN_FENCE已知调用它不会结束任何纪元(特别是调用assert = MPI_MODE_NOPRECEDE)不一定充当障碍。

此外,栅栏用于主动目标通信,不应与被动目标通信操作(如MPI_Win_lock.

解决方案:将调用替换为MPI_Win_fenceon 的屏障MPI_COMM_WORLD

另外,请注意您的实现中存在错误 - 当您将窗口锁定为 rankval->hostrank时,您总是将 rank 传递0给 unlock 调用。

于 2014-07-22T15:32:44.530 回答