# sort each row in a matrix?

**URL:** <https://forums.developer.nvidia.com/t/sort-each-row-in-a-matrix/26824>\
**Category:** CUDA Programming and Performance\
**Created:** [June 5, 2012, 12:02am UTC](https://forums.developer.nvidia.com/t/sort-each-row-in-a-matrix/26824 "2012-06-05T00:02:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhaojuanmao](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zhaojuanmao](https://forums.developer.nvidia.com/u/zhaojuanmao)\
**Post date:** [June 5, 2012, 12:02am UTC](https://forums.developer.nvidia.com/t/sort-each-row-in-a-matrix/26824/1 "2012-06-05T00:02:32Z")

</div>

Are there CUDA library to sort each row in a matrix simultaneously?

if I use thrust sort library, I have to sequentially call thrust sort function to sort each row, if there are 10000 rows, I need to make 10000 sequential calls. This is not efficient.

Any help on this?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![struct](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/struct/32/10034_2.png) [@struct](https://forums.developer.nvidia.com/u/struct)\
**Post date:** [June 5, 2012, 5:47pm UTC](https://forums.developer.nvidia.com/t/sort-each-row-in-a-matrix/26824/2 "2012-06-05T17:47:58Z")

</div>

Hi Zhao,

You can try sort in ArrayFire.

Here is a snippet of code that can help understand it:

```auto
array a = randu(4, 3); // random matrix of 4 rows and 3 columns

array b = sort(a, 0); // Sort along the columns

array c = sort(a, 1); // Sort along the rows

print(a);

print(b);

print(c);

```

outputs

```auto
a =

           0.7402 0.9251 0.4702 

           0.9210 0.4464 0.5132 

           0.0390 0.6673 0.7762 

           0.9690 0.1099 0.2948 

b =

           0.0390 0.1099 0.2948 

           0.7402 0.4464 0.4702 

           0.9210 0.6673 0.5132 

           0.9690 0.9251 0.7762 

c =

           0.4702 0.7402 0.9251 

           0.4464 0.5132 0.9210 

           0.0390 0.6673 0.7762 

           0.1099 0.2948 0.9690

```

---

<div class="post-metadata">

**Author:** ![zhaojuanmao](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@zhaojuanmao](https://forums.developer.nvidia.com/u/zhaojuanmao)\
**Post date:** [June 5, 2012, 8:15pm UTC](https://forums.developer.nvidia.com/t/sort-each-row-in-a-matrix/26824/3 "2012-06-05T20:15:18Z")

</div>

thanks for your reply!

Actually, I’ve tried ArrayFire and installed successfully.

However, when I tried example codes, it always gave “segmentation fault” info. No clue about it.

I run ArrayFire on a GPU cluster through Putty~

> [@](#):
>
> Hi Zhao,
> 
> You can try sort in ArrayFire.
> 
> Here is a snippet of code that can help understand it:
> 
> ```auto
> array a = randu(4, 3); // random matrix of 4 rows and 3 columns
> 
> array b = sort(a, 0); // Sort along the columns
> 
> array c = sort(a, 1); // Sort along the rows
> 
> print(a);
> 
> print(b);
> 
> print(c);
> 
> ```
> 
> outputs
> 
> ```auto
> a =
> 
> 0.7402 0.9251 0.4702 
> 
> 0.9210 0.4464 0.5132 
> 
> 0.0390 0.6673 0.7762 
> 
> 0.9690 0.1099 0.2948 
> 
> b =
> 
> 0.0390 0.1099 0.2948 
> 
> 0.7402 0.4464 0.4702 
> 
> 0.9210 0.6673 0.5132 
> 
> 0.9690 0.9251 0.7762 
> 
> c =
> 
> 0.4702 0.7402 0.9251 
> 
> 0.4464 0.5132 0.9210 
> 
> 0.0390 0.6673 0.7762 
> 
> 0.1099 0.2948 0.9690
> 
> ```
