Repository navigation
Description
Is your feature request related to a problem? Please describe.
As @vyasr pointed out in #9666 (comment), row_equality_comparator and row_lexicographic_comparator are not commutative, i.e., op(i,j) !==> op(j,i). (Edit: Obviously < is not commutative, but it still has the same kind of problem because there is implicit meaning to the values that will cause failures if violated)
Both of these operators work on two tables, a lhs and rhs. For two ids i and j op(i,j) assumes i references into row i of lhs and j into rhs. Thus, op(i,j) is not the same as op(j,i).
For example, imagine doing a binary search for the rows of table B into table A
table_device_view A{...}, B{...};
row_lexicographic_comparator less_compare{A, B};
thrust::binary_search( counting_iterator{0}, counting_iterator{A.size()}
counting_iterator{0}, counting_iterator{B.size()}
less_compare);
This uses counting iterators in combination with the row_lexicographic_comparator to perform a binary search of row i from B into A.
Invoking less_compare(i,j) it implicitly assumes that i references table A (the lhs) and j references table B (the rhs). But there is absolutely zero guarantee from the binary_search algorithm that it would only use values from the first input range for the first argument and values from the second range for the second argument. In other words, less_compare(i,j) could be invoked where i indexes into B and j indexes into A. This would perform an incorrect comparison and silently produce incorrect results.
Concrete example of this in action with std::merge: https://godbolt.org/z/j5z5oG87j
Describe the solution you'd like
We should leverage strong typing to solve this problem.
In short, define a strong type for a lhs_index_t and rhs_index_t and provide two overloads of the row operators binary operator() that accept arguments of these types in swapped ordering. These strongly typed overloads can simply unwrap the strong type wrapper and call an internal overload that works on just size_t indices:
enum class lhs_index_t : int32_t{};
enum class rhs_index_t : int32_t{};
struct device_row_comparator{
// Maps the strong type values to the correct order for the implicitly ordered size_t overload
bool operator()(lhs_index_t l, rhs_index_t r){
return this->operator()(static_cast<int32_t>(l), static_cast<int32_t>(r));
}
// Maps the strong type values to the correct order for the implicitly ordered size_t overload
bool operator()(rhs_index_t r, lhs_index_t l){
return this->operator()(static_cast<int32_t>(l), static_cast<int32_t>(r));
}
private:
// Assumes lhs_index references lhs, and likewise with rhs_index and rhs
bool operator()(size_t lhs_index, size_t rhs_index){ }
table_device_view lhs, rhs;
}
I don't believe a thrust::counting_iterator will work as-is with the lhs_index_t/rhs_index_t as defined above. We may need to provide a convenience wrapper for making counting iterators for these strong types, or making them actual structs with appropriate operator+ to make it work with counting_iterator by default.
Describe alternatives you've considered
There really isn't an alternative. We've only survived this far by relying on implementation defined behavior that algorithms aren't switching the order of arguments on us.
Additional Context
Using the strongly typed indices will require modifying algorithms to use these new strong types. This change should be made in conjunction with the updating to the new comparators added in #10164.
This issue will block updating an algorithm that expects to perform comparisons between two different tables, e.g., #9452.
Activity
jrhemstad commentedon Mar 24, 2022
I suggest alongside implementing this change we update an existing algorithm to use the new strongly typed logic.
cudf::mergewould be an excellent candidate as it currently encodes the "side" information as runtime values: https://github.com/rapidsai/cudf/blob/57ff6f55b9fd44e8a8e10282d3f95d5f38e299ef/cpp/src/merge/merge.cu#L143-L180Using strong types for the input indices would be a good way to simplify the logic.
jrhemstad commentedon Mar 24, 2022
cudf::searchwould be another good exemplar algorithm.vyasr commentedon Mar 24, 2022
Thanks for writing this issue up! I think I've left inline comments about this in a couple of places and we've discussed possible solutions before, but I hadn't gotten around to writing up a full proposal.
It may also be worthwhile to test with at least one algorithm using cuco. The changes associated with #10401 will likely run into these kinds noncommutativity bugs.
jrhemstad commentedon Apr 14, 2022
Hey, turns out we had an issue for this a long time ago! #3257
I knew it sounded familiar :)
github-actions commentedon May 14, 2022
This issue has been labeled
inactive-30ddue to no recent activity in the past 30 days. Please close this issue if no further response or action is needed. Otherwise, please respond with a comment indicating any updates or changes to the original issue and/or confirm this issue still needs to be addressed. This issue will be labeledinactive-90dif there is no activity in the next 60 days.Two-table comparators with strong index types (#10730)