2

将在 amp 代码中使用的结构的构造函数是否需要包含 restrict(amp)?前任:

struct Foo
{
  inline Foo(void)
  {
  }
  float a;
};

还是应该像...

struct Foo
{
  inline Foo(void) restrict(amp)
  {
  }
  float a;
};
4

1 回答 1

1

是的。如果您想在 AMP 内核中构建这些对象。在下面的示例中,stuff实例是在amp受限制的parallel_for_each. 构造函数需要标记为restrict(amp)才能正确编译。

class stuff
{
public:
    int a;

    stuff(int v) restrict(amp, cpu) 
        : a(v) { }
};

class test_case
{
public:
    test_case() { }

    void test_amp()
    {
        concurrency::array_view<stuff, 1> data(100);

        concurrency::parallel_for_each(data.extent,
            [data](concurrency::index<1> idx) restrict(amp)
        {
            data[idx] = stuff(s.a * s.a);
        });
        data.synchronize();
    };
};

我还把它写成了一篇博文,Using C++ Classes with C++ AMP

于 2014-02-05T05:39:44.040 回答