2

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;
}
6
  • 1
    Please provide a complete code (why eliminate the #include statements? 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. Commented Apr 22, 2014 at 6:09
  • #include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/copy.h> #include <thrust/sort.h> #include <cstdlib> #include "iostream" these are the include statements Commented Apr 22, 2014 at 6:33
  • The GPU is GT 610 And I am using runtime library Multi-Threaded Debug DLL (/MDd) Commented Apr 22, 2014 at 6:39
  • 2
    @RobertCrovella By 32 << 20, the OP is generating 33554432 random numbers, but displaying only the first 32. I suspect that what he is observing is that the first 32 sorted numbers are just 0. 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 when 32 << 20 is changed to 32. In the latter case, I see non-zero numbers. What I do not understand is why you are not observing the same. Commented Apr 22, 2014 at 9:21
  • 1
    @JackOLantern you've identified the issue. rand() returns a number in the range of 0..RAND_MAX On my 64-bit linux machine, RAND_MAX is 2^31-1. On a 32-bit windows machine, RAND_MAX is 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. Commented Apr 22, 2014 at 14:05

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.