【问题标题】:thrust::sort_by_key is much slower than qsort推力::sort_by_key 比 qsort 慢得多
【发布时间】:2017-04-13 03:27:54
【问题描述】:

我发现推力::sort_by_key 比 qsort 慢很多,让我感到困惑的是并行排序性能低,为什么?

数据集是 100。 qsort 时间为 0.000026(s)。 GPU_sort 时间为 0.000912(s)。

数据集是 1000。 qsort 时间为 0.000205。 GPU_sort 时间为 0.003177。

数据集是 10000。 qsort 时间为 0.001598。 GPU_sort 时间为 0.031547。

数据集是 100000。 qsort 时间为 0.018564。 GPU_sort 时间为 0.31230。

数据集是 1000000。 qsort 时间为 0.219892。 GPU_sort 时间为 3.138608。

数据集是 10000000。 qsort 时间为 2.581469。 GPU_sort 时间为 85.456543。

这是我的代码:

struct HashValue{
int id_;
float proj_;
 };

int HashValueQsortComp(const void* e1, const void* e2)                      

{

int ret = 0;

HashValue* value1 = (HashValue *) e1;

HashValue* value2 = (HashValue *) e2;

if (value1->proj_ < value2->proj_) {
    ret = -1;
} else if (value1->proj_ > value2->proj_) {
    ret = 1;
} else {
    if (value1->id_ < value2->id_) ret = -1;
    else if (value1->id_ > value2->id_) ret = 1;
}
return ret;
}


const int N = 10;

void sort_test()
{

clock_t start_time = (clock_t)-1.0;
clock_t end_Time = (clock_t)-1.0;

HashValue *hashValue = new HashValue[N];
srand((unsigned)time(NULL));

for(int i=0; i < N; i++)
{
    hashValue[i].id_ = i;
    hashValue[i].proj_ = rand()/(float)(RAND_MAX/1000);
}

start_time = clock();
qsort(hashValue, N, sizeof(HashValue), HashValueQsortComp);
end_Time = clock();
printf("The qsort time is %.6f\n", ((float)end_Time - start_time)/CLOCKS_PER_SEC);

float *keys = new float[N];
int *values = new int[N];
for(int i=0; i<N; i++)
{
    keys[i] = hashValue[i].proj_;
    values[i] = hashValue[i].id_;
}
start_time = clock();
thrust::sort_by_key(keys, keys+N, values);
end_Time = clock();
printf("The GPU_sort time is %.6f\n", ((float)end_Time - start_time)/CLOCKS_PER_SEC);

delete[] hashValue;
hashValue = NULL;

delete[] keys;
keys = NULL;

delete[] values;
values = NULL;
}

【问题讨论】:

  • 设备是K40。而我的cpu是1200.468 MHz,GenuineIntel
  • 你明白推力排序不在 GPU 上运行吗?

标签: sorting cuda qsort


【解决方案1】:

您传递给推力排序的变量(keysvalues):

thrust::sort_by_key(keys, keys+N, values);

主机变量。这意味着 thrust will dispatch the host path 用于算法,它不在 GPU 上运行。请参阅thrust quickstart guide 以了解有关推力的更多信息,here 是一个将推力与设备变量一起使用的工作示例。

显然,主机调度的推力排序比您的qsort 实现慢。如果您使用设备路径(并且仅对推力排序操作进行计时),它可能会更快。

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2011-10-27
    相关资源
    最近更新 更多