首页 > 解决方案 > 试图在一个类中编译内核,得到“__init__() got an unexpected keyword argument 'kernel'”错误

问题描述

我正在开发一个 openCL 项目来执行高效的矩阵和矢量产品。

我正在使用该__init__方法,因为我正在使用 openCL。我创建了上下文并设置了设备,但是当我尝试编译内核时遇到了 python 错误。到目前为止,这是我的代码:

import numpy as np
import pyopencl as cl

class gigatron(object):

    def __init__(self):
        ctx=cl.create_some_context()
        device=ctx.devices[0]
        queue=cl.CommandQueue(ctx,properties=cl.command_queue_properties.PROFILING_ENABLE)
        mf=cl.mem_flags

        #initialize CSR matrix and vector
        data=np.array([1, 2, 3, 4, 5, 6],dtype=np.float32)
        indices=np.array([0, 2, 2, 0, 1, 2],dtype=np.int32)
        indptr=np.array([0, 2, 3, 6],dtype=np.int32)
        vec=np.array([1,1,1],dtype=np.float32)
        matrix_shape=(3,3)
        size=np.array(vec.shape,dtype=np.int32)
        size_buf=cl.Buffer(ctx,mf.READ_ONLY|mf.COPY_HOST_PTR,hostbuf=size)

        #load above variables into CPU buffer
        data_buf=cl.Buffer(ctx,mf.READ_ONLY|mf.COPY_HOST_PTR,hostbuf=data)
        indices_buf=cl.Buffer(ctx,mf.READ_ONLY|mf.COPY_HOST_PTR,hostbuf=indices)
        indptr_buf=cl.Buffer(ctx,mf.READ_ONLY|mf.COPY_HOST_PTR,hostbuf=indptr)
        vec_buf=cl.Buffer(ctx,mf.READ_ONLY|mf.COPY_HOST_PTR,hostbuf=vec)
        out_buf=cl.Buffer(ctx,mf.WRITE_ONLY,vec.nbytes)
        #os.environ['PYOPENCL_COMPILER_OUTPUT'] = '1'

        prg = cl.Program(ctx,kernel="""
        __kernel void adder(const __global int *size,
                    const __global float *data,
                    const __global int *indices,
                    const __global int *indptr,
                    const __global float *vec,
                    __global float *out)
        {
        int i = get_global_id(0);
        if(i<size[0])
        {
        float dot=0.0;
        int nvals = indptr[i+1]-indptr[i];
        int start = indptr[i];
        int end = indptr[i+1];
        for(int j = start; j<end; j++)
        {
        dot+=data[j]*vec[indices[j]];
        }
        out[i]=dot;
        }
        }
        """).build()

        event=adder(queue,matrix_shape,None,size_buf,data_buf,indices_buf,indptr_buf,vec_buf,out_buf)
        event.wait()
        # create output array to copy buffer to 
        output=np.zeros(vec.shape,dtype=np.float32)

        # copy to output 
        cl.enqueue_copy(queue,output,out_buf)

        # print output 
        print(output)   

x = gigatron()

问题是当我尝试构建它时,它给了我以下错误:

___init___() got an unexpected keyword argument 'kernel'

我不确定如何解决这个问题。我尝试将内核作为参数放入 init 中,但它不起作用。请帮忙!!

标签: pythonc++cnumpypyopencl

解决方案


错误说得够多了,赋值kernel=在函数参数中不起作用。要么删除它

prg = cl.Program(ctx,"""
    __kernel void adder(const __global int *size,
                const __global float *data,
                const __global int *indices,
                const __global int *indptr,
                const __global float *vec,
                __global float *out)
    {
    int i = get_global_id(0);
    if(i<size[0])
    {
    float dot=0.0;
    int nvals = indptr[i+1]-indptr[i];
    int start = indptr[i];
    int end = indptr[i+1];
    for(int j = start; j<end; j++)
    {
    dot+=data[j]*vec[indices[j]];
    }
    out[i]=dot;
    }
    }
    """).build()

或者在调用之前分配变量cl.Program

kernel = """
    __kernel void adder(const __global int *size,
                const __global float *data,
                const __global int *indices,
                const __global int *indptr,
                const __global float *vec,
                __global float *out)
    {
    int i = get_global_id(0);
    if(i<size[0])
    {
    float dot=0.0;
    int nvals = indptr[i+1]-indptr[i];
    int start = indptr[i];
    int end = indptr[i+1];
    for(int j = start; j<end; j++)
    {
    dot+=data[j]*vec[indices[j]];
    }
    out[i]=dot;
    }
    }
    """
prg = cl.Program(ctx, kernel).build()

推荐阅读