我的源代码来自异构计算和OpenCL,第4章基本的OpenCL示例>图像旋转。这本书遗漏了几个关键的细节。
我的主要问题是,我不知道如何初始化我提供给它们内核的数组(它们不告诉您如何初始化)。我拥有的是:
int W = inImage.width();
int H = inImage.height();
float *myImage = new float[W*H];
for(int row = 0; row < H; row++)
for(int col = 0; col < W; col++)
myImage[row*W+col] = col;我提供给这个内核:
__kernel void img_rotate(__global float* dest_data, __global float* src_data, int W, int H, float sinTheta, float cosTheta)
{
const int ix = get_global_id(0);
const int iy = get_global_id(1);
float x0 = W/2.0f;
float y0 = H/2.0f;
float xoff = ix-x0;
float yoff = iy-y0;
int xpos = (int)(xoff*cosTheta + yoff*sinTheta + x0);
int ypos = (int)(yoff*cosTheta - xoff*sinTheta + y0);
if(((int)xpos>=0) && ((int)xpos < W) && ((int)ypos>=0) && ((int)ypos<H))
{
dest_data[iy*W+ix] = src_data[ypos*W+xpos];
//dest_data[iy*W+ix] = src_data[iy*W+ix];
}
}我也很难找到正确的θ值。整数是θ的合适值,对吗?
float theta = 45; // 45 degrees, right?
float cos_theta = cos(theta);
float sin_theta = sin(theta);发布于 2013-07-30 18:54:30
在编写OpenCL代码时,我总是将每个内核视为读取一组3D数据,不管数据是1D、2D还是3D:
__kernel void TestKernel(__global float *Data){
k = get_global_id(0); //also z
j = get_global_id(1); //also y
i = get_global_id(2); //also x
//Convert 3D to 1D
int linear_coord = i + get_global_size(0)*j + get_global_size(0)*get_global_size(1)*k;
//do stuff
}在执行clEnqueueNDKernelRange(.)时,只需将维度设置为:
int X = 500;
int Y = 300;
int Z = 1;
size_t GlobalDim = {Z, Y, X};这就让我所有的内核都能在所有维度上轻松地工作。
https://stackoverflow.com/questions/17954147
复制相似问题