Python 试图在类中编译内核,得到__init__()得到一个意外的关键字参数';内核&x27&引用;错误
我正在从事一个openCL项目,以执行高效的矩阵和向量积 我使用的是Python 试图在类中编译内核,得到__init__()得到一个意外的关键字参数';内核&x27&引用;错误,python,c++,c,numpy,pyopencl,Python,C++,C,Numpy,Pyopencl,我正在从事一个openCL项目,以执行高效的矩阵和向量积 我使用的是\uuu init\uu方法,因为我使用的是openCL。我创建了上下文并设置了设备,但当我试图编译内核时,遇到了python错误。 这是我目前的代码: import numpy as np import pyopencl as cl class gigatron(object): def __init__(self): ctx=cl.create_some_context() dev
\uuu init\uu
方法,因为我使用的是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中把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()
这就解决了问题。非常感谢!
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()
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()