I'm working on an openCL project to perform efficient matrix and vector products.
I'm using the __init__
method since i'm working with openCL. I created my context and set my devices but when I try to compile my kernel I run into a python error.
This is my code so far:
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()
The problem is when I try to build this, it gives me the following error:
___init___() got an unexpected keyword argument 'kernel'
I am unsure of how to fix this. I tried putting in kernel as an argument in init but it doesn't work. Please help!!
The error says enough, the assignment kernel=
doesn't work in function parameters. Either remove it
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()
Or assign the variable before call 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()