尝试在 class 中编译内核,得到“__init__() 得到一个意外的关键字参数 'kernel'”错误
Trying to compile kernel in a class, getting "__init__() got an unexpected keyword argument 'kernel'" error
我正在开发一个 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 中,但它不起作用。请帮忙!!
错误已经说明了,赋值 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()
我正在开发一个 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 中,但它不起作用。请帮忙!!
错误已经说明了,赋值 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()