在学习 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();
}