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).