C++ 将MPI_散点与char*数组一起使用
我是MPI编程新手。因此,我尝试使用MPI_散点将具有静态大小的char*数组分布到char*数组的几个较小的块中。但结果仅对ID 0正确,其余的都有垃圾值。你知道它有什么毛病吗C++ 将MPI_散点与char*数组一起使用,c++,mpi,C++,Mpi,我是MPI编程新手。因此,我尝试使用MPI_散点将具有静态大小的char*数组分布到char*数组的几个较小的块中。但结果仅对ID 0正确,其余的都有垃圾值。你知道它有什么毛病吗 #include "mpi.h" #include <algorithm> #include <functional> #include <cstdlib> #include <ctime> #include <cctype> #include <fst
#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.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
常量静态整数数组大小=130000;
使用Lines=char[ARRAY_SIZE][16];
//删除标点符号
仅结构字母_:std::ctype
{
字母_only():std::ctype(get_table()){}
静态std::ctype_base::mask const*get_table()
{
静态std::vector
rc(std::ctype::table_size,std::ctype_base::space);
std::fill(&rc['A'],&rc['z'+1],std::ctype_base::alpha);
返回&rc[0];
}
};
int main(int argc,char*argv[]){
int进程ID;
int fillarraycount=0;
int-num_过程;
//设置MPI
MPI_Init(&argc,&argv);
MPI通信等级(MPI通信世界和进程ID);
MPI_Comm_大小(MPI_Comm_WORLD和num_进程);
线条;
//读取输入文件并将单词放入字符数组(行)
if(processId==0){
std::ifstream文件;
imbue(std::locale(std::locale(),仅新字母_());
打开(argv[1]);
std::字符串工作字符串;
int i=0;
while(文件>>工作字符串){
memset(第[i]行,'\0',16行);
memcpy(行[i++],workString.c_str(),workString.length());
fillarraycount++;
}
}
int n=fillarraycount/num_进程;
字符子行[n][16];
MPI_分散(行,n*16,MPI_字符,子行,n*16,MPI_字符,0,MPI_通信世界);
std::cout在0以外的列组上,n
是0,因为fillarraycount
从不递增。如果fillarraycount
是动态的,则必须首先向所有列组广播ist。这可能没有帮助,我记得遇到了类似的问题,并通过使用指针和malloc
而不是数组解决了它。(char[x][y]->char*sublines=malloc(sizeof(char)*x*y)
)。唯一突出的是确保在编译时定义了num\u进程(否则这样声明数组无效,无法知道要分配多少内存),和最后的理智检查:你做了分散,你收集了你的结果吗?你必须;)请提供一个。谢谢你的回答!你能再解释一下吗,我真的想完全理解这个问题。。谢谢!