0

双三次插值是一种常见的插值方法,但我在 OpenCL 上找不到任何可行的实现。我决定自己在 OpenCL 上编写双三次插值,但是......

我对内核程序有一些问题。

当我运行内核执行时,程序失败并出现错误 CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST。没有关于错误原因的任何其他信息。我正在使用 google 代码的 javacl 绑定形式:http ://code.google.com/p/javacl,Ubuntu linux 10.10 上的 AMD Accelerated Parallel Processing SDK 2.3,硬件 AMD Radeon 5xxxHD

我还没有在 ubuntu 上打开用于 AMD APP SDK 的调试器(

如果我取消注释 float4 val=read_imagef(signal, sampler, (float2)(x+iX,y+iY)); 并评论双三次插值“float4 val = ...”的计算都没有任何错误(但使用双线性插值)。我认为这个错误是因为无效的内存访问或寄存器内存溢出。

const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_LINEAR | CLK_ADDRESS_CLAMP_TO_EDGE;
const float CATMULL_ROM[16]={-0.5F,1.5F,-1.5F,0.5F,1.0F,-2.5F,2.0F,-0.5F,-0.5F,0.0F,0.5F,0.0F,0.0F,1.0F,0.0F,0.0F};
__kernel void bicubicUpscale(int scale,read_only image2d_t signal, write_only image2d_t upscale) {

int x = get_global_id(0)-2, y = get_global_id(1)-2;

float C[16];
float T[16];


for (int i = 0; i < 16; i++)
{
    C[i]=0.0F;
    T[i]=0.0F;
}

for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++)
        {
            T[4*i+j] += read_imagef(signal, sampler, (int2)(x+k,y+i)).x * CATMULL_ROM[4*j+k];
        }
for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++)
        {
            C[4*i+j] += CATMULL_ROM[4*i+k] * T[4*k+j];
        }

for (int i = 0; i < scale; i++)
{
    for (int j = 0; j < scale; j++)
    {
        float iX=(float)j/(float) scale;
        float iY=(float)i/(float) scale;
        //float4 val=read_imagef(signal, sampler, (float2)(x+iX,y+iY));
        float val= iX * (iX * (iX * (iY * (iY * (iY * C[0] + C[1]) + C[2]) + C[3])
        + (iY * (iY * (iY * C[4] + C[5]) + C[6]) + C[7]))
        + (iY * (iY * (iY * C[8] + C[9]) + C[10]) + C[11]))
        + (iY * (iY * (iY * C[12] + C[13]) + C[14]) + C[15]);
        write_imagef(upscale, (int2)(x*scale+j, y*scale+i), val);
    }
}
}

我重写了这个程序以使用本地内存,但它仍然无法正常工作

const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_LINEAR | CLK_ADDRESS_CLAMP_TO_EDGE;
const float CATMULL_ROM[]={-0.5F,1.5F,-1.5F,0.5F,1.0F,-2.5F,2.0F,-0.5F,-0.5F,0.0F,0.5F,0.0F,0.0F,1.0F,0.0F,0.0F};
__kernel void bicubicUpscale(local float* sharedBuffer,int scale,read_only image2d_t signal, write_only image2d_t upscale) {
int x = get_global_id(0)-2, y = get_global_id(1)-2;
//int locX=get_local_id(0);

int offsetT = (y+2)*512+(x+2)*32+16;
int offsetC = (y+2)*512+(x+2)*32;

global float* C=&sharedBuffer[offsetT];
global float* T=&sharedBuffer[offsetT];

for (int i = 0; i < 32; i++){
    sharedBuffer[offsetC+ i]=0.0F;
}

for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++){
            //T[4*i+j] = mad(read_imagef(signal, sampler, (int2)(x+k,y+i)).x,CATMULL_ROM[4*j+k],T[4*i+j]);
            T[i+j] += read_imagef(signal, sampler, (int2)(x+k,y+i)).x * CATMULL_ROM[4*j+k];
        }
for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++){
            //C[4*i+j] = mad(CATMULL_ROM[4*i+k],T[4*k+j],C[4*i+j]);
            sharedBuffer[offsetC +4*i+j] += CATMULL_ROM[4*i+k] * sharedBuffer[offsetT + 4*k+j];
        }


barrier (CLK_GLOBAL_MEM_FENCE);


