Flattening works by lifting functions to operate on arrays instead of on single values. For example, a function
is lifted to a function
. This means an expression
can be replaced with an application of the lifted function:
. Intuitively, flattening thus works by replacing all function applications with applications of the corresponding lifted function.
After flattening, arrays are represented as single-dimensional value vector V containing scalar elements, alongside auxiliary information recording the nested structure, typically in the form of a boolean flag vector F. The flag vector indicates, for the corresponding element in the value vector, whether it is the beginning of a new segment. For example, the two-dimensional irregular array
can be represented as the data vector
alongside the flag vector
.
This flag vector is necessary in order to correctly flatten nested parallelism. For example, it is used in the flattening of prefix sum to segmented scan.
Flattening can increase the asymptotic work and space complexity of the original program, leading to a much less efficient result.[3]