【问题标题】:C++ AMP ConstructorsC++ AMP 构造函数
【发布时间】:2013-06-16 17:31:55
【问题描述】:

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

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

或者应该是……

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

【问题讨论】:

    标签: c++ parallel-processing gpu c++-amp


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2011-04-03
      • 2010-12-16
      • 2011-04-19
      • 2020-05-14
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多