OpenCL 双调排序 CPU 版
▶ 学习了双调排序,参考(https://blog.csdn.net/xbinworld/article/details/76408595)
● 使用 CPU 排序的代码
#include <stdio.h> #define LENGTH 1024
#define ASCENDING 1
#define DESCENDING 0 int a[LENGTH]; void compare(int i, int j, int dir)
{
if (dir == (a[i]>a[j]))
{
int h = a[i];
a[i] = a[j];
a[j] = h;
}
} void bitonicMerge01(int lo, int cnt, int dir)// 先再大跨度(半区间长)上调整元素,再递归地在小跨度上进行相同的调整
{
if (cnt > )
{
int k = cnt / ;
for (int i = lo; i < lo + k; i++)
compare(i, i + k, dir);
bitonicMerge01(lo, k, dir);
bitonicMerge01(lo + k, k, dir);
}
} void bitonicSort01(int lo, int cnt, int dir)// 先递归地要求小跨度区间依次排成 “升↗降↘升↗降↘” 再在较大跨度上进行合并
{
if (cnt > )
{
int k = cnt / ;
bitonicSort01(lo, k, ASCENDING);
bitonicSort01(lo + k, k, DESCENDING);
bitonicMerge01(lo, cnt, dir);
}
} void bitonicMerge02(int l, int r, const int dir)
{
if (r - l > )
{
int stride = (r - l) / + ;
for (int i = l; i < l + stride; i++)
compare(i, i + stride, dir);
bitonicMerge02(l, l + stride - , dir);
bitonicMerge02(l + stride, r, dir);
}
} void bitonicSort02(int l, int r, const int dir)
{
if (r - l > )
{
int rNew = l + (r - l) / ;
bitonicSort02(l, rNew, ASCENDING);
bitonicSort02(rNew + , r, DESCENDING);
bitonicMerge02(l, r, dir);
}
} void bitonicMerge03(int l, int r, const int dir)
{
if (r - l > )
{
int stride = (r - l) / ;
for (int i = l; i < l + stride; i++)
compare(i, i + stride, dir);
bitonicMerge03(l, l + stride, dir);
bitonicMerge03(l + stride, r, dir);
}
} void bitonicSort03(int l, int r, const int dir)
{
if (r - l > )
{
int rNew = l + (r - l) / ;
bitonicSort03(l, rNew, ASCENDING);
bitonicSort03(rNew, r, DESCENDING);
bitonicMerge03(l, r, dir);
}
} int main()
{
int i, error;
srand();
for (i = ; i < LENGTH; a[i++] = rand()); printf("\n");
for (i = ; i < LENGTH; i++)
{
printf("%5d,", a[i]);
if ((i + ) % == )
printf("\n");
} //bitonicSort01(0, LENGTH, ASCENDING); // 使用起点和长度
//bitonicSort02(0, LENGTH - 1, ASCENDING); // 使用左端点和右端点(都包含)
bitonicSort03(, LENGTH, ASCENDING); // 使用左端点和右端点(左包含右不包含) printf("\n");
for (i = , error = -; i < LENGTH; i++)
{
printf("%5d,", a[i]);
if (i < LENGTH - && a[i] > a[i + ])
error = i;
if ((i + ) % == )
printf("\n");
}
if (error != -)
printf("\n\nerror at i==%d, a[i]==%d, a[i+1]==%d", error, a[error], a[error + ]); getchar();
return ;
}
● 输出结果(临时改为排序 64 个元素,每行显示 16个)
, ,,,,,,, ,,,,,,,,
,,, ,,, ,,,,,,,,,,
,, ,,,,,,,, ,,,, ,,
,, ,,,,,,,,,,,,, , , , , , , , , , , , ,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
OpenCL 双调排序 CPU 版的更多相关文章
- OpenCL 双调排序 GPU 版
▶ 参考书中的代码,写了 ● 代码,核函数文件包含三中算法 // kernel.cl __kernel void bitonicSort01(__global uint *data, const ui ...
- DirectX11 With Windows SDK--27 计算着色器:双调排序
前言 上一章我们用一个比较简单的例子来尝试使用计算着色器,但是在看这一章内容之前,你还需要了解下面的内容: 章节 26 计算着色器:入门 深入理解与使用缓冲区资源(结构化缓冲区/有类型缓冲区) Vis ...
- 【转载】双调排序Bitonic Sort,适合并行计算的排序算法
双调排序是data-independent的排序, 即比较顺序与数据无关的排序方法, 特别适合做并行计算,例如用GPU.fpga来计算. 1.双调序列 在了解双调排序算法之前,我们先来看看什么是双调序 ...
- 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 双调排序是data-indepen ...
- tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案
tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案 一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...
- Windows7下安装cpu版的Tensorflow
windows7下安装python3.5 1.下载python-3.5.2-amd64.whl https://www.python.org/downloads/release/python-352/ ...
- 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】
转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...
- Ubuntu16.04下caffe CPU版的详细安装步骤
一.caffe简介 Caffe,是一个兼具表达性.速度和思维模块化的深度学习框架. 由伯克利人工智能研究小组和伯克利视觉和学习中心开发. 虽然其内核是用C++编写的,但Caffe有Python和Mat ...
- CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录
1.首先安装pip-install 在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error ...
随机推荐
- 51Nod:1134 最长递增子序列
动态规划 修改隐藏话题 1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递 ...
- idea快捷键 好的网址收藏
http://blog.csdn.net/u010800804/article/details/48491395http://blog.csdn.net/wei83523408/article/det ...
- svn分支开发注意事项
1.切换的时候最好查看本文件的是主干上的还是分支上的, 单击右键,点击属性,可以看到以下图片,其中"URL"就可以 看到是主干还是分支 2.切换到分支 点击切换后就选择要切换到的路 ...
- WebApi_基于Token的身份验证——JWT
JWT是啥? JWT就是一个字符串,经过加密处理与校验处理的字符串,形式为: A.B.C A由JWT头部信息header加密得到B由JWT用到的身份验证信息json数据加密得到C由A和B加密得到,是校 ...
- java中使用MD5加密的算法
MD5,全名Message Digest Algorithm 5,中文名为消息摘要算法第五版,为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.以下是JAVA语言中使用MD5加密的工具 ...
- linux 知识点
关于登录Linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...
- mysql 中find_in_set()和in()用法比较
mysql 中find_in_set()和in()用法比较 在mysql中in可以包括指定的数字,而find_in_set()用于特定的数据类型. find_in_set 函数使用方法 个例子来说:有 ...
- Android getprop setprop watchprops用法
转载请注明出处:https://www.cnblogs.com/lialong1st/p/10172973.html 在安卓系统中,当你写了一个脚本,已经添加到开机启动 init.rc 中,即使脚本中 ...
- Extjs Column布局常见问题及解决方法
原文地址:http://blog.csdn.net/weoln/article/details/4339533 第一次用Extjs的column布局时遇见了很多问题,记录下来,供大家参考.column ...
- tomcat和servlet的关系
一.什么是servlet? 处理请求和发送响应的过程是由一种叫做Servlet的程序来完成的,并且Servlet是为了解决实现动态页面而衍生的东西.理解这个的前提是了解一些http协议的东西,并且知道 ...