共用方式為


restrict(amp) restrictions part 5 of N – identifier expressions

This post assumes and requires that you have read the introductory post to this series which also includes a table of content. With that out of the way let’s look at restrictions around identifier expressions.

In an amp-restricted function, you cannot access any global or static variables (including static data members). This restriction is introduced because “global” states give the expectation that they are interoperable between the GPU and CPU, which is very hard to use without introducing data races. Therefore, it’s better to disallow them to prevent pitfalls. This also prompts a more object-oriented programming style, and encourages more explicit data transfer between GPU and CPU.

The only exception to this restriction is a static constant integral variable, which can be safely reduced to an integer literal. For example,

    static const int SCALE = 75;

 

    void foo(int& n) restrict(amp)

    {

        n *= SCALE; // allowed although SCALE is a static variable

}

Similarly, you cannot declare a static local variable within an amp-restricted function; neither can you declare extern variables. The only supported storage classes for local variables within amp-restricted functions are register, auto, or tile_static. tile_static is a new storage class introduced with C++ AMP which is described in another blog post (tile_static, tile_barrier, and tiled matrix multiplication with C++ AMP).

Comments

  • Anonymous
    February 20, 2013
    Hi, I'm wondering if it is possible to access an 3-dim array that is declared as static data member in a class object. I have put the definition outside the class as I have learned c++ requires.

  • Anonymous
    February 21, 2013
    Hi Dave, By "access an 3-dim array that is declared as static data member", did you mean your class have a static data member with type of array<T, 3>, and you want to access it in a restrict(amp) function? If so, the answer is no. You cannot access global or static (including class static data member) in restrict(amp) function. Let me know if I didn't understand your question correctly. Thanks, Lingli

  • Anonymous
    February 21, 2013
    Ahh... I forgot to write, it is constant, eg: class MyClass { private: static const int myArray[2][2][2]; } const int MyClass::myArray[2][2][2] = {{{1, 2},{1, 2}},  {{1, 2},{1, 2}}};

  • Anonymous
    February 21, 2013
    Although it's const, but it's not simple const integer literal that the compiler can recognize and replace it with the literal value. So no, it cannot be accessed within restrict(amp) code.

  • Anonymous
    February 21, 2013
    OK. The class I'm trying to port has metods that returns values from the array on indexes calculated from member variables (non static or const). Any suggestions on how to make it AMP friendly? Thanks for your help!

  • Anonymous
    February 27, 2013
    Hi Dave, Sorry for the late response. Didn't notice your comment until now. Can you make the array an instant data member of your class? Or if's shared across many objects, can you capture it via lambda? It probably would be easier for me to assist you if you can share some code snippet that explains your intention clearly. Thanks, Lingli