Unstructured data lifetimes with classes

Hi,
I am wondering whether unstructured data lifetimes are working with C++ classes and constructors/destructors.

I have a stupid class CArray:

  class CArray {
   public:
    CArray(int n) {
      a = new double[n];
      #pragma acc enter data create(CArray::a[0:n])
    }
    ~CArray() {
      #pragma acc exit data delete(CArray::a[0:n])
      delete(a);
    }
      double *a;
  };

and then I want to access ist data in a parallel region. Like this:

int na=120;
CArray myarray(na);
#pragma acc parallel loop  present(myarray[0:na])
     for(i=0; i<na; i++) {
      myarray.a[i] = i;
     }

However, I get the error message that “data in PRESENT clause was not found on device”. If I omit the present clause, the compiler creates a present_or_copyin for that array. If I then kaunch the parallel region, I get a launch failure.

I appreciate any comments.
Thanks, Sandra

Hi Sandra,

There’s two problems here. First is we don’t yet support class members in data clauses due the “this” pointer. It’s actually one of the major features we’re currently working on and expect it to be available in the next few months. You can work around it by creating temp variables which point to the members.

  • Mat
#include <iostream>

  class CArray {
   public:
    CArray(int n) {
      a = new double[n];
      double * tmp = a;
      #pragma acc enter data create(tmp[0:n])
    }
    ~CArray() {
      double * tmp = a;
      #pragma acc exit data delete(tmp)
      delete(a);
    }
      double *a;
  };

int main () {

int na=120;
CArray myarray(na);
double *tmp;
tmp = myarray.a;
#pragma acc parallel loop copyout(tmp[0:na])
     for(int i=0; i<na; i++) {
      tmp[i] = i;
     }

   std::cout <<  myarray.a[0] << " " << myarray.a[na-1] << std::endl;
   exit(0);
}

Dear Mat,
Thanks a lot for your fast response. Your workaround is working.
You mentioned TWO problems. What is the other one?
Sandra

Sorry, I was going to mention that there as was typo in the present clause where you just had “myarray[0:na]” instead of “myarray.a[0:na]”. However, it didn’t matter after the workaround. Forgot to go back and edit.

  • Mat