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

  • SEARCH
  • Home
  • 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 7058101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:04:21+00:00 2026-05-28T04:04:21+00:00

Say I have a vector of keys thrust::device_vector<int> keys(10); keys[0] = 51; // —–>

  • 0

Say I have a vector of keys

thrust::device_vector<int> keys(10); 
keys[0] = 51; // -----> 
keys[1] = 51; 
keys[2] = 72; // -----> 
keys[3] = 72; 
keys[4] = 72; 
keys[5] = 103; //-----> 
keys[6] = 103; 
keys[7] = 504; // ------> 
keys[8] = 504 
keys[9] = 504 ; 

I already know before hand that there are 4 distinct key values in
this vector. I want to populate the two device arrays
pidx[4] and pnum[4].

  1. The pidx array gives me the first position of each distinct key in the
    keys vector, namely the positions marked with ----> in the code snippet above. So, in this example, I should have pidx[4] = {0, 2, 5, 7}.

  2. The pnum array gives me the number of occurrences of each key. So, in this example, I should have
    pnum[4] = {2, 3, 2, 3} .

How would one perform the above operation with CUDA Thrust?

  • 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-28T04:04:22+00:00Added an answer on May 28, 2026 at 4:04 am

    This is not the optimal solution, but I can’t figure out a better way.

    // Use `unique` to grab the distinct values
    thrust::device_vector<int> values(4);
    thrust::unique_copy( keys.begin(), keys.end(), values.begin() );
    
    // For each of the values use `count` to get the frequencies
    for ( int i = 4; i != 0; --i )
        pnum[i] = thrust::count( keys.begin(), keys.end(), values[i] );
    
    // Use prefix sum to get the indices
    thrust::exclusive_scan( pnum.begin(), pnum.end(), pidx.begin() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a vector of int which I've prefilled with 100 elements
Say I have the following: std::vector<int> myints; and then I have a function that
Let's say I have a loop like this: vector<shared_ptr<someStruct>> vec; int i = 0;
Let's say I have: vector<T *> vect; I know it's not the obvious way
Let's say I have a function: typedef std::vector<int> VecType; VecType randomVector(); int processing() {
In R, let's say we have a vector area = c(rep(c(26:30), 5), rep(c(500:504), 5),
Lets say we have the following code: std::vector<int> f() { std::vector<int> y; ... return
Say I have something like: #include <vector> #include <algorithm> #include <tr1/functional> void bar(int value)
What is the behavior of std::advance when you have say: std::vector<int> foo(10,10); auto i
I have a vector of vectors, say std::vector< std::vector<int> > my2dArray; Now I would

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.