ved*_*eda 3 c++ fstream openmp
我想知道使用openMP是否有可能在文件读取方面获得一些性能提升.
示例代码,
fstream file;
file.open("test.txt",ios::in);
file.seekg(0,ios::end);
int len = file.tellg();
char *arr = new char[len];
char *temp = new char[1];
int i;
#pragma omp parallel for shared(arr, len) private(temp, i)
for(i = 0; i < len; i++)
{
file.seekg(i);
file.read(temp,1);
arr[i] = temp[0];
}
Run Code Online (Sandbox Code Playgroud)
我想使用多个线程进行I/O操作是一个糟糕的选择,因为最终文件读取操作将被序列化.但是,我仍然希望能否获得性能提升.此外,我还想知道openMP如何处理并行文件读取操作.
正如您所提到的,您不可能获得任何加速并行化任何类型的I/O绑定任务.但是,还有一个更大的问题.代码甚至不正确.
该seekg()和read()方法修改file变量.所以你的迭代不是独立的.所以你将在流上有竞争条件.换句话说,循环不可并行化.
所以不要指望代码可以工作 - 更不用说具有更好的性能.