Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:21:50+00:00 2026-05-26T20:21:50+00:00

I have a sorted integer array on the device, e.g.: [0,0,0,1,1,2,2] And I want

  • 0

I have a sorted integer array on the device, e.g.:

[0,0,0,1,1,2,2]

And I want the offsets to each element in another array:

[0,3,5]

(since the first 0 is at position 0, the first 1 at position 3 and so on)
I know how many different elements there will be beforehand. How would you implement this efficiently in CUDA? I’m not asking for code, but a high level description of the algorithm you would implement to compute this transformation. I already hat a look at the various functions in the thrust name space, but could not think of any combination of thrust functions to achieve this. Also, does this transformation have a widely accepted name?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:21:51+00:00Added an answer on May 26, 2026 at 8:21 pm

    You can solve this in Thrust using thrust::unique_by_key_copy with thrust::counting_iterator. The idea is to treat your integer array as the keys argument to unique_by_key_copy and to use a sequence of ascending integers (i.e., counting_iterator) as the values. unique_by_key_copy will compact the values array into the indices of each unique key:

    #include <thrust/device_vector.h>
    #include <thrust/iterator/counting_iterator.h>
    #include <thrust/iterator/discard_iterator.h>
    #include <thrust/unique.h>
    #include <thrust/copy.h>
    #include <iterator>
    #include <iostream>
    
    int main()
    {
      thrust::device_vector<int> keys(7);
      keys[0] = 0; keys[1] = 0; keys[2] = 0;
      keys[3] = 1; keys[4] = 1; keys[5] = 2; keys[6] = 2;
    
      std::cout << "keys before unique_by_key_copy: [ ";
      thrust::copy(keys.begin(), keys.end(), std::ostream_iterator<int>(std::cout," "));
      std::cout << "]" << std::endl;
    
      thrust::device_vector<int> offsets(3);
    
      thrust::unique_by_key_copy(keys.begin(), keys.end(),          // keys
                                 thrust::make_counting_iterator(0), // [0, 1, 2, 3, ...] are the values
                                 thrust::make_discard_iterator(),   // discard the compacted keys
                                 offsets.begin());                  // the offsets are the values
    
      std::cout << "offsets after unique_by_key_copy: [ ";
      thrust::copy(offsets.begin(), offsets.end(), std::ostream_iterator<int>(std::cout," "));
      std::cout << "]" << std::endl;
    
      return 0;
    }
    

    Here’s the output:

    $ nvcc test.cu -run
    keys before unique_by_key_copy: [ 0 0 0 1 1 2 2 ]
    offsets after unique_by_key_copy: [ 0 3 5 ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1-)For sorted array I have used Binary Search. We know that the worst case
Suppose I have a sorted array of integers int[] , and I want to
I have a N-Ary non sorted in any way tree and each node can
I have a simple one dimmensional array of integer values that represent a physical
I have a list of dates in an array (in unix integer format). I
I have a 2D-array that I want to sort based on the second column.
Let's say I have an array of floating point numbers, in sorted (let's say
I have an array of objects that I need to sort by a position
You are given two sorted integer arrays, which have some common integers. Any common
I have a sorted array of 5000 integers. How fast can I tell if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.