HPC Magazine - OpenACC 2.0 - The new data management features

Listing 5: Global variable with dynamic lifetime on the device.


//---- external.c ----

int counter ; 
   
#pragma acc routine(update_counter) 
void update_counter(void) {
  counter++ ;
}


//---- main.c ----

extern int counter ; 

void work(void)
{
  #pragma acc parallel 
  {
    /* 
     * Problem: The compiler is not aware that
     * update_counter is accessing counter and
     * since counter has a dynamic lifetime on 
     * the device, how can update_counter figure 
     * out its (non-constant) device address?
     */
    update_counter() ; 
  }
}

int main(void)
{
  #pragma data copy(counter) ;
  {
    work() ;      
  }
}