I have a fairly simple problem but I cannot figure out an elegant solution to it.
I have a Thrust code which produces c
vectors of same size containing values. Let say each of these c
vectors have an index. I would like for each vector position to get the index of the c
vector for which the value is the lowest:
Example:
C0 = (0,10,20,3,40)
C1 = (1,2 ,3 ,5,10)
I would get as result a vector containing the index of the C
vector which has the lowest value:
result = (0,1 ,1 ,0,1)
I have thought about doing it using thrust zip iterators, but have come accross issues: I could zip all the c
vectors and implement an arbitrary transformation which takes a tuple and returns the index of its lowest value, but:
- How to iterate over the contents of a tuple?
- As I understand tuples can only store up to
10
elements and there can be much more than 10
c
vectors.
I have then thought about doing it this way: Instead of having c
separate vectors, append them all in a single vector C
, then generate keys referencing the positions and perform a stable sort by key which will regroup the vector entries from a same position together. In the example that would give:
C = (0,10,20,3,40,1,2,3,5,10)
keys = (0,1 ,2 ,3,4 ,0,1,2,3,4 )
after stable sort by key:
output = (0,1,10,2,20,3,3,5,40,10)
keys = (0,0,1 ,1,2 ,2,3,3,4 ,4 )
Then generate keys with the positions in the vector, zip the output with the index of the c
vectors and then perform a reduce by key with a custom functor which for each reduction outputs the index with the lowest value. In the example:
input = (0,1,10,2,20,3,3,5,40,10)
indexes= (0,1,0 ,1,0 ,1,0,1,0 ,1)
keys = (0,0,1 ,1,2 ,2,3,3,4 ,4)
after reduce by keys on zipped input and indexes:
output = (0,1,1,0,1)
However, how to write such functor for the reduce by key operation?
See Question&Answers more detail:
os