We have already talked about how array, array_view, texture, and writeonly_texture_view are captured and passed into a parallel_for_each for use on the accelerator_view. In this blog, I’m going to draw your attention to a pattern of data capturing, and explain the caveats associated with it.
Aliased Invocation of parallel_for_each
Let’s first take a look on a typical parallel_for_each invocation:
C++ AMP Compiler analyzes the parallel_for_each invocation, and generates host side code for data marshaling and launching the computation to the accelerator, and the device side code for performing the computation on the accelerator. Note the compiler only inspects the parallel_for_each call and does not try to inspect the rest of the code. Therefore, any compile time known knowledge is limited to what the parallel_for_each invocation can tell. In above example, the compiler would conclude that there are two “arrays” being supplied to parallel_for_each, and generates code assuming the use of two different buffers underlying the arrays. This assumption is actually correct for the example above. However, it may not always be true. For example, I can refactor the parallel_for_each in above example to a function:
Then I can call this function as,
So the first invocation is the same as the original example — the two arrays captured to the parallel_for_each in “assign” are indeed different. However, in the second invocation, the two arrays are the same, thus breaks the assumption that there are two different buffers. You can also see that whether the two arrays captured by reference are the same or different cannot be known at compilation time.
For arrays and textures that are captured by reference, and array_views and writeonly_texture_views that are captured by value, we refer them as captured containers.
We say two captured containers are aliased if a parallel_for_each invocation satisfies the following two conditions:
- if both refer to the same container on the accelerator_view where the parallel_for_each is launched, and
- at least one of the them is writable inside parallel_for_each.
Note if both are only readable (strictly speaking, it’s also a type of aliasing, i.e. read-read aliasing), we don’t consider them as aliased. A parallel_for_each invocation that uses aliased containers is called an aliased invocation.
Here is another example of aliased invocation triggered by using array_views:
Both “first_half” and “second_half” are created using section method of array class. The two array_views represents the two sections of the same underlying container (i.e. “arr3”). In this case, the invocation of parallel_for_each is also an aliased invocation.
We call array_views created directly on top of host memory or host containers as top-level array_views. All top-level array_views are considered distinct when captured to a parallel_for_each. Even though they might be overlapping on the same host memory region, the C++ AMP runtime creates and manages a distinct copy for each of them on the accelerator_view where the parallel_for_each is launched. As a result, top-level array_views do not cause aliased invocations. For example,
Even “av_all” and “av_first_half” overlaps on host container “vec”, the invocation is not an aliased invocation since both are top-level array_views, However, if we change
Then the invocation becomes an aliased one, because “av_first_half” is not a top-level array_view, it uses the same copy of av_all on the accelerator_view.
So now you know what aliased invocation of a parallel_for_each is and you may appreciate the good news that C++ AMP support aliased invocation when capturing array references and array_views. However, there are three caveats, as detailed in the following three sections.
Caveat 1: Performance of aliased kernel
The C++ AMP compiler generates two DirectX compute shaders for a parallel_for_each: one for the non-aliased invocation and the other for the aliased invocation. The C++ AMP runtime examines the input, decide whether there is aliasing, and then selects the corresponding shader to execute.
The non-aliased shader assumes there is no aliasing, thus efficient code is generated. The aliased shader, on the other hand, our current implementation on top of DirectX could be a lot more conservative on code generation, for reasons that are beyond the scope of this post. Depending on the characteristics and complexity of the code, the driver, and the accelerator characteristics, the aliased shader could be much slower than the non-aliased shader. So it’s important to keep this in mind and try to avoid aliased invocations if you can.
When we talked about how to measure the performance of a parallel_for_each invocation, we mentioned that there is a Just-in-time (JIT) compilation overhead for the first time invocation of a parallel_for_each. It’s worth noting that such JIT cost is paid for the non-aliased shader at the first non-aliased invocation, and similarly for the aliased shader at the first aliased invocation.
Caveat 2: Doesn’t work with interop
For arrays created using DirectX Interop, the C++ AMP runtime does not detect whether there is aliasing involving such arrays. If there is no other aliasing, the runtime just selects the non-aliased shader. So this can lead to either runtime exception or undefined result.
Caveat 3: Texture aliasing scenario not supported
…and I call the function with the following code,
Inside “assign”, both “a” and “b” are actually referring to the same “tex1”. In this case, you will get a runtime_exception as
Read/Write aliasing is detected between two textures (texture_view’s). RW-aliasing is not allowed for texture.
Similarly, the following code will also trigger the exception:
This is because “tex2” and “wo_tv2” refer to the same texture object “tex2”. So this is also a case of aliasing.
In this post, I explained the aliased invocation of parallel_for_each, and showed you that C++ AMP does support such aliased invocation with caveats. As usual, you are welcome to ask questions and provide feedback below or on our MSDN Forum.