# Sorting of 2D vector

**URL:** <https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737>\
**Category:** CUDA Programming and Performance\
**Created:** [August 20, 2017, 6:12am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737 "2017-08-20T06:12:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 6:12am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/1 "2017-08-20T06:12:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BulatZiganshin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BulatZiganshin](https://forums.developer.nvidia.com/u/BulatZiganshin)\
**Post date:** [August 20, 2017, 8:10am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/2 "2017-08-20T08:10:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 9:04am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/3 "2017-08-20T09:04:05Z")

</div>

> [@](#):
>
> use pointer arithmetic

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

---

<div class="post-metadata">

**Author:** ![BulatZiganshin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BulatZiganshin](https://forums.developer.nvidia.com/u/BulatZiganshin)\
**Post date:** [August 20, 2017, 9:26am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/4 "2017-08-20T09:26:36Z")

</div>

sorry, i can’t help. anyway start with the comparison

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 9:46am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/5 "2017-08-20T09:46:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 10:00am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/6 "2017-08-20T10:00:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BulatZiganshin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BulatZiganshin](https://forums.developer.nvidia.com/u/BulatZiganshin)\
**Post date:** [August 20, 2017, 10:09am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/7 "2017-08-20T10:09:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 10:13am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/8 "2017-08-20T10:13:00Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 10:26am UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/9 "2017-08-20T10:26:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![BulatZiganshin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BulatZiganshin](https://forums.developer.nvidia.com/u/BulatZiganshin)\
**Post date:** [August 20, 2017, 12:11pm UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/10 "2017-08-20T12:11:32Z")

</div>

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 :)

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 20, 2017, 2:06pm UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/11 "2017-08-20T14:06:08Z")

</div>

sorting strings with thrust:

[url][c++ - Thrust CUDA allocating char \* to device\_vector of objects - Stack Overflow](https://stackoverflow.com/questions/24126610/thrust-cuda-allocating-char-to-device-vector-of-objects%5B/url%5D)

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

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 7:47pm UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/12 "2017-08-20T19:47:01Z")

</div>

Thank you txbob! Again! Just what I need.

---

<div class="post-metadata">

**Author:** ![Flisp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Flisp](https://forums.developer.nvidia.com/u/Flisp)\
**Post date:** [August 20, 2017, 8:07pm UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/13 "2017-08-20T20:07:06Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 20, 2017, 8:30pm UTC](https://forums.developer.nvidia.com/t/sorting-of-2d-vector/52737/14 "2017-08-20T20:30:25Z")

</div>

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.
