简体   繁体   中英

Using pyclblas for a simple example

I was trying to run a calculation using pyclblas (a python wrapper for clblas), but ran into some trouble.

Here's my code:

# imports (my python is 2.7)
from __future__ import absolute_import, print_function
import numpy as np
import pyopencl as cl
import pyclblas

# create some generic structures according to pyopencl tutorial
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags

# create a vector and a buffer
c_np = np.random.rand(50000).astype(np.float)
c_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=c_np)

# use pyclblas to make a calculation with the vector
res = pyclblas.clblasSscal(len(c_np), 1.0, c_g, 0, 1, queue, None)

This gives me the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "/home/name/.spyder2/.temp.py", line 49, in <module>
    res = pyclblas.clblasSscal(len(c_np), 1.0, c_g, 0, 1, queue, None)
  File "/usr/local/lib/python2.7/dist-packages/pyclblas.py", line 245, in clblasSscal
    return pyclblas_swig.clblasSscal(N, alpha, X, offx, incx, commandQueues, eventWaitList)
TypeError: in method 'clblasSscal', argument 6 of type 'cl_uint'

The documentation says that queue should be pyopencl.CommandQueue, not 'cl_uint'. Does any one know what the problem is? Thanks!

It looks like I'm a bit late, but I'm the developer of the pyclblas package.

What version of the package were you trying to use when it broke? In some of the older versions you needed to wrap the command queue in a list or tuple for the SWIG interface to recognize your input. I've since updated the SWIG interface such that it works if you pass in a command queue directly. The version when you posted this, 0.8.1, also had a bug that kept it from being installed properly. The latest version 0.8.3 seems to install properly from pip and should meet your needs.

However, there were a few bugs in your script that will need to be corrected: np.float needs to be np.float32 (because you're using Sscal instead of Dscal). Also, your c_g needs to be declared READ_WRITE instead of READ_ONLY because the Sscal kernel stores the modified result back into c_g.

This version works correctly for me using a fresh pip install:

#!/usr/bin/env python
import numpy as np
import pyopencl as cl
import pyclblas
import sys

alpha = float(sys.argv[1])

# create some generic structures according to pyopencl tutorial
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags

# create a vector and a buffer
c_np = np.random.rand(50000).astype(np.float32)
c_g = cl.Buffer(ctx, mf.READ_WRITE, size=c_np.nbytes)
cl.enqueue_copy(queue, c_g, c_np)

# use pyclblas to make a calculation with the vector
res = pyclblas.clblasSscal(len(c_np), alpha, c_g, 0, 1, queue, None)



import scipy.linalg.blas
res_np = np.empty_like(c_np)
cl.enqueue_copy(queue, res_np, c_g)
exp_np = np.copy(c_np)
scipy.linalg.blas.sscal(alpha, exp_np)

print np.linalg.norm(c_np), np.linalg.norm(res_np), np.linalg.norm(exp_np)
print np.linalg.norm(exp_np - res_np)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM