我是MPI编程的新手。我之所以使用C ++,是因为我想使用向量而不是数组(因此用户可以动态选择游戏网格的大小(Conway的《人生游戏》),而不是硬编码的数组大小。
假设:共有4个过程(p = 4
)
这是我的代码:
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
vector<CELL> *gameBoard = NULL;
if (rank == 0)
{
//create the gameboard on just the root process
gameBoard = new vector<CELL>();
gameBoard->resize(16);
}
//create a partialGrid on EACH process
vector<CELL> *partialGrid = new vector<CELL>();
partialGrid->resize(4);
int partialSize = 4;
MPI_Scatter(gameBoard, partialSize, mpi_cell, partialGrid, partialSize, mpi_cell, 0, MPI_COMM_WORLD);
//do something on each process
cout << "HI!!! I'm scattered process number " << rank << endl;
//ideally, do something to the sub vector here (once it works)
vector<CELL> *rbuf = NULL; //Just make null for all other processes. Valid receive buffer only needed for root (rank = 0)
if (rank == 0)
{
//make sure the receiving buff is created (for just the root rank 0)
vector<CELL> *rbuf = new vector<CELL>();
rbuf->resize(16);
cout << "RANK 0 TEST!" << endl;
}
MPI_Gather(&partialGrid, (pow(n, 2) / p), mpi_cell, &rbuf, (pow(n, 2) / p), mpi_cell, 0, MPI_COMM_WORLD);
目标是(目前为硬编码)将16个元素的向量拆分为4个子向量,每个子元素用于4个过程中的每个要执行的操作。
我认为我的等级0的逻辑是正确的。我仅在排名过程中为MPI_Gather
创建一个接收缓冲区,并且在MPI_Scatter
之前为EACH过程创建子矢量。我尝试了很多事情,MPI_Scatter
总是失败。请帮忙!
注意:CELL
是九个struct
值中的int
。 mpi_cell
是我在MPI程序中创建的自定义类型。为简单起见,已排除了代码,但如果要测试,可以将CELL
替换为int
,将mpi_cell
替换为MPI_INT
。
我收到细分错误:
答案 0 :(得分:1)
如评论中助理所述,您的new
错误。使用std::vector
的正确方法如下:
std::vector<CELL> gameBoard;
if (rank == 0)
gameBoard.resize(16);
std::vector<CELL> partialGrid;
partialGrid.resize(4);
MPI_Scatter(gameBoard.data(), 4, mpi_cell, partialGrid.data(), 4, mpi_cell, 0, MPI_COMM_WORLD);
// ...
std::vector<CELL> rbuf;
if (rank == 0)
rbuf.resize(16);
MPI_Gather(partialGrid.data(), 4, mpi_cell, rbuf.data(), 4, mpi_cell, 0, MPI_COMM_WORLD);
为简单起见,我也对所有常量进行了硬编码。