[英]openMP lastprivate and firstprivate to the same variable
[英]ofstream variable cannot appear in OpenMP firstprivate?
码
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>&)[with _CharT = char; _Traits = std :: char_traits]'
/ usr / include / c ++ / 5 / fstream:723:7:注意:这里声明为basic_ofstream(const basic_ofstream&)= delete;
错误:'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; } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.