【发布时间】: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 上运行吗?