I have run the following Thrust example for sorting. The problem is that after the thrust::sort
, the output contains all 0
's.
Please, tell me what is wrong here.
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/sort.h>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void)
{
thrust::host_vector<int> h_vec(32 << 20);
thrust::generate(h_vec.begin(), h_vec.end(), rand);
thrust::device_vector<int> d_vec=h_vec;
for(int i = 0; i<32;i++)
cout<<d_vec[i]<<endl;
cout<<endl<<endl<<endl;
thrust::sort(d_vec.begin(), d_vec.end());
for(int i = 0; i<32;i++)
cout<<d_vec[i]<<endl;
cout<<endl<<endl<<endl;
thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
for(int i = 0; i<32;i++)
cout<<h_vec[i]<<endl;
return 0;
}
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.