简体   繁体   English

CUDA - 推力::在设备上排序只返回 0

[英]CUDA - thrust::sort on device returns only 0's

I have run the following Thrust example for sorting.我已经运行以下 Thrust 示例进行排序。 The problem is that after the thrust::sort , the output contains all 0 's.问题是,在thrust::sort ,输出包含所有0

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, ie, 32 << 20 = 33554432 , between 0 and RAND_MAX , you are orderning them, but you are displaying only 32 of them.您观察所有0的原因是您正在生成大量随机数,即32 << 20 = 33554432 ,介于0RAND_MAX之间,您正在对它们进行排序,但您只显示其中的32

As mentioned by Robert Crovella, on a Windows machine (the OP is working on Windows), RAND_MAX = 2^15-1 = 32767 .正如 Robert Crovella 所提到的,在 Windows 机器上(OP 正在 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.因此,您将生成032767之间的33554432整数,这意味着原始数组中将有大量0 ,因此排序数组的前32数字中全部为0

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 .我已经亲自验证过,这在 Windows 3264位机器上都会发生,也就是说,在 Windows 3264位系统上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 .同样,正如 Robert 所指出的,这种效果将在 Linux 32位机器上显示,但不会在 Linux 64位机器上显示,对于RAND_MAX = 2^31-1因为在这种情况下, RAND_MAX远大于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.避免全0的显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM