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;
}
#includestatements? That just makes it harder for others to help you.) Please also identify the GPU you are running on and the compile command you are using. Your code runs fine for me, the complete example is here. There may be a problem with the machine you are trying to run it on, or your compile command may be wrong for the GPU you are using.32 << 20, the OP is generating33554432random numbers, but displaying only the first32. I suspect that what he is observing is that the first32sorted numbers are just0. I have tested the OP's code on two Windows 7 systems, both equipped with CUDA 5.5, but using two different cards (GT210 and GT540M having cc 1.2 and 2.1, respectively) and I see the same effect, which disappears when32 << 20is changed to32. In the latter case, I see non-zero numbers. What I do not understand is why you are not observing the same.rand()returns a number in the range of 0..RAND_MAXOn my 64-bit linux machine, RAND_MAX is 2^31-1. On a 32-bit windows machine,RAND_MAXis 2^15-1 (32767). (I haven't tested a 64-bit windows machine, but it would appear to be 32767 also). When RAND_MAX is much larger than 32<<20, then we have a small population of zeros in the produced field of numbers. But when RAND_MAX is much smaller than 32<<20, we have a large population of zeros, which show up at the beginning of the sorted output.