如何制作从 Python 到 pycuda 的 Julia 集

问题描述

我在 Python 中使用 Pycuda 制作了一组 Julia。 但是,出现错误。我不知道错误的原因。

代码如下: 导入 ctypes 将 numpy 导入为 np 导入 cv2 导入 pycuda.driver 作为 cuda 导入 pycuda.autoinit 导入 numpy 从 pycuda.compiler 导入 SourceModule

bmp = np.zeros((1000,1000),dtype=np.float32)
ptr = bmp.ctypes.data_as(ctypes.c_void_p)
bmp = bmp.astype(numpy.float32)
a_gpu = cuda.mem_alloc(bmp.nbytes)
cuda.memcpy_htod(a_gpu,bmp)

mod = SourceModule(""" 
    struct cuComplex {
        float r; float i;
        __device__ cuComplex(float a,float b) : r(a),i(b) {}
        __device__ float magnitude2(void) { return r * r + i * i; }
        __device__ cuComplex operator* (const cuComplex& a){
            return cuComplex(r * a.r - i * a.i,i * a.r + r * a.i);}
        __device__ cuComplex operator+ (const cuComplex& a){
            return cuComplex(r + a.r,i + a.i);}
    };    
    __device__ int julia(int x,int y){
        const float scale = 1.5;
        float jx = scale * (float)(1000/2-x)/(1000/2);
        float jy = scale * (float)(1000/2-y)/(1000/2);
        
        cuComplex c(-0.8,0.156);
        cuComplex a(jx,jy);
        
        for (int i = 0;i<200;i++){
            a = a * a + c;
            if(a.magnitude2()>1000)
                return 0;
        }
        return 1;
    }
    __global__ void juliaSet(float *ptr)
    {
        int x = blockIdx.x;
        int y = blockIdx.y;
        int offset = x + y * gridDim.x;
        
        int juliaValue = julia(x,y);
        ptr[offset*4+0]=255*juliaValue;
        ptr[offset*4+1]=0;
        ptr[offset*4+2]=0;
        ptr[offset*4+3]=255;
    }
    """)
func = mod.get_function("juliaSet")
bmp_kernel = numpy.empty_like(bmp)
func(a_gpu,block=(1000,1000,1))
cuda.memcpy_dtoh(bmp_kernel,a_gpu)
cv2.imshow('bmp',bmp)
cv2.waitKey()

错误是:

Traceback (most recent call last):

File "C:\Users\hyeri\PycharmProjects\pythonProject\JuliaSet.py",line 55,in <module>

  func(a_gpu,1))

File "C:\Users\hyeri\AppData\Local\conda\conda\envs\pycuda\lib\site-packages\pycuda\driver.py",line 480,in function_call

  func._set_block_shape(*block)

pycuda._driver.LogicError: cuFuncSetBlockShape failed: invalid argument

我需要帮助...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...