-2

Why does the following code not allow me to set var to 10 via the function intfun?

#include <iostream>

void intfun(int * variable){
    #pragma acc parallel deviceptr(variable) num_gangs(1) num_workers(1)
    {
        *variable = 10;
    }
}

int main(){
    int var;

    #pragma acc enter data create(var)
    #pragma acc host_data use_device(var)
    {
        intfun(&var);
    }
    #pragma acc exit data copyout(var)

    std::cout << var << std::endl;
}

Compilation:

pgcpp -acc main.cpp

Execution:

PGCC-S-0155-Compiler failed to translate accelerator region (see -Minfo messages): Unknown variable reference (main.cpp: 5)
PGCC/x86 Linux 14.9-0: compilation completed with severe errors

How can I get intfun to set the value of the parameter int var on the device?

lodhb
  • 929
  • 2
  • 12
  • 29
  • 1
    Take away all the fancy OpenACC pragmas and ask yourself the question "how could `intfun` modify an argument passed by *value* at the caller's scope". Hopefully you will arrive at the answer that it cannot. No compiler magic and pragmas can change that.... – talonmies Sep 22 '14 at 20:43
  • Indeed, please see my edited post. – lodhb Sep 22 '14 at 20:46

1 Answers1

2

You haven't given the compiler enough information to scope the usage of *variable within intfun.

The following seems to work fine:

$ cat main7.cpp
#include <iostream>

void intfun(int * variable){
    #pragma acc parallel copy(variable[:1])
    {
        *variable = 10;
    }
}

int main(){
    int var;

        intfun(&var);

    std::cout << var << std::endl;
}
$ pgcpp -acc -Minfo main7.cpp
intfun(int *):
      5, Generating copy(variable[:1])
         Accelerator kernel generated
         Generating Tesla code
$ ./a.out
10
$

where I have copy(variable[:1]), copyout(variable[:1]) would work as well, for this particular example.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Thanks! Follow-up question here: http://stackoverflow.com/questions/26003954/how-is-value-being-copied-in-in-openacc – lodhb Sep 23 '14 at 20:15