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 8240901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:39:13+00:00 2026-06-07T20:39:13+00:00

Possible Duplicate: Counting occurences of numbers in cuda array Is there a way to

  • 0

Possible Duplicate:
Counting occurences of numbers in cuda array

Is there a way to use Thrust or CUDA to count occurrences for the duplicates in an array?

For example:
If I have a device vector

{11, 11, 9, 1, 3, 11, 1, 2, 9, 1, 11}

I should get

1:3, 2:1, 3:1, 9:2, 11:4

If Thrust cannot do that, how can I use a kernel to do that?

Thanks! I am doing a concentration calculation. That’s why I am asking this question.
Assume that there are 100000 particles in the domain which has nx * ny * nz cells, I need to calculate the concentration of each cell (how many particles in each cell).

My kernel is this

__global__ void concentration_kernel(float3* posPtr, uint* device_cons) 
{
    __shared__ uint cache[256];
    uint x = threadIdx.x + blockIdx.x * blockDim.x;
    uint y = threadIdx.y + blockIdx.y * blockDim.y;
    uint offset = x + y * blockDim.x * gridDim.x; 

    float3 posf3 = posPtr[offset];//make_float3(43.5,55,0.66);//
    uint cellIndex = (uint)(posf3.z+1)*153*110 + (uint)(posf3.y)*153 + (uint)posf3.x;

    cache[threadIdx.x] = device_cons[cellIndex];
    __syncthreads();
    uint a = cache[threadIdx.x];
    a++;
    cache[threadIdx.x] = a;
    __syncthreads();

    device_cons[cellIndex] = cache[threadIdx.x]; 
}
  • 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-06-07T20:39:14+00:00Added an answer on June 7, 2026 at 8:39 pm

    You can first sort the vector using thrust::sort and then use thrust::reduce_by_key. However, you also need to create a new vector (called values) of 1‘s (and of the same length as your sorted vector) after sort. These values will be added up to get the counts:

    reduce_by_key is a generalization of reduce to key-value pairs.
    For each group of consecutive keys in the range [keys_first, keys_last)
    that are equal, reduce_by_key copies the first element of the group to
    the keys_output. The corresponding values in the range are reduced using
    the plus and the result copied to values_output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Counting Line Numbers in Eclipse How Can I count the number of
Possible Duplicate: Counting inversions in an array This is a programming question that I
Possible Duplicate: Counting inversions in an array This is an phone interview question :
Possible Duplicate: Is there any way to clone a git repository’s sub-directory only? clone
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there

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.