Skip to content

set_intersection improperly compares keys not in the input #822

Open

Description

@hwinkler

Bug in set_intersection using keys that are structs, only for THRUST_DEVICE_SYSTEM_CUDA.
(It works fine in THRUST_DEVICE_SYSTEM_CPP).
The assert in Key::operator<() will fail.
That means one of the two Keys it is comparing has been constructed with the default constructor.
But this example ensures the input keys it passes to set_intersection were initialized
to a different value. set_intersection ought never compare against any default constructed
Keys.

(I think!)

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/set_operations.h>

// Bug in `set_intersection` using keys that are `struct`s, only for THRUST_DEVICE_SYSTEM_CUDA.
// (It works fine in THRUST_DEVICE_SYSTEM_CPP).
// The `assert` in Key::operator<() will fail.
// That means one of the two Keys it is comparing has been constructed with the default constructor.
// But this example ensures the input keys it passes to `set_intersection` were initialized
// to a different value. `set_intersection` ought never compare against any default constructed
// Keys.

struct Key
{
    int some_member;

    __host__ __device__ Key();              // some_member <- 0
    __host__ __device__ Key(int s);         // some_member <- 1

    bool __host__ __device__ operator<(const Key& b) const;
};

__host__ __device__ Key::Key()
  : some_member(0)
{
}

__host__ __device__ Key::Key(int)
  : some_member(1)
{
}

bool __host__ __device__ Key::operator<(const Key& b) const
{
    if (this->some_member != b.some_member) {
        assert(this->some_member == b.some_member);
        printf("some_member mismatch %d %d\n", this->some_member, b.some_member);
    }
    // all keys are equal for our purposes:
    return false;
}

auto doSetIntersectionByKey()
{
    // This Key will have `some_member` initialized to 1:
    Key hostKey(1);

    Key hostKeysA[] = { hostKey, hostKey, hostKey, hostKey };
    Key hostKeysB[] = { hostKey, hostKey, hostKey, hostKey };

    thrust::device_vector<Key> aKey(hostKeysA, hostKeysA + 4);
    thrust::device_vector<Key> bKey(hostKeysB, hostKeysB + 4);

    thrust::device_vector<Key> outKeys(4);

    // The two input vectors have Keys all of whose `some_member` is 1.
    // Yet when compiled for CUDA backend, the assertion in
    // operator<() fails. When compiled for CPP, the assertion passes.
    // Under CUDA, set_intersection must be comparing to Key instances constructed
    // using the default constructor (or else to uninitialized Key instances).
    return thrust::set_intersection(
      aKey.begin(), aKey.end(), bKey.begin(), bKey.end(), outKeys.begin());
}

int main()
{
    // Dereferencing just to force optimized builds not to eliminate code.
    Key x = *doSetIntersectionByKey();
    return (x.some_member) ? 0 : 1;
}

to build:

echo Building repro-cuda-debug 
/usr/local/cuda-11.7/bin/nvcc \
    -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA \
    -g -G -O0 \
    -std=c++17 \
    -arch=sm_70 -gencode=arch=compute_86,code=sm_86 \
    -x cu \
    -o "repro-cuda-debug" \
    "set_intersection_repro.cu"

Activity

  1. jrhemstad commented on Oct 13, 2022

    @jrhemstad
    Collaborator

    This sounds a lot like #789 and #780

    I think there are CUDA algorithm implementations within Thrust where threads in a block will erroneously do things like default construct objects or read past boundaries even if they don't ultimately participate in the work of the algorithm which can ultimately lead to problems like this.

  2. hwinkler commented on Oct 13, 2022

    @hwinkler
    Author

    I think there are CUDA algorithm implementations within Thrust where threads in a block will erroneously do things like default construct objects or read past boundaries even if they don't ultimately participate in the work of the algorithm which can ultimately lead to problems like this.

    That was my hope, but I get different answers (not in this example, but in the development code I was working on) in the CPP case than in the CUDA case. Man, I hope I don't have to make that repro. :)

  3. hwinkler commented on Oct 13, 2022

    @hwinkler
    Author

    In the development code I was working on, I returned something arbitrary from operator<() when one of the two Keys was default constructed. In that case I was using set_intersection_by_key and the output values in the CUDA case differed from the CPP case. So I think the non-participation of those comparisons in the work of the algorithm might be suspect.

  4. jrhemstad commented on Oct 13, 2022

    @jrhemstad
    Collaborator

    Right, I believe there are bugs in the CUDA implementations where threads that should not be participating are actually participating.

  5. hwinkler commented on Oct 17, 2022

    @hwinkler
    Author

    After further work with my development code, I don't have any evidence that the actual sorted results come out incorrectly. The only "problem" is that these default-constructed Keys which I did not create or pass to set_intersection actually get passed to operator<(). That is surprising, but for my case, not an actual problem. I can see it being a problem for some implementations, though. I have no objection to closing this issue. Maybe it's not a bug but a documentation problem? Should all Thrust client callbacks be prepared to be called on objects that were default constructed and never passed intentionally as Thrust function inputs?

  6. jrhemstad commented on Nov 8, 2022

    @jrhemstad
    Collaborator

    After further work with my development code, I don't have any evidence that the actual sorted results come out incorrectly. The only "problem" is that these default-constructed Keys which I did not create or pass to set_intersection actually get passed to operator<(). That is surprising, but for my case, not an actual problem. I can see it being a problem for some implementations, though. I have no objection to closing this issue. Maybe it's not a bug but a documentation problem? Should all Thrust client callbacks be prepared to be called on objects that were default constructed and never passed intentionally as Thrust function inputs?

    It's definitely a bug and is consistent with the other issues I linked. I still suspect there is a problem with out of bounds elements being default initialized and passed to the binary operator, but don't actually participate in the final result.

  7. added
    thrust
    on Feb 22, 2023
  8. transferred this issue fromNVIDIA/thruston Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    thrust

    Type

    No type

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions