OpenACC - C ++' new'运营商问题

时间:2017-04-19 13:32:45

标签: c++ new-operator openacc

在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以及如何更改代码以避免此问题?

1 个答案:

答案 0 :(得分:4)

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

下面我使用固定大小的数据成员和动态数据成员更新了您的示例。除了修正一些拼写错误,我删除了&#34;数据&#34;来自构造函数/析构函数的指令,因为&#34;数据&#34;指令只能在主机代码中使用。使用固定大小的数据成员时,代码是直截了当的。对于动态数据成员,每个单独的数据成员都需要&#34;附加&#34; (即,需要在设备对象中设置成员的设备地址)。 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();
}