【发布时间】:2014-11-08 10:08:59
【问题描述】:
有很多 4 元素、6 元素(输入)... 16 输入排序网络,但我需要 32 输入版本 拥有 32x32 剪切排序算法(我计划作为 Opencl 辅助功能)拥有 1024x1024 剪切排序 opencl 算法。如何导出我的 32 输入排序网络?
- 也许是某种进化算法可以最大限度地减少交换次数,我在 opencl 代码中使用它?
- 有固定的规则吗?
-
还是只是通过反复试验发现的?
Input array: 1M elements ----> 1024x1024 2D matrix with inverted odd-rows (shear) each row(1024) of matrix --------> 32 x 32 2D matrix (shear) 32 element row ---------> Sorting (network) Each thread computes one row of 1024 elements. So only 1024 threads for 1M element array.
我计划在网络中使用的非发散比较是:
if(a>b) // where a and b are between 0 and 16M
swap(a,b)
becomes
a0=a; b0=b; // saving
c = a-b
d = !(sign bit of c) (0 for negative, 1 for positive)
tmp=b*d; //tmp=a if a>b otherwise 0
a=a*d //a=b if a>b otherwise 0
b=tmp*d; //b=tmp if a>b otherwise 0
// a0 is backup of a, b0 is backup of b
e = (sign bit of c) (1 for negative, 0 for positive)
tmp0=a0*e; //tmp0=a0 if a0<=b0 otherwise 0
a0=b0*e //a0=b0 if a0<=b0 otherwise 0
b0=tmp0*e; //b0=tmp0 if a0<=b0 otherwise 0
aOut=a+a0; // only a or a0 can be different than zero
bOut=b+b0; // only b or b0 can be different than zero
我确定这不是最快的,但我需要进行快速简单的排序以尝试我的粒子约束求解器,该求解器为固定空间索引(网格)尖叫排序,我有 1M 粒子并尝试剪切网络排序的剪切.
为了验证剪切排序,我在每个线程的基础上实现了 32 输入排序串行双调排序器,以构建每列和行排序的 32x32 矩阵。所以 32x32 = 1024 元素排序需要 9 毫秒,这对于 32 核 @ 700 MHz 来说太慢了。
1024 元素排序需要 9 毫秒,每次 1024 排序后至少需要 20 次迭代才能对 1M 数组进行排序。即使它得到 90 毫秒,这对于仅按键来说也太慢了。会有很多值绑定到键上。(超过 100 个)
尝试用冒泡排序代替双调,得到了 10 毫秒,所以问题一定出在剪切排序实现中吗?
【问题讨论】:
标签: sorting optimization comparison shearsort sorting-network