#include <CL/cl.h>
#include <iostream>
#include <string>
#include <fstream>
#pragma comment(lib, "OpenCL.lib")
const char * loadfile(const char * fileName)
{
std::ifstream fs(fileName, std::ios::binary);
fs.seekg(, std::ios::end); int size = fs.tellg();
char * data = new char[size + ];
fs.seekg();
fs.read(data, size);
fs.close();
data[size] = ;
return data;
} int main()
{
cl_platform_id platform;
clGetPlatformIDs(, &platform, NULL); cl_device_id device;
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, , &device, NULL); cl_context context = clCreateContext(NULL, , &device, NULL, NULL, NULL);
cl_command_queue queue = clCreateCommandQueue(context, device, , NULL); const char * clSourceFile = loadfile("H:/QtTool/build/TritonRayTracing/kernel.txt");
cl_program program = clCreateProgramWithSource(context, , &clSourceFile, NULL, NULL);
cl_int result = clBuildProgram(program, , &device, NULL, NULL, NULL);
if (result)
{
std::cout << "Error buring compilation" << std::endl;
}
cl_kernel kernel = clCreateKernel(program, "main", NULL);
cl_mem output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, * sizeof(cl_int), NULL, );
cl_mem buffer1 = clCreateBuffer(context, CL_MEM_READ_WRITE, * sizeof(cl_int), NULL, );
cl_mem buffer2 = clCreateBuffer(context, CL_MEM_READ_WRITE, * sizeof(cl_int), NULL, );
clSetKernelArg(kernel, , sizeof(output), (void *)&output);
clSetKernelArg(kernel, , sizeof(buffer1), (void *)&buffer1);
clSetKernelArg(kernel, , sizeof(buffer2), (void *)&buffer2);
cl_int * buffer1Ptr = (cl_int *)clEnqueueMapBuffer(queue,
buffer1,
CL_TRUE,
CL_MAP_WRITE,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
cl_int * buffer2Ptr = (cl_int *)clEnqueueMapBuffer(queue,
buffer2,
CL_TRUE,
CL_MAP_WRITE,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
for (int i = ; i < ; ++i)
{
buffer1Ptr[i] = i;
buffer2Ptr[i] = i;
}
clEnqueueUnmapMemObject(queue, buffer1, buffer1Ptr, , , );
clEnqueueUnmapMemObject(queue, buffer2, buffer2Ptr, , , );
size_t global_work_size = ;
clEnqueueNDRangeKernel(queue,
kernel,
,
NULL,
&global_work_size,
NULL, , NULL, NULL);
cl_int * resultBufferPtr = (cl_int *)clEnqueueMapBuffer(queue,
output,
CL_TRUE,
CL_MAP_READ,
,
* sizeof(cl_int),
, NULL, NULL, NULL);
for (int i = ; i < ; i++)
{
std::cout << "ptr[" << i << "] = " << resultBufferPtr[i] << std::endl;
}
return ;
}

OpenCL( 一)的更多相关文章

  1. 基于SoCkit的opencl实验1-基础例程

    基于SoCkit的opencl实验1-基础例程 准备软硬件 Arrow SoCkit Board 4GB or larger microSD Card Quartus II v14.1 SoCEDS ...

  2. OPenCL

    OpenCLhttp://baike.baidu.com/link?url=7uHWCVUYB3Sau_xh3OOKP-A08_IvmT1SJixdAXKezCuCfkzeSQDiSmesGyVGk8 ...

  3. Opencl 并行求和

    上周尝试用opencl求极大值,在网上查到大多是求和,所谓的reduction算法.不过思路是一样的. CPP: ; unsigned ; ; ; int nGroup = nGroupSize / ...

  4. opencl初体验

    总结一下,opencl的步骤差不多是这些 先要获取平台的id clGetPlatformIDs(nPlatforms, platform_id, &num_of_platforms) 然后获取 ...

  5. Altera OpenCL用于计算机领域的13个经典案例(转)

    英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...

  6. 面向OPENCL的ALTERA SDK

    面向OPENCL的ALTERA SDK 使用面向开放计算语言 (OpenCL™) 的 Altera® SDK,用户可以抽象出传统的硬件 FPGA 开发流程,采用更快.更高层面的软件开发流程.在基于 x ...

  7. OpenCV GPU CUDA OpenCL 配置

    首先,正确安装OpenCV,并且通过测试. 我理解GPU的环境配置由3个主要步骤构成. 1. 生成关联文件,即makefile或工程文件 2. 编译生成与使用硬件相关的库文件,包括动态.静态库文件. ...

  8. CUDA/OpenCL 学习资料

    VS2010 NVIDIA OpenCL 开发环境配置 CUDA 在线课程 [经典培训] 全球首套中文CUDA 教程-胡文美教授主讲

  9. opencl 学习资源

    1.AMD  opencl-optimization-guide http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated ...

  10. opencl gauss filter优化(三)

    1.根据前两次的最终结果: 使用普通buffer,Horizontal 5ms, Vertical 17 ms 使用image buffer:Horizontal 9.4ms, Vertical 6. ...

随机推荐

  1. STM32驱动ht1621b显示LCD

    这几天在写ht1621b显示LCD的程序,主芯片是Stm32f10的芯片.对于stm32和ht1621b的运用和操作本人是新手,属于赶鸭子上架,通过查看datasheet等资料和网上查看前人写的程序终 ...

  2. 自己封装的tc

    封装弹窗 html <head> <script src="jquery.js"></script> <script src=" ...

  3. HTML,CSS,JS,JQ

    CSS: <style> <!--属性选择器--> .container input[type="text"][name="txt"]{ ...

  4. myeclipse启动服务器时,tomcat出错问题

    启动服务器,错误提示: Launching web on myeclipse tomcat has encountered a problem An internal error occurred d ...

  5. HTML a标签 target属性作用

    特殊的目标 有 4 个保留的目标名称用作特殊的文档重定向操作: _blank 浏览器总在一个新打开.未命名的窗口中载入目标文档. _self 这个目标的值对所有没有指定目标的 <a> 标签 ...

  6. java中system.out.println()是什么意思

    在Java编程中,我们常常用System.out.println()方法来输出字符串,也许我们都已经猜到println()是方法名,但System是什么,out又是什么呢?这里就涉及用到一个stati ...

  7. 用sql实现汉字转拼音

    有时我们会需要将汉字转为拼音,例如需要将省市转为拼音后当做编码存储(尽管国家有统一的标识码,但有时候我们还是会用到),网络上也有工具提供汉字转拼音的功能,但各有优劣,一般转拼音后还会存在带声调的字母, ...

  8. 生成元(Digit Generator,ACM/ICPC Seoul 2005,UVa 1583)

    #include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int t, n, a, ...

  9. linux的用户,群组和其他用户

    1 linux 安全模型:(多任务,多用户的操作系统) 1)使用user和group控制使用者对文件的存储权限. 2)用户使用账户和口令登录linux 3)每个文件都有owner(创建者),owner ...

  10. 线段树解决leetcode307. Range Sum Query - Mutable

    在下面的图中,求[0,0]和[1,1]区间元素和, 对于[0,0] start =0 ,end = 0, 查到root = 4 [0-1]的节点,mid = 0, 此时mid > end-1,表 ...