for (int i = 0; i < scale; i++)
    for (int j = 0; j < scale; j++)
        {
            float iX=(float)j/(float) scale;
            float iY=(float)i/(float) scale;
            float4 val= iX * (iX * (iX * (iY * (iY * (iY * C[0] + C[1]) + C[2]) + C[3])
            + (iY * (iY * (iY * C[4] + C[5]) + C[6]) + C[7]))
            + (iY * (iY * (iY * C[8] + C[9]) + C[10]) + C[11]))
            + (iY * (iY * (iY * C[12] + C[13]) + C[14]) + C[15]);
            write_imagef(upscale, (int2)(x*scale+j, y*scale+i), val);
        }
}

你知道这个问题的任何决定。

Java 源代码 + maven2 构建。使用命令“mvn clean compile exec:java”编译并运行demo。

问候,伊戈尔

4

1 回答 1

1

我修好了!从性能的角度来看,这个内核并不是最优的,但功能上是正确的。

请对 enqueueNDRange 使用此类参数:

            kernelBicubic.getKernel().setArgs(scaleFactor, inImage, imageOut);
            lastEvent=kernelBicubic.getKernel().enqueueNDRange(queue,
                    new int[]{(int) inImage.getWidth()+1,(int) inImage.getHeight()+1},lastEvent);

内核代码:

const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_LINEAR | CLK_ADDRESS_CLAMP_TO_EDGE;

const float CATMULL_ROM[16]={-0.5F, 1.5F,-1.5F, 0.5F, 1.0F,-2.5F, 2.0F,-0.5F,-0.5F, 0.0F, 0.5F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F};

inlie float calcT(image2d_t signal,int x,int y,int i,int j){
      return read_imagef(signal, sampler, (int2)(x  ,y+i)).x * CATMULL_ROM[4*j]
            +read_imagef(signal, sampler, (int2)(x+1,y+i)).x * CATMULL_ROM[4*j+1]
            +read_imagef(signal, sampler, (int2)(x+2,y+i)).x * CATMULL_ROM[4*j+2]
            +read_imagef(signal, sampler, (int2)(x+3,y+i)).x * CATMULL_ROM[4*j+3];
}

inline float C(image2d_t signal,int x,int y,int i,int j){
      return CATMULL_ROM[4*i  ] * calcT(signal,x,y,0,j)
            +CATMULL_ROM[4*i+1] * calcT(signal,x,y,1,j)
            +CATMULL_ROM[4*i+2] * calcT(signal,x,y,2,j)
            +CATMULL_ROM[4*i+3] * calcT(signal,x,y,3,j);
}


__kernel void bicubicUpscale(int scale,read_only image2d_t signal, write_only image2d_t upscale) {

int x = get_global_id(0)-2, y = get_global_id(1)-2;

float C0 =C(signal,x,y,0,0);
float C1 =C(signal,x,y,0,1);
float C2 =C(signal,x,y,0,2);
float C3 =C(signal,x,y,0,3);
float C4 =C(signal,x,y,1,0);
float C5 =C(signal,x,y,1,1);
float C6 =C(signal,x,y,1,2);
float C7 =C(signal,x,y,1,3);
float C8 =C(signal,x,y,2,0);
float C9 =C(signal,x,y,2,1);
float C10=C(signal,x,y,2,2);
float C11=C(signal,x,y,2,3);
float C12=C(signal,x,y,3,0);
float C13=C(signal,x,y,3,1);
float C14=C(signal,x,y,3,2);
float C15=C(signal,x,y,3,3);

float xOff=scale*1.5F + x*scale;
float yOff=scale*1.5F + y*scale;

 for (int i = 0; i < scale; i++)
 {
    for (int j = 0; j < scale; j++)
    {
        float iY=(float)j/(float) scale;
        float iX=(float)i/(float) scale;
        float val= iX * (iX * (iX * (iY * (iY * (iY * C0 + C1) + C2) + C3)
        + (iY * (iY * (iY * C4 + C5) + C6) + C7))
        + (iY * (iY * (iY * C8 + C9) + C10) + C11))
        + (iY * (iY * (iY * C12 + C13) + C14) + C15);
        write_imagef(upscale, (int2)(xOff+j, yOff+i), val);
    }
 }
}
于 2011-03-31T05:28:11.787 回答