我已经用 C++ 和 MPI 实现了一个代码,它应该进行数百万次计算,并为每个处理其数据的 CPU 将数百万个数字保存在大约 7 个文件中。我使用了大约 10,000 个内核,总共提供了 70,000 个文件,其中包含数百万行要并行编写的代码。
我使用 ofstream 进行写作,但由于某种原因,MPI 代码在中间中断并且文件似乎是空的。我希望每个处理器独立于所有其他处理器编写其 7 个文件,根据我的搜索,这可以使用 MPI 完成,但我在许多资源中阅读了它,我不明白它如何用于独立写入和在执行期间动态指定文件名。如果这是正确的方法,有人可以尽可能详细地解释它吗?如果不是,请尽可能详细地解释您的其他建议?
我目前不起作用的写作看起来像这样:
if (rank == 0)
{
if(mkdir("Database",0777)==-1)//creating a directory
{
}
rowsCount = fillCombinations(BCombinations, RCombinations,
BList, RList,
maxCombinations, BIndexBegin,
BIndexEnd, RIndexBegin,
RIndexEnd,
BCombinationsIndex, RCombinationsIndex
);
}
//then broad cast all the arrays that will be used in all of the computations and at the root
//send all the indexes to work on on the slaves then at the slave
or (int cc = BeginIndex ; cc <= EndIndex; cc++)
{
// begin by specifying the values that will be used
// and making files for each B and R in the list
BIndex = betaCombinationsIndex [cc];
RIndex = roughCombinationsIndex [cc];
//creating files to save data in and indicating the R and B by their index
//specifying files names
std::string str1;
std::ostringstream buffer1;
buffer1 << "Database/";
str1 = buffer1.str();
//specifying file names
std::ostringstream pFileName;
std::string ppstr2;
std::ostringstream ppbuffer2;
ppbuffer2 <<"P_"<<"Beta_"<<(BIndex+1)<<"_Rho_"<<(RIndex+1)<<"_sampledP"<< ".txt";
ppstr2 = ppbuffer2.str();
pFileName <<str1.c_str()<<ppstr2.c_str();
std::string p_file_name = pFileName.str();
std::ostringstream eFileName;
std::string eestr2;
std::ostringstream eebuffer2;
eebuffer2 <<"E_"<<"Beta_"<<(BIndex+1)<<"_Rho_"<<(RIndex+1)<<"_sampledE"<< ".txt";
eestr2 = eebuffer2.str();
eFileName <<str1.c_str()<< eestr2.c_str();
std::string e_file_name = eFileName.str();
// and so on for the 7 files ....
//creating the files
ofstream pFile;
ofstream eFile;
// and so on for the 7 files ....
//opening the files
pFile .open (p_file_name.c_str());
eFile .open (e_file_name.c_str());
// and so on for the 7 files ....
// then I start the writing in the files and at the end ...
pFile.close();
eFile.close();
}
// end of the segment loop