The reason why you are observing all 0
's is that you are generating a large number of random numbers, i.e., 32 << 20 = 33554432
, between 0
and RAND_MAX
, you are orderning them, but you are displaying only 32
of them.
As mentioned by Robert Crovella, on a Windows machine (the OP is working on Windows), RAND_MAX = 2^15-1 = 32767
. Accordingly, you are generating 33554432
integers between 0
and 32767
, which means that you will have a large number of 0
's in the original array and so all 0
's in the first 32
numbers of the sorted array.
I have personally verifyed that this occurs for both, Windows 32
and 64
bit machines, that is, on both Windows 32
and 64
bit systems RAND_MAX = 32767
.
Again, as pointed out by Robert, this effect will show on Linux 32
bit machines, but not on Linux 64
bit machines, for which RAND_MAX = 2^31-1
since, for that case, RAND_MAX
is much larger than 32 << 20
.
As suggested by Robert, one may change the instruction
thrust::host_vector<int> h_vec(32 << 20);
to
thrust::host_vector<int> h_vec(min(32 << 20,RAND_MAX));
to avoid the all 0
's show.