pyplot为什么将opencl计算结果显示为黑屏?

时间:2018-11-11 17:15:47

标签: python matplotlib opencl pyopencl mandelbrot

我正在用OpenCL编写mandelbrot集计算程序,以了解有关它的信息。但是计算结果有点奇怪。我有一个计算集合的函数:

def cl_mandelbrot(ray, maxiter, ctx):
program = cl.Program(ctx, """
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__kernel void m_brot(__global float2 *arr,
                     __global ushort *output,
                     ushort const maxiters)
{
    int gid = get_global_id(0);
    float nreal, real = 0;
    float imag = 0;
    output[gid] = 0;
    for(int curiter = 0; curiter < maxiters; curiter++) {
        nreal = real*real - imag*imag + arr[gid].x;
        imag = 2* real*imag + arr[gid].y;
        real = nreal;
        if (real*real + imag*imag > 4.0f){
             output[gid] = curiter;
             break;
        }
    }
 }   
""").build()

output = np.empty(arr.shape, dtype=np.uint8)

queue = cl.CommandQueue(ctx)
mem_flags = cl.mem_flags

arr_buf = cl.Buffer(ctx, mem_flags.COPY_HOST_PTR | mem_flags.READ_ONLY, hostbuf=ray)
out_buf = cl.Buffer(ctx, mem_flags.WRITE_ONLY, output.nbytes)

program.m_brot(queue, output.shape, None, arr_buf, out_buf, np.uint16(maxiter))
cl.enqueue_copy(queue, output, out_buf).wait()

return output

将参数传递给cl_mandelbrot()函数的函数:

def fillarr_gpu(xmax, xmin, ymax, ymin, iter, width, height, ctx):
lx = np.linspace(xmin, xmax, width, dtype=np.float64)
ly = np.linspace(ymin, ymax, height, dtype=np.float64)
c = lx + ly[:, None]*1j
c = np.ravel(c)
print(c[1])
res = cl_mandelbrot(c, iter, ctx)
print(res.size)
res = res.reshape((width, height))
return res

问题出在这些功能上。 Here's the result drawen with pyplot

0 个答案:

没有答案