1 out of 1 rated this helpful - Rate this topic

Restriction Clause (C++ AMP)

Visual Studio 11

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The restriction modifier is applied to function and lambda declarations. It enforces restrictions on the code in the function and the behavior of the function in applications that use the C++ Accelerated Massive Parallelism (C++ AMP) runtime.

The restrict clause takes the following forms:

Clause

Description

restrict(cpu)

The function can use the full C++ language. Only other functions declared with restrict(cpu) functions can call the function.

restrict(amp)

The function can only use the subset of the C++ language that C++ AMP can accelerate.

A sequence of restrict(cpu) and restrict(amp).

The function must adhere to the limitations of both restrict(cpu) and restrict(amp). The function can be called by functions declared with restrict(cpu), restrict(amp), restrict(cpu, amp), or restrict(amp, cpu).

The form restrict(A) restrict(B) can be written as restrict(A,B).

The restrict keyword is a contextual keyword. The restriction specifiers, cpu and amp are not reserved words. The list of specifiers is not extensible. A function that does not have a restrict clause is the same as a function that has the restrict(cpu) clause.

A function that has the restrict(amp) clause has the following limitations:

  • The function can call only functions that have the restrict(amp) clause.

  • The function must be inlinable.

  • The function can declare only int, unsigned int, float, and double variables, and classes and structures that contain only these types.

  • Lambda functions cannot capture by reference and cannot capture pointers.

  • References and single-indirection pointers are supported only as local variables and function arguments.

  • The following are not allowed:

    • Recursion.

    • Variables declared with the volatile keyword.

    • Virtual functions.

    • Pointers to functions.

    • Pointers to member functions.

    • Pointers in structures.

    • Pointers to pointers.

    • goto statements.

    • Labeled statements.

    • try , catch, or throw statements.

    • Global variables.

    • Static variables. Use tile_static Keyword instead.

    • dynamic_cast casts.

    • The typeid operator.

    • asm declarations.

    • Varargs.

For a discussion of function limitations, see Parallel Programming in Native Code Blog.

The following example shows how to use the restrict(amp) clause to limit execution to the Direct3D target.


#include <amp.h>
#include <iostream>
using namespace concurrency;


void CampMethod() {
    int aCPP[] = {1, 2, 3, 4, 5};
    int bCPP[] = {6, 7, 8, 9, 10};
    int sumCPP[5] = {0, 0, 0, 0, 0};

    // Create C++ AMP objects.
    array_view<int, 1> a(5, aCPP);
    array_view<int, 1> b(5, bCPP);
    array_view<int, 1> sum(5, sumCPP);

    parallel_for_each(   
        // Define the compute domain, which is the set of threads that are created.
        sum.extent,          
        // Define the code to run on each thread on the accelerator.
        [=](index<1> idx) restrict(amp)
        {
            sum[idx] = a[idx] + b[idx];
        }
    );

    // Print the results. The output is "7, 9, 11, 13, 15".
    for (int i = 0; i < 5; i++) {
        std::cout << sum[i] << "\n";
    }
}

Did you find this helpful?
(1500 characters remaining)