OpenMP是否复制私有对象? [英] Does OpenMP copy private objects?

查看:111
本文介绍了OpenMP是否复制私有对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序读取大文件(3x280 GB),并对文件中的数据进行拟合.并行化这样的程序非常方便,而使用OpenMP可以很容易地做到这一点.

I'm writing a program that reads huge file (3x280 GB) and does a fitting procedure to the data in the file. It's pretty convenient to parallelise such a program, where this is easily done with OpenMP.

我不了解的是OpenMP中如何获取私有变量.众所周知,fstream的对象是不可复制的,并且很直观,这使我无法将其用作私有对象.因此,文件的读者是共享的.

The thing I don't understand is how private variables are taken in OpenMP. As we all know, fstream's obejcts are a non-copyable, and intiuitively, that prevented me from using it as a private object. So the reader of the file was shared.

后来我遇到了一些问题,我想到尝试将fstreams设为私有,...猜怎么着?有效!!!这怎么可能呢?如果对象是不可复制的,那么OpenMP如何为每个内核使用同一对象的不同副本?

I got some problem later, and I thought of trying have fstreams as private, ... and guess what? it worked!!! How could this be possible?! if the object is non-copyable, how could OpenMP use different copies of the same object for each kernel?

这是我的程序的样子:

fstream dataReaderX(Dirs[0].c_str(), ios::in | ios::binary);
fstream dataReaderY(Dirs[1].c_str(), ios::in | ios::binary);
fstream dataReaderZ(Dirs[2].c_str(), ios::in | ios::binary);
#pragma omp parallel num_threads(cpus_num) shared(...) private(...,dataReaderX,dataReaderY,dataReaderZ)
{
...
}

谢谢.

推荐答案

firstprivate变量将被复制,而不是private-对于后者,默认构造函数被称为:

firstprivate variables are copied, not private - for the latter the default constructor is called:

第2.9.3.3节-private子句:

Section 2.9.3.3 - private clause:

新列表项已初始化,或者具有未定义的初始值,就好像它是在没有初始化程序的情况下在本地声明的.未指定用于类类型的不同私有变量的任何默认构造函数的调用顺序.任何顺序 未指定用于类类型的不同私有变量的C/C ++析构函数.

The new list item is initialized, or has an undefined initial value, as if it had been locally declared without an initializer. The order in which any default constructors for different private variables of class type are called is unspecified. The order in which any C/C++ destructors for different private variables of class type are called is unspecified.

这是一个简单的演示代码:

Here is a simple demonstration code:

#include <fstream>
#include <stdio.h>
#include <omp.h>

int main (void)
{
   std::fstream reader("test.txt", std::ios::in);
   printf("Main thread: reader.is_open() = %d\n", reader.is_open());
   #pragma omp parallel private(reader)
   {
      printf("Thread %d: reader.is_open() = %d\n",
             omp_get_thread_num(), reader.is_open());
   }
   return 0;
}

这是预期的输出:

Main thread: reader.is_open() = 1
Thread 1: reader.is_open() = 0
Thread 0: reader.is_open() = 0
Thread 3: reader.is_open() = 0
Thread 2: reader.is_open() = 0

一个有趣的事情是,英特尔C ++编译器会出现内部错误(断言失败),并在版本11.1、12.0和12.1中进行了测试.另一方面,GNU C ++编译器遵守该标准(上面的输出来自g++).尽管使用英特尔C ++编译器再次出现内部错误,但两个编译器都抱怨使用firstprivate代替.

One funny thing is that Intel C++ Compiler errs with internal error (a failed assertion) - tested with versions 11.1, 12.0 and 12.1. On the other hand GNU C++ compiler adheres to the standard (the output above is from g++). Both compilers complain when firstprivate is used instead although Intel C++ Compiler again errs with internal error.

这听起来很愚蠢,但是您是否检查了是否在使用的特定编译器中启用了OpenMP支持?

It may sound stupid but did you check that you have enabled OpenMP support in the particular compiler that your are using?

这篇关于OpenMP是否复制私有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