# GPU vs. CPU sorting how to time sorting on both devices using ArrayFire library

Anyone has any idea how to declare host matrix on CPU and sort its rows along with indices using ArrayFire library.
For instance,

A =
[ 6 3 9 2;
7 2 8 3;
6 4 5 2;
9 1 2 8 ]

Row-wise sorted
sorted =
[ 2 3 6 9;
2 3 7 8;
2 4 5 6;
1 2 8 9 ]

ArrayFire is a GPU library. To compare against the CPU, you would need to write your own C code (or otherwise find another sorting library for the CPU).

There is an OpenCL version of ArrayFire available (which will support both GPU and CPU execution), but currently the OpenCL version does not have sorting and we recommend using the CUDA version.

Good luck!

Ok. Thanks.

Is there any way to use randu for integers. i tried Array A = randu(10,10,u32) but its saying.

terminate called after throwing an instance of ‘af::exception’

Aborted

While using randu(10,10) is working perfectly fine; returning floats.

-haaji

``````float hA[] = {6,7,6,9, 3,2,4,1, 9,8,5,2, 2,3,2,8}; // can also be int, double, ..

array A(hA,4,4);

print(sort(A,1)); // sort along dimension=1  (sort rows)
``````

Check out the online documentation of sort for more details and options.

We haven’t yet implemented randu() or integers, but in the mean time, try something like this with the build you have currently:

``````array a = convert(10 * randu(10,10), u32);
``````

Side note: ArrayFire 1.1 will have an API change and deprecate “convert(A,type)” to instead use “A.as(type)” similar to Python.

Also, in general, best to wrap ArrayFire code in standard C++ try/catch to see what the exception abort indicates.

``````try {

} catch (af::exception &e) {

fprintf(stderr, __FILE__":%d: %s\n", __LINE__, e.what());

throw; // rethrow exception

}
``````

But, I checked myself and the error message wasn’t very helpful (indicated unsupported type #10). I fixed it to now show this:

``````src/gena/gi_arith.cpp:236: error: unsupported type (u32) in rand
``````

But I also jumped in to add integer support. Since random integers aren’t really uniform or normal (every bit is simply random 0,1), I named it just ‘rand’ instead of ‘randu’ or ‘randn’. This will be out in the coming days in the nightly builds but definitely when we roll out ArrayFire 1.1 with CUDA 4.1 support next week.

``````rand(1,5,s32) =

-1115749450 -339329097 167591721 -133303299 -321518802

rand(1,5,u32) =

2866113984 472148682 2019573489 2204150021 3333646895
``````

Let us know if there’s any other features you need. Thanks for your patience and feedback.

James