How to update class data member in the device

Hello,

For the code below, I initialize the value for the data member x, then I change the value of x in a member function and use the new value of x to assign an array, but it still uses the value from the constructor.

I can find a way to get around it by use a local variable, on the other hand, is there a way to update the value of x in the device? so that it will use the right value in the acc loop?

Best regards,
Feng

  using namespace std;

  #include <iostream>

 class A
{
    private:
     double x;

  public:
     A()
    {
       x = 2;
       #pragma acc enter data copyin(this)
    }

    ~A()
    {
       #pragma acc exit data delete(this)
    }

    void change()
   {
       int i;
       x = 5;

       double* l;
       l = new double [10];
      #pragma acc enter data copyin(l[0:10])
      #pragma acc parallel loop present(l[0:10],this)
       for(i=0; i<10; i++)
      {
         l[i] = x;
      }
      #pragma acc exit data copyout(l[0:10])

       for(i=0; i<10; i++)
      {
         cout << l[i] << "\n";
      }
       delete[] l;
   }
};
 int main()
{
     A a;

     a.change();
     return 0;
}

Hi Feng,

You need to put “x” in a update directive so the device value is synchronized with the updated host value.

    void change()
   {
       int i;
       x = 5;
      #pragma acc update device(x)   // << Add update of "x"

       double* l;
       l = new double [10];
      #pragma acc enter data copyin(l[0:10])
      #pragma acc parallel loop present(l[0:10],this)
       for(i=0; i<10; i++)
      {
         l[i] = x;
      }
      #pragma acc exit data copyout(l[0:10])

-Mat

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.