【发布时间】:2017-12-13 22:03:34
【问题描述】:
我想做的是对一些字符串运行基本的 MapReduce 操作。我想:
- 将字符串列表(平等地)分配给我的所有进程,
- 在进程中:将接收到的字符串映射到自定义类的对象(例如
WordWithFrequency), - 收集对象并将它们再次发送到进程以进行进一步操作。
这应该是一项简单的任务,但我找不到正确的方法。这是我损坏的代码:
#include <iostream>
#include <fstream>
#include <mpi.h>
#include <vector>
...
int main(int argc, char *argv[]) {
// Initialize the MPI environment
MPI_Init(&argc, &argv);
// Find out the process rank and the world size
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
vector<string> words = { "a", "bc", "d" };
const int wordsLength = words.size();
const int wordsPerProcess = wordsLength / world_size;
string *subWords = new string[wordsPerProcess];
MPI_Scatter(&words, wordsPerProcess, MPI_CHAR, subWords, wordsPerProcess, ???customDataType???, 0, MPI_COMM_WORLD);
printf("Process %d got words:\n", world_rank);
for (int i = 0; i < wordsPerProcess; ++i) {
cout << subWords[i] << endl;
}
...
输出是一些有趣的字母,从执行到执行:
Process 0 got words:
�R
Process 1 got words:
【问题讨论】:
-
尝试使用
std::vector之类的东西,而不是new[]。由于内存管理问题大大减少,它会让您的生活显着更轻松。 -
@tadman 我认为(实际上,我确定)问题出在
MPI_Scatter,而不是数组。我在这里做的事情完全错误,但我在互联网上找不到任何字符串散射的例子。附言。std::vector没有帮助。 -
在这种特殊情况下,它不会神奇地解决您的所有问题,但从长远来看,它会很有用,因为您不会浪费时间跟踪内存泄漏。
-
stackoverflow.com/questions/47747287/… 的可能重复项 长话短说,
MPI_Scatter()不适用于指针数组。如果你真的想使用MPI_Scatter(),你应该使用char的二维数组(例如,一个固定长度的“字符串”数组) -
我通过使用 Boost.MPI 解决了我的问题,它可以分散字符串向量而无需任何额外工作。它还使得序列化自定义对象变得超级容易。感谢您的帮助!