# Implement an optimal algorithm for counting characters in a large amount of text Or count the number

Hi.

Tell me how to implement an optimal algorithm for counting characters in a large amount of text on the CUDA C. Or count the number of unsigned char and unsigned short. Maybe it’s a fairly simple example showing the acceleration of the GPU.

Now I’m using CUDA Thrust to accelerate the standard algorithms: sorting and binary search. But this is clearly not the best option.

``````thrust::sort(vec_sort.begin(), vec_sort.end() );

thrust::sequence(alphabet_temp.begin(), alphabet_temp.end());

thrust::upper_bound(vec_sort.begin(), vec_sort.end(),

alphabet_temp.begin(), alphabet_temp.end(),

alphabet_count.begin());

// Now the desired result in the alphabet_count
``````

Perhaps such an algorithm is already implemented. But I could not find it.

Can anybody give a prompt?

An approach that is probably faster for small alphabets (like 26) is to do a reduction on a vector of 26 values (represent as 7 int4s).

So if we had an alphabet of only 4 letters (a, b, c, d) then

a = (1, 0, 0, 0)

b = (0, 1, 0, 0)

c = (0, 0, 1, 0)

d = (0, 0, 0, 1)

and if you do that reduction then the final vector will contain the counts that you seek.

In general what you are trying to do is a histogram and a search of the forums for that term or google will provide with some more info.