Question

Do constructors for a struct that will be used in amp code need to have restrict(amp) included? Ex:

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

Or should it be like...

struct Foo
{
  inline Foo(void) restrict(amp)
  {
  }
  float a;
};
Was it helpful?

Solution

Yes. If you want to construct those objects within an AMP kernel. In the example below stuff instances are created on within the amp restricted parallel_for_each. The constructor needs to be marked as restrict(amp) in order to compile correctly.

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();
    };
};

I also wrote this up as a blog post, Using C++ Classes with C++ AMP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top