2

尝试使用与我在使用 TBB(线程构建块)运行时使用的代码(某种)相同的代码。

我对 OpenCL 没有太多经验,但我认为大部分主要代码都是正确的。我相信错误在.cl文件中,它在那里进行数学运算。

这是我在 TBB 中的 mandelbrot 代码:

曼德布洛特待定

这是我在 OpenCL 中的代码

曼德布洛特 OpenCL

任何帮助将不胜感激。

4

2 回答 2

2

我更改了内核中的代码,它运行良好。我的新内核代码如下:

// voronoi kernels

//
// local memory version
//
kernel void voronoiL(write_only image2d_t outputImage)
{
    // get id of element in array
    int x = get_global_id(0);
    int y = get_global_id(1);
    int w = get_global_size(0);
    int h = get_global_size(1);

    float4 result = (float4)(0.0f,0.0f,0.0f,1.0f);
    float MinRe = -2.0f;
    float MaxRe = 1.0f;
    float MinIm = -1.5f;
    float MaxIm = MinIm+(MaxRe-MinRe)*h/w;
    float Re_factor = (MaxRe-MinRe)/(w-1);
    float Im_factor = (MaxIm-MinIm)/(h-1);
    float MaxIterations = 50;


    //C imaginary
    float c_im = MaxIm - y*Im_factor;

    //C real
    float c_re = MinRe + x*Re_factor;

    //Z real
    float Z_re = c_re, Z_im = c_im;

    bool isInside = true;
    bool col2 = false;
    bool col3 = false;
    int iteration =0;

    for(int n=0; n<MaxIterations; n++)
    {
        // Z - real and imaginary
        float Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;

        //if Z real squared plus Z imaginary squared is greater than c squared
        if(Z_re2 + Z_im2 > 4)
        {
            if(n >= 0 && n <= (MaxIterations/2-1))
            {
                col2 = true;
                isInside = false;
                break;
            }
            else if(n >= MaxIterations/2 && n <= MaxIterations-1)
            {
                col3 = true;
                isInside = false;
                break;
            }
        }
        Z_im = 2*Z_re*Z_im + c_im;
        Z_re = Z_re2 - Z_im2 + c_re;
        iteration++;
    }
    if(col2) 
    { 
        result = (float4)(iteration*0.05f,0.0f, 0.0f, 1.0f);
    }
    else if(col3)
    {
        result = (float4)(255, iteration*0.05f, iteration*0.05f, 1.0f);
    }
    else if(isInside)
    {
        result = (float4)(0.0f, 0.0f, 0.0f, 1.0f);
    }


    write_imagef(outputImage, (int2)(x, y), result);
}

你也可以在这里找到它:

https://docs.google.com/file/d/0B6DBARvnB__iUjNSTWJubFhUSDA/edit

于 2013-04-20T00:48:28.923 回答
1

请参阅此链接。它由@eric-bainville 开发。本机和 OpenCL 的 CPU 代码都不是最佳的(它不使用 SSE/AVX),但我认为 GPU 代码可能很好。对于 CPU,您可以通过使用 AVX 并一次在八个像素上运行来大大加快代码速度。

http://www.bealto.com/mp-mandelbrot.html

于 2013-04-15T07:52:30.583 回答