【发布时间】:2015-03-20 21:24:17
【问题描述】:
设置
您好,我有用于读取 ASCII 双精度数据的 Fortran 代码(问题底部的数据文件示例):
program ReadData
integer :: mx,my,mz
doubleprecision, allocatable, dimension(:,:,:) :: charge
! Open the file 'CHGCAR'
open(11,file='CHGCAR',status='old')
! Get the extent of the 3D system and allocate the 3D array
read(11,*)mx,my,mz
allocate(charge(mx,my,mz) )
! Bulk read the entire block of ASCII data for the system
read(11,*) charge
end program ReadData
和“等效”的 C++ 代码:
#include <fstream>
#include <vector>
using std::ifstream;
using std::vector;
using std::ios;
int main(){
int mx, my, mz;
// Open the file 'CHGCAR'
ifstream InFile('CHGCAR', ios::in);
// Get the extent of the 3D system and allocate the 3D array
InFile >> mx >> my >> mz;
vector<vector<vector<double> > > charge(mx, vector<vector<double> >(my, vector<double>(mz)));
// Method 1: std::ifstream extraction operator to double
for (int i = 0; i < mx; ++i)
for (int j = 0; j < my; ++j)
for (int k = 0; k < mz; ++k)
InFile >> charge[i][j][k];
return 0;
}
Fortran 踢@$$ 并取名
注意这一行
read(11,*) charge
执行与 C++ 代码相同的任务:
for (int i = 0; i < mx; ++i)
for (int j = 0; j < my; ++j)
for (int k = 0; k < mz; ++k)
InFile >> charge[i][j][k];
其中InFile 是if stream 对象(请注意,虽然 Fortran 代码中的迭代器从 1 而不是 0 开始,但范围是相同的)。
但是,Fortran 代码的运行方式比 C++ 代码快得多,我认为是因为 Fortran 做了一些聪明的事情,比如根据范围和形状(mx、my、@987654345 的值)读取/解析文件@) 一口气,然后简单地将charge 指向读取数据的内存。相比之下,C++ 代码需要在每次迭代中来回访问 InFile 和 charge(通常很大),从而导致(我相信)更多的 IO 和内存操作。
我正在读取可能有数十亿个值(数 GB),所以我真的想最大限度地提高性能。
我的问题:
如何在 C++ 中实现 Fortran 代码的性能?
继续...
这是一个比上面的 C++ 快得多的 C++ 实现,其中文件被一次性读取到 char 数组中,然后在解析 char 数组时填充 charge:
#include <fstream>
#include <vector>
#include <cstdlib>
using std::ifstream;
using std::vector;
using std::ios;
int main(){
int mx, my, mz;
// Open the file 'CHGCAR'
ifstream InFile('CHGCAR', ios::in);
// Get the extent of the 3D system and allocate the 3D array
InFile >> mx >> my >> mz;
vector<vector<vector<double> > > charge(mx, vector<vector<double> >(my, vector<double>(mz)));
// Method 2: big char array with strtok() and atof()
// Get file size
InFile.seekg(0, InFile.end);
int FileSize = InFile.tellg();
InFile.seekg(0, InFile.beg);
// Read in entire file to FileData
vector<char> FileData(FileSize);
InFile.read(FileData.data(), FileSize);
InFile.close();
/*
* Now simply parse through the char array, saving each
* value to its place in the array of charge density
*/
char* TmpCStr = strtok(FileData.data(), " \n");
// Gets TmpCStr to the first data value
for (int i = 0; i < 3 && TmpCStr != NULL; ++i)
TmpCStr = strtok(NULL, " \n");
for (int i = 0; i < Mz; ++i)
for (int j = 0; j < My; ++j)
for (int k = 0; k < Mx && TmpCStr != NULL; ++k){
Charge[i][j][k] = atof(TmpCStr);
TmpCStr = strtok(NULL, " \n");
}
return 0;
}
同样,这比基于运算符的简单 >> 方法快得多,但仍比 Fortran 版本慢得多——更不用说更多的代码了。
如何获得更好的性能?
如果我要自己实现方法 2,我确信方法 2 是可行的方法,但我很好奇如何提高性能以匹配 Fortran 代码。我正在考虑和目前正在研究的事物类型是:
- C++11 和 C++14 功能
- 经过优化的 C 或 C++ 库仅用于执行此类操作
- 对方法 2 中使用的各个方法的改进
- 标记化库,例如 C++ String Toolkit Library 而不是
strtok() -
char到double的转换比atof()更高效
- 标记化库,例如 C++ String Toolkit Library 而不是
C++ 字符串工具包
特别是,C++ 字符串工具包库将采用 FileData 和分隔符 " \n" 并给我一个字符串标记对象(称为 FileTokens,然后三重 for 循环看起来像
for (int k = 0; k < Mz; ++k)
for (int j = 0; j < My; ++j)
for (int i = 0; i < Mx; ++i)
Charge[k][j][i] = FileTokens.nextFloatToken();
这会稍微简化代码,但在将FileData 的内容(本质上)复制到FileTokens 中需要额外的工作,这可能会扼杀使用nextFloatToken() 方法带来的任何性能提升(据推测比strtok()/atof() 组合)。
C++ String Toolkit (StrTk) Tokenizer tutorial page(包含在问题的底部)上有一个使用 StrTk 的 for_each_line() 处理器的示例,看起来与我想要的应用程序相似。然而,这两种情况的不同之处在于,我无法假设输入文件的每一行会出现多少数据,而且我对 StrTk 的了解也不够多,无法判断这是否是一个可行的解决方案。
不是重复的
ASCII 数据快速读取到数组或结构的话题之前已经出现过,但是我查看了以下帖子,他们的解决方案还不够:
- Fastest way to read data from a lot of ASCII files
- How to read numbers from an ASCII file (C++)
- Read Numeric Data from a Text File in C++
- Reading a file and storing the contents in an array
- C/C++ Fast reading large ASCII data file to array or struct
- Read ASCII file into matrix in C++
- How can I read ASCII data file in C++
- Reading a file and storing the contents in an array
- Reading in data in columns from a file (C++)
- The Fastest way to read a .txt File
- How does fast input/ output work in C/C++, by using registers, hexadecimal number and the likes?
- reading file into struct array
示例数据
这是我正在导入的数据文件的示例。 ASCII 数据由空格和换行符分隔,如下例所示:
5 3 3
0.23080516813E+04 0.22712439791E+04 0.21616898980E+04 0.19829996749E+04 0.17438686650E+04
0.14601734127E+04 0.11551623512E+04 0.85678544224E+03 0.59238325489E+03 0.38232265554E+03
0.23514479113E+03 0.14651943589E+03 0.10252743482E+03 0.85927499703E+02 0.86525872161E+02
0.10141182750E+03 0.13113419142E+03 0.18057147781E+03 0.25973252462E+03 0.38303754418E+03
0.57142097675E+03 0.85963728360E+03 0.12548019843E+04 0.17106124085E+04 0.21415379433E+04
0.24687336309E+04 0.26588012477E+04 0.27189091499E+04 0.26588012477E+04 0.24687336309E+04
0.21415379433E+04 0.17106124085E+04 0.12548019843E+04 0.85963728360E+03 0.57142097675E+03
0.38303754418E+03 0.25973252462E+03 0.18057147781E+03 0.13113419142E+03 0.10141182750E+03
0.86525872161E+02 0.85927499703E+02 0.10252743482E+03 0.14651943589E+03 0.23514479113E+03
StrTk 示例
这里是上面提到的StrTk example。该场景正在解析包含 3D 网格信息的数据文件:
输入数据:
5
+1.0,+1.0,+1.0
-1.0,+1.0,-1.0
-1.0,-1.0,+1.0
+1.0,-1.0,-1.0
+0.0,+0.0,+0.0
4
0,1,4
1,2,4
2,3,4
3,1,4
代码:
struct point
{
double x,y,z;
};
struct triangle
{
std::size_t i0,i1,i2;
};
int main()
{
std::string mesh_file = "mesh.txt";
std::ifstream stream(mesh_file.c_str());
std::string s;
// Process points section
std::deque<point> points;
point p;
std::size_t point_count = 0;
strtk::parse_line(stream," ",point_count);
strtk::for_each_line_n(stream,
point_count,
[&points,&p](const std::string& line)
{
if (strtk::parse(line,",",p.x,p.y,p.z))
points.push_back(p);
});
// Process triangles section
std::deque<triangle> triangles;
triangle t;
std::size_t triangle_count = 0;
strtk::parse_line(stream," ",triangle_count);
strtk::for_each_line_n(stream,
triangle_count,
[&triangles,&t](const std::string& line)
{
if (strtk::parse(line,",",t.i0,t.i1,t.i2))
triangles.push_back(t);
});
return 0;
}
【问题讨论】:
-
Fortranner 使用
read(11,*) charge(1:mx,1:my,1:mz)或read(11,*) charge而不是read(11,*)(((charge(i,j,k),i=1,mx),j=1,my),k=1,mz) -
为什么不编写一个 Fortran 例程将代码读入数组并从 C++ 调用该例程?看看fortran-iso-c-binding...
-
是的,第二种用法假定
charge具有正确的尺寸。如果不是这样,您必须指出子数组的边界。 -
但请记住,如果您非常关心 IO 速度,请使用二进制(流、未格式化...)数据格式。
-
那么您可以使用小型 fortran 程序对数据进行预处理,将 ascii 文件转换为二进制文件,然后使用现有的 C++ 导入器读取它们。还告诉那些化学家开始编写二进制文件并停止浪费你的磁盘空间:)