Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default parameters to restrict(amp) functions

The following code fails to compile. The error messages are :

Error 1:

error C3930: 'foo' : no overloaded function has restriction specifiers that are compatible with the ambient context ''

Error 2:

error C2660: 'f1' : function does not take 0 arguments

Error 3:

IntelliSense: amp-restricted function "int foo() restrict(amp)" (declared at line 5) must be called from an amp-restricted function

The program:

#include <amp.h>
#include <iostream>
using namespace std;

int foo() restrict(amp) { return 5; }

int f1(int x = foo()) restrict(amp) {
  return x;
}

int main()
{
  using namespace concurrency;

  int a[10] = {0};
  array_view<int> av(10, a);

  parallel_for_each(av.extent, [=](index<1> i) restrict(amp) {
    av[i] = f1();
  });

  for(unsigned i=0; i<10; ++i) {
    cout << av[i] << "\n";
  }
  return 0;
}

Strangely, when I remove the restrict(amp) on foo(), and replace the call of f1() in the lambda with, say, 5, the program compiles fine. So what are the rules for function calls in default arguments to amp functions?

like image 708
keveman Avatar asked Nov 04 '22 09:11

keveman


1 Answers

MSDN Forum answer to the question.

The semantics of the default arguments we have chosen are aligned with the overarching premise of C++ that parsing of a program is done in a one left-to-right pass (notwithstanding few significant exceptions to this rule, most notably member functions) - therefore since the restriction specifier is read after the function parameter declaration, any function calls located in default argument expressions are bound according to the "outer" restriction specification, for better or for worse. In other words, you read the program from the beginning with the cpu-restriction "active" (because it's the default one) and switch to restriction X for everything between "restrict(X)" and "}" closing the relevant scope.

like image 83
keveman Avatar answered Nov 11 '22 04:11

keveman