I have 1D array "A" which is composed from many arrays "a" like this :
I'm implementing a code to sum up non consecutive segments ( sum up the numbers in the segments of the same color of each array "a" in "A" as follow:
Any ideas to do that efficiently with thrust?
Thank you very much
Note: The pictures represents only one array "a". The big array "A" contains many arrays "a"
In the general case, where the ordering of the data and grouping by segments is not known in advance, the general suggestion is to use
thrust::sort_by_key
to group like segments together, and then usethrust::reduce_by_key
to sum the segments. Examples are given here.However, if the input data segments follow a known repeating pattern, such as is suggested here, we can eliminate the sorting step by using a
thrust::permutation_iterator
to "gather" the like segments together, as the input tothrust::reduce_by_key
.Using the example data in the question, the hard part of this is to create the permutation iterator. For that, and using the specific number of segment types (3), segment lengths (3) and number of segments per segment type (3) given in the question, we need a map "vector" (i.e. iterator) for our permutation iterator that has the following sequence:
This sequence would then "map" or rearrange the input array, so that all like segments are grouped together. I'm sure there are various ways to create such a sequence, but the approach I chose is as follows. We will start with the standard counting iterator sequence, and then apply a transform functor to it (using
make_transform_iterator
), so that we create the above sequence. I chose to do it using the following method, arranged in a stepwise sequence showing the components that are added together:Here is a fully worked example: