# Sorting of 2D vector

I have a 2D vector of char[32] that I need to sort. On the CPU it takes 9 sec and I want to use the GPU instead. thrust::host_vector<char[32]> h_vec; does compile but

thrust::host_vector<int[2]> h_vec;
int a[2];
a[0] = 1;
a[1] = 2;
h_vec.push_back(a); //does not compile

Is there some way?

use pointer arithmetic. but first compare speed of CPU sorting with PCI-E memory bandwidth - with 15 GB/s it may be not fast enough to outperform CPU

I’m sorry, I’m no good att C++. How would I do that?

The vector to sort is only 1.5 MB. Previously in the program I have moved the vector several times back and forth between CPU and GPU in fractions of seconds. I don’t know why the sorting takes so long time.
Someone else who can show me how to use pointer arithmetics in this case.

Well, not the vector, but the data, as a 1D vector.

i’m sorry for going into your way, but my uneducated guess is that by using the same pointer arithmetic, you can make it 1000x faster on CPU. It’s just about million comparisons of 32-byte entities

You are not in my way at all. I’m grateful for every suggestion. I’ll just as well do the sorting on the CPU. I just don’t know how. If you can point me to some tutorial…?

That is the thing I realy do not understand with C++. I use std::sort(Text2D32.begin(), Text2D32.end()); to sort my vector. If there is a way to do it 1000x times faster WHY is std::sort not that way and simply 1000x faster??? Why do I have to reinvent an algorithm a million of programmers must have used a billion of times before me?

well, there are skills that are easy to learn, so they have huge competition and low salaries. there are skills that are hard to learn that results in good salaries. obviously, thing that are easy to learn can’t make you good profits. if you want to became well-paid C++ programmer, you have no choice but going hard way.

in this case, your call is absolutely polite, so the problem is in objects being sorted. my guess is that each data moving involves memory allocation, but i don’t want to research it for you. why the hell?? try to find someone else if your prefer to improve your social rather than technical skills :)

sorting strings with thrust:

https://stackoverflow.com/questions/24126610/thrust-cuda-allocating-char-to-device-vector-of-objects

that is just one possible method. I’m sure there are many other possibilities. Also note that doesn’t actually sort the strings, it just gives the sorted order. If you want to actually sort the strings (i.e. move them into sorted order) than can be done with thrust::copy

Thank you txbob! Again! Just what I need.

Hi Bulat. The treacherous swamps of assumption. I’m neither a low nor a high payed programmer. I’m no programmer at all. I know a little C# and write a prototype to something that might become a non-profit NGO open-source project, if the prototype works. Since the program has to plow through a large body of text and analyze it, I need something fast. I started with MySql and it took a week to analyze a text of a million letters (like a regular novel). I skipped the database and used C# and arrays and brought it down to 20 min. And now I struggle with C++ and Cuda and it looks as if the program can analyze that text in some 10 seconds if I get the sorting right. If I had only a fraction of the knowledge you guys have…

Also note that the link I gave:

1. Is for variable-length strings. If you know that all the strings you want to sort are exactly 32 chars in length, there may be significant optimization possible.

2. may not be very optimal (apart from its use of thrust). Using a for-loop in the functor to compare strings character-by-character seems more-or-less necessary, but the access mechanism may be something that could be optimized.

Things like the number of strings you have to sort, and the min/max length of each string could be important factors to consider when looking for a fast method.