【问题标题】:Default parameters to restrict(amp) functions限制(amp)函数的默认参数
【发布时间】:2012-11-30 22:17:00
【问题描述】:

以下代码无法编译。错误消息是:

错误 1:

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

错误 2:

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

错误 3:

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

程序:

#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;
}

奇怪的是,当我删除 foo() 上的 restrict(amp) 并将 lambda 中的 f1() 调用替换为 5 时,程序编译得很好。那么 amp 函数的默认参数中函数调用的规则是什么?

【问题讨论】:

  • 我遇到了来自 GCC 和 MSVC 的奇怪消息。原来我有一个MyStruct 的数组,并且元素的数量减少了一个:MyStruct s_val[3] = { MyStruct(...), MyStruct(...) };。两个编译器都没有告诉我计数已关闭。

标签: c++ visual-c++ c++11 visual-studio-2012 c++-amp


【解决方案1】:

MSDN Forum answer 回答问题。

我们选择的默认参数的语义与 C++ 的总体前提一致,即程序的解析是在一次从左到右的过程中完成的(尽管这条规则很少有明显的例外,尤其是成员函数) - 因此,由于在函数参数声明之后读取限制说明符,因此位于默认参数表达式中的任何函数调用都根据“外部”限制说明进行绑定,无论好坏。换句话说,您从一开始就使用 cpu-restriction “active”(因为它是默认设置)阅读程序,然后为“restrict(X)”和“}”之间的所有内容切换到限制 X,从而关闭相关范围。

【讨论】:

  • 你应该解释一下这个。我不清楚你在做什么(除了引用 MSDN)。
猜你喜欢
  • 2011-02-20
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2010-11-28
  • 2013-04-15
相关资源
最近更新 更多