__device__ void swap(int& a, int& b) {
int t = a;
a = b;
b = t;
}
__global__ void sort(int* a, int flag_j, int flag_i, int count)
{
unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int tid_comp = tid ^ flag_j;
if (tid > count)
return;
if (tid_comp > tid) {
if ((tid & flag_i) == 0) { //ascending
if (a[tid] > a[tid_comp]) {
swap(a[tid], a[tid_comp]);
}
}
else { //desending
if (a[tid] < a[tid_comp]) {
swap(a[tid], a[tid_comp]);
}
}
}
}
int main()
{
int count = 800;
std::default_random_engine e;
int* a, * b;
cudaMallocHost((void**)&a, sizeof(int) * count);
cudaMallocHost((void**)&b, sizeof(int) * count);
for (int i = 0; i < count; i++)
{
a[i] = e() % 100000;
cuda对global memery的双调排序
最新推荐文章于 2024-06-04 20:51:11 发布
本文介绍了CUDA中使用双调排序对全局内存进行排序的方法,特别针对不定长度的数据进行详细阐述,并提供经验证的有效CUDA代码示例。
摘要由CSDN通过智能技术生成