似乎在OpenACC new区域内禁止使用C++ routine运算符。
我想知道为什么(我检查了routine指令的规范,但未找到任何内容)。

这是我使用OpenACC实现的代码,它是使用我自己的复数类的基本复数矩阵产品(我压缩了代码以使其更具可读性):

class Complex {
  private:
    double* c;
  public:
    #pragma acc routine seq
    Complex ( )
    {
      c = new double[2];
      #pragma acc enter data copyin(this)
      #pragma acc enter data create(c[:2])
      c[0] = 0.0;
      c[1] = 0.0;
    }
    Complex ( Complex const& z )
    {
      c = new double[2];
      #pragma acc enter data copyin(this)
      #pragma acc enter data create(c[:2])
      c[0] = z.c[0];
      c[1] = z.c[1];
    }
    ~Complex ( )
    {
      #pragma acc exit data delete(c[:2])
      #pragma acc exit data delete(this)
      delete[] c;
    }
    #pragma acc routine seq
    Complex& operator= ( Complex const z )
    {
      c[0] = z.c[0];
      c[1] = z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator+= ( Complex const z )
    {
      c[0] += z.c[0];
      c[1] += z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator*= ( Complex const z )
    {
      double a(c[0]), b(c[1]);
      c[0] = a*z.c[0] - b*z.c[1];
      c[1] = b*z.c[0] + a*z.c[1];
      return *this;
    }
};
#pragma acc routine seq
inline Complexe operator* ( Complex z1, Complex const z2 )
{
  z1 *= z2;
  return z1;
}

int main ( )
{
  Complex A[N][N];
  Complex B[N][N];
  // initialisation of A and B
  Complex C[N][N];
  #pragma acc data copyout(C[:N]) copyin(A[:N],B[:N])
  {
    #pragma acc parallel loop
    for (unsigned int i = 0; i < N; i++)
    {
      #pragma acc loop
      for (unsigned int j = 0; j < N; j++)
      {
        Complex accum;
        #pragma acc loop seq
        for (unsigned int j = 0; j < N; j++)
        {
          accum += A[i][k]*B[k][j];
        }
        C[i][j] = accum;
      }
    }
  }
}

我知道动态数复数数组并不是最好的主意,但这只是一个例子。

当我用pgc++编译时,出现此错误(来自我的Complex::Complex()构造函数):
PGCC-S-1000-Call in OpenACC region to procedure '_Znam' which has no acc routine information

我读过_Znam过程被new调用。

因此,我想知道为什么在OpenACC区域内无法使用new,以及如何更改代码以避免此问题?

最佳答案

在大多数情况下,OpenACC标准未指定对特定语言功能的支持。这取决于实现,并将取决于目标设备。对于PGI以NVIDIA GPU为目标的OpenACC实现,不,OpenACC计算区域内不支持new。支持“malloc”,但我强烈建议不要从设备代码中动态分配数据。除了具有相对较小的堆(当前默认为8MB,但可以使用环境变量PGI_ACC_CUDA_HEAPSIZE增大到32MB)之外,具有成千上万的线程分配数据可能会导致严重的性能下降。

下面,我使用固定大小的数据成员和动态的数据成员更新了您的示例。除了修复一些拼写错误之外,我还从构造函数/析构函数中删除了“数据”指令,因为“数据”指令只能在宿主代码中使用。使用固定大小的数据成员时,代码很简单。对于动态数据成员,每个“单独的数据成员”都需要“附加”(即,该成员的设备地址需要在设备对象中设置)。 OpenACC标准委员会正在研究一种自动执行此操作的方法,但是现在它需要在程序本身中完成。下面使用的方法(也称为手动深层复制)是PGI扩展,将在下一个OpenACC标准2.6中采用。

测试1固定大小的数据成员:

#include <iostream>
#ifdef _OPENACC
#include <openacc.h>
#endif
#ifndef N
#define N 32
#endif

class Complex {
  private:
    double c[2];
  public:
    #pragma acc routine seq
    Complex ( )
    {
      c[0] = 0.0;
      c[1] = 0.0;
    }
    Complex ( Complex const& z )
    {
      c[0] = z.c[0];
      c[1] = z.c[1];
    }
    ~Complex ( )
    {
    }
    #pragma acc routine seq
    Complex& operator= ( Complex const z )
    {
      c[0] = z.c[0];
      c[1] = z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator+= ( Complex const z )
    {
      c[0] += z.c[0];
      c[1] += z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator*= ( Complex const z )
    {
      double a(c[0]), b(c[1]);
      c[0] = a*z.c[0] - b*z.c[1];
      c[1] = b*z.c[0] + a*z.c[1];
      return *this;
    }
    void printme() {
       std::cout << c[0] << ":" << c[1] << std::endl;
    }

};
#pragma acc routine seq
inline Complex operator* ( Complex z1, Complex const z2 )
{
  z1 *= z2;
  return z1;
}

int main ( )
{
  Complex A[N][N];
  Complex B[N][N];
  // initialisation of A and B
  Complex C[N][N];
  #pragma acc data copyout(C[:N]) copyin(A[:N],B[:N])
  {
    #pragma acc parallel loop
    for (unsigned int i = 0; i < N; i++)
    {
      #pragma acc loop
      for (unsigned int j = 0; j < N; j++)
      {
        Complex accum;
        #pragma acc loop seq
        for (unsigned int k = 0; k < N; k++)
        {
          accum += A[i][k]*B[k][j];
        }
        C[i][j] = accum;
      }
    }
  }
  C[0][0].printme();
}

测试2个动态数据成员
#include <iostream>
#ifdef _OPENACC
#include <openacc.h>
#endif
#ifndef N
#define N 32
#endif

class Complex {
  private:
    double *c;
  public:
    #pragma acc routine seq
    Complex ( )
    {
      c = (double*) malloc(sizeof(double)*2);
      c[0] = 0.0;
      c[1] = 0.0;
    }
    Complex ( Complex const& z )
    {
      c = (double*) malloc(sizeof(double)*2);
      c[0] = z.c[0];
      c[1] = z.c[1];
    }
    ~Complex ( )
    {
      free(c);
    }
    #pragma acc routine seq
    Complex& operator= ( Complex const z )
    {
      c[0] = z.c[0];
      c[1] = z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator+= ( Complex const z )
    {
      c[0] += z.c[0];
      c[1] += z.c[1];
      return *this;
    }
    #pragma acc routine seq
    Complex& operator*= ( Complex const z )
    {
      double a(c[0]), b(c[1]);
      c[0] = a*z.c[0] - b*z.c[1];
      c[1] = b*z.c[0] + a*z.c[1];
      return *this;
    }
    void printme() {
       std::cout << c[0] << ":" << c[1] << std::endl;
    }
#ifdef _OPENACC
    void acc_create() {
        #pragma acc enter data create(c[0:2])
    }
    void acc_copyin() {
        #pragma acc enter data copyin(c[0:2])
    }
    void acc_delete() {
        #pragma acc exit data delete(c)
    }
    void acc_copyout() {
        #pragma acc exit data copyout(c[0:2])
    }
#endif
};
#pragma acc routine seq
inline Complex operator* ( Complex z1, Complex const z2 )
{
  z1 *= z2;
  return z1;
}

int main ( )
{
  Complex A[N][N];
  Complex B[N][N];
  // initialisation of A and B
  Complex C[N][N];

#ifdef _OPENACC
    #pragma acc enter data create(A[0:N][0:N],B[0:N][0:N],C[0:N][0:N])
    for (unsigned int i = 0; i < N; i++)
    {
      for (unsigned int j = 0; j < N; j++) {
           A[i][j].acc_copyin();
           B[i][j].acc_copyin();
           C[i][j].acc_create();
      }
    }
#endif

    #pragma acc parallel loop present(A,B,C)
    for (unsigned int i = 0; i < N; i++)
    {
      #pragma acc loop
      for (unsigned int j = 0; j < N; j++)
      {
        Complex accum;
        #pragma acc loop seq
        for (unsigned int k = 0; k < N; k++)
        {
          accum += A[i][k]*B[k][j];
        }
        C[i][j] = accum;
      }
  }
#ifdef _OPENACC
    for (unsigned int i = 0; i < N; i++)
    {
      for (unsigned int j = 0; j < N; j++) {
           A[i][j].acc_delete();
           B[i][j].acc_delete();
           C[i][j].acc_copyout();
      }
    }
    #pragma acc exit data delete(A[0:N][0:N],B[0:N][0:N],C[0:N][0:N])
#endif
  C[0][0].printme();
}

关于c++ - OpenACC-C++ 'new'运算符问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43497285/

10-11 22:06