我是MPI编程的新手。所以我正在尝试使用MPI_Scatter将具有静态大小的char *数组分配到char *数组的较小块中。但是结果只对ID 0正确,其余的都有垃圾值。你知道它有什么问题吗?用char *数组使用MPI_Scatter
#include "mpi.h"
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
const static int ARRAY_SIZE = 130000;
using Lines = char[ARRAY_SIZE][16];
// To remove punctuations
struct letter_only: std::ctype<char>
{
letter_only(): std::ctype<char>(get_table()) {}
static std::ctype_base::mask const* get_table()
{
static std::vector<std::ctype_base::mask>
rc(std::ctype<char>::table_size,std::ctype_base::space);
std::fill(&rc['A'], &rc['z'+1], std::ctype_base::alpha);
return &rc[0];
}
};
int main(int argc, char* argv[]) {
int processId;
int fillarraycount=0;
int num_processes;
// Setup MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &processId);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
Lines lines;
// Read the input file and put words into char array(lines)
if (processId == 0) {
std::ifstream file;
file.imbue(std::locale(std::locale(), new letter_only()));
file.open(argv[1]);
std::string workString;
int i = 0;
while(file >> workString){
memset(lines[i], '\0', 16);
memcpy(lines[i++], workString.c_str(), workString.length());
fillarraycount++;
}
}
int n =fillarraycount/num_processes;
char sublines[n][16];
MPI_Scatter(lines,n*16,MPI_CHAR,sublines,n*16,MPI_CHAR,0,MPI_COMM_WORLD);
std::cout<< processId<<" ";
for(int i=0;i<n;++i)
std::cout<<sublines[i]<<" ";
std::cout<<std::endl;
MPI_Finalize();
return 0;
}
我知道我必须在那之后MPI_gather过使用,但我为什么上ID为0的亚系产生的阵列的正确块,但产生的垃圾值其他ID困惑。
我试图编译和与测试程序:
模块负荷的openmpi
MPIC++ -std = C++ 11 try.cpp -o尝试
的mpirun -np 5试try.txt
其中try.txt:
您好,这是try文本文档
又是这种尝试的文本文档
是被NOTIS Si是是是是哈哈
这可能不会帮助,我记得遇到类似的问题,并通过使用指针和'malloc'而不是数组来解决它。 ('char [x] [y] - > char * sublines = malloc(sizeof(char)* x * y)')。唯一其他的事情是确保num_processes在编译时被定义(否则像这样声明数组是无效的,不可能知道要分配多少内存),并最终完成检查:你做了分散,**你是否收集你的结果回来**?你必须[分散和收集](http://mpitutorial.com/tutorials/mpi-scatter-gather-and-allgather/);) – sjm324
请提供[mcve]。 – Zulan