流变量不能出现在 OpenMP 第一私有中?
ofstream variable cannot appear in OpenMP firstprivate?
code
ofstream myfile("file_path");
#pragma omp parallel for default(none) schedule(dynamic) firstprivate(myfile) private(i)
for(i=0; i<10000; i++) {
myfile<<omp_get_thread_num()+100<<endl;
}
但是编译器向我显示错误:
错误:使用已删除的函数"std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const std::basic_ofstream<_CharT, _Traits>&( [_CharT = char; _Traits = std::char_traits]'
/usr/include/c++/5/fstream:723:7:注意:此处声明 basic_ofstream(常量 basic_ofstream&( = 删除;
错误:在封闭并行中未指定"myfile">
firstprivate
通过创建值的线程私有副本来工作。这不适用于流,因为您无法复制它们。仅通过打开多个文件流,无法安全地写入文件。基本上有两种选择:
-
有一个共享流,使用
#pragma omp critical
保护对它的所有线程访问。ofstream myfile("file_path"); #pragma omp parallel for for (int i=0; i < 10000; i++) { #pragma omp critical myfile << (omp_get_thread_num()+100) << endl; }
-
为不同文件上的每个线程打开一个流。
#pragma omp parallel { ofstream myfile(std::string("file_path.") + std::to_string(omp_get_thread_num())); #pragma omp for for (int i=0; i < 10000; i++) { myfile << (omp_get_thread_num()+100) << endl; } }
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么模板类中的对象不能返回值
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 数组长度,为什么从命令行获取时不能使用它?
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 为什么我不能将 rand() 与数组的大小一起使用?
- 流变量不能出现在 OpenMP 第一私有中?
- Mat::convertTo不能与OpenMP一起工作
- Makefile with OpenMP:不能用-c、-S或-E指定多个文件的-o
- 我的openmp代码不能在服务器上工作