Skip to content

inclusive_scan_by_keys calls the 'sum' operator needlessly #801

Open

Description

@elikl

inclusive_scan_by_keys is called with a one-element keys vector. It should just copy the first input element to the output vector but in addition it invokes the 'sum' operator 4096 times!

Reproduction code (bug.cu)

#include <thrust/device_vector.h>
#include <thrust/scan.h>

#include <stdio.h>

class Agg {
public:
    __device__ int operator()(int x, int y) {
        printf("%d %d\n", x, y);
        return x + y;
    }
};

int main(int argc, char** argv) {
    auto partitions = thrust::device_vector<int>(1, 0);
    auto in = thrust::device_vector<int>(1, 0);
    auto out = thrust::device_vector<int>(1, 0);

    thrust::inclusive_scan_by_key(
        partitions.begin(),
        partitions.end(),
        in.begin(),
        out.begin(),
        thrust::equal_to<int>(),
        Agg()
    );
    return 0;
}


Build

nvcc --std=c++14 -o bug bug.cu

Environment

CentOS 7 (Kernel 3.10.0-1160.el7.x86_64)

NVIDIA-SMI 470.82.01
Driver Version: 470.82.01
CUDA Version: 11.4

GPU: NVIDIA GeForce RTX 2060

Activity

  1. elikl commented on Mar 23, 2022

    @elikl
    Author

    Note: In our real world application this behavior causes illegal cuda memory access.

  2. reopened this on Mar 24, 2022
  3. lilohuang commented on Mar 25, 2022

    @lilohuang

    @allisonvacanti same issue on thrust::inclusive_scan, even reduce_by_key. I think it's because cub::BlockScan doesn't support passing valid_items like cub::BlockLoad and cub::BlockStore, so for those out-of-bound items (could be uninitialized value) will still be combined in ScanOp.

    cc @senior-zero

    #include <thrust/device_vector.h>
    #include <thrust/scan.h>
    #include <thrust/execution_policy.h>
    #include <stdio.h>
    
    class ScanOp {
    public:
       __device__ int operator()(int x, int y) {
          printf("%d %d\n", x, y);
          return x + y;
       }
    };
    
    int main(int argc, char** argv) {
       auto in = thrust::device_vector<int>(1, 0);
       auto out = thrust::device_vector<int>(1, 0);
    
       thrust::inclusive_scan(
          thrust::device,
          in.begin(),
          in.end(),
          out.begin(),
          ScanOp()
       );
    
       return 0;
    }
  4. alliepiper commented on Mar 25, 2022

    @alliepiper
    Contributor

    That sounds about right, thanks for the report and initial investigation! I can't give an ETA at the moment, but this is something we should fix in CUB.

  5. elikl commented on Mar 31, 2022

    @elikl
    Author

    @allisonvacanti looks related to #789 and #780

  6. jrhemstad commented on Mar 31, 2022

    @jrhemstad
    Collaborator

    @allisonvacanti looks related to #789 and #780

    Yeah, I thought this sounded familiar. I think this is a duplicate of those issues.

  7. elikl commented on Mar 31, 2022

    @elikl
    Author

    Yeah, I thought this sounded familiar. I think this is a duplicate of those issues.

    @jrhemstad Note that both #789 and #780 got 'unverified' tags and here we have a clear reproduction.

  8. alliepiper commented on Apr 4, 2022

    @alliepiper
    Contributor

    They're not quite duplicates since they are in different algorithms, so let's leave the others open to track what needs fixing.

  9. added
    thrust
    on Feb 22, 2023
  10. 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

Labels

thrust

Type

No type

Projects

  • Status
    Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions