OPENCL之学习手记(chapter2)以及平台搭建和程序执行步骤

OpenCL标准(OpenCL standard):

OpenCL定义了含有C++的C的API接口,可以用于绑定第三方语言:Java,python,.NET。OpenCL C严格执行了C99的标准!


OpenCL说明(OpenCL specification):

OpenCL包括四个部分:

1、平台模型:有主机和设备(设备上就是跑OpenCL)。在设备上跑的OpenCL代码就叫做kernels。

2、执行模型:定义了OpenCL在主机上的环境配置以及kernel在设备上如何执行。包括了诸如在主机上创建一个OpenCL项目,提供主机-设备交互,并且定义了在设备上执行kernel时的并发模型。

3、内存模型:定义了kernel使用的抽象内存层次,即使在真的内存架构下。内存模型与GPU内存层次很接近。

4、编程模型:定义了映射到物理硬件的并发模型。


OpenCL的执行模型和kernel:

一个普通的向量加法:

// Perform an element-wise addition of A and B and store in C.
// There are N elements per array.
void vecadd(int *C, int* A, int *B, int N) {
for(int i ¼ 0; i < N; i++) {
C[i] = A[i] + B[i];
}
}

对于多核处理器的做法是:(strip mining)

// Perform and element-wise addition of A and B and store in C.
// There are N elements per array and NP CPU cores.
void vecadd(int *C, int* A, int *B, int N, int NP, int tid) {
int ept ¼ N/NP; // elements per thread
for(int i = tid*ept; i < (tid+1)*ept; i++) {
C[i] = A[i] + B[i];
}
}

OpenCL的做法:(work-item,n维排序)

// Perform an element-wise addition of A and B and store in C
// N work-items will be created to execute this kernel.
__kernel
void vecadd(__global int *C, __global int* A, __global int *B) {
int tid ¼ get_global_id(0); // OpenCL intrinsic function
C[tid] ¼ A[tid] + B[tid];
}

size_t indexSpaceSize[3]={1024, 1, 1}; //size_t就是定义了该维的大小

Work item swith in a work group have a special relationship with one another:They can perform barrier operations to synchronize and they have access to a shared memory address space.

size_t workGroupSize[3]={64, 1, 1};如果每个数组的大小是1024,那么就会有1024/64 = 16个group

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值