Using pyclblas for a simple example - python

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)

Related

Does scikit-cuda support the newest version of pycuda (9.1) or do I have to revert to 7.5?

I have installed pycuda and scikit cuda via pip install on python 3.6 and have been trying to run the following example from scikit cuda:
from __future__ import print_function
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
import numpy as np
import skcuda.linalg as culinalg
import skcuda.misc as cumisc
culinalg.init()
# Double precision is only supported by devices with compute
# capability >= 1.3:
import string
import scikits.cuda.cula as cula
demo_types = [np.float32, np.complex64]
if cula._libcula_toolkit == 'premium' and \
cumisc.get_compute_capability(pycuda.autoinit.device) >= 1.3:
demo_types.extend([np.float64, np.complex128])
for t in demo_types:
print('Testing svd for type ' + str(np.dtype(t)))
a = np.asarray((np.random.rand(50, 50) - 0.5) / 10, t)
a_gpu = gpuarray.to_gpu(a)
u_gpu, s_gpu, vh_gpu = culinalg.svd(a_gpu)
a_rec = np.dot(u_gpu.get(), np.dot(np.diag(s_gpu.get()), vh_gpu.get()))
print('Success status: ', np.allclose(a, a_rec, atol=1e-3))
print('Maximum error: ', np.max(np.abs(a - a_rec)))
print('')
which then prints:
Traceback (most recent call last):
File "<ipython-input-3-a4adfb24f4ca>", line 8, in <module>
import skcuda.linalg as culinalg
File "C:\Users\moore\downloadsl\Continuum\anaconda3\lib\site-packages\skcuda\linalg.py", line 21, in <module>
from . import cublas
File "C:\Users\moore\downloadsl\Continuum\anaconda3\lib\site-packages\skcuda\cublas.py", line 55, in <module>
raise OSError('cublas library not found')
OSError: cublas library not found
I looked into the cublas.py and there is a list of versions and it only goes upto 7.5, so my question is threefold. Are the newest versions of pycuda and scikit-cuda compatible and if not, are there any analogous libraries for nvidia gpu acceleration of gpu code that is as easy as scikit-cuda, and if not what versions will I have to download in order to make this all work?

Audio analysis with python + octave

i want to analysis audio files with Python and Octave. Acutally i am getting an error
Traceback (most recent call last):
File "C:/Users/peter/PycharmProjects/AudioTools/ShutterTest.py", line 11, in <module>
(peaks, indexes) = octave.findpeaks(np.array(test), 'DoubleSided')
ValueError: not enough values to unpack (expected 2, got 1)
my Code is:
import numpy as np
from matplotlib import pyplot as plt
from scipy.io import wavfile
from oct2py import octave
samplerate, data = wavfile.read("TestWav.wav")
cb = np.array(data, dtype=np.int16)
test =[d[0] for d in cb]
octave.eval("pkg load signal")
(peaks, indexes) = octave.findpeaks(np.array(test), 'DoubleSided')
plt.plot(peaks)
plt.show()
Can someone give an advise?
Thanks
Since the version 4.0.0 of oct2py (see release notes here), it is necessary to provide an nout argument when requiring more outputs.
So in your case you have to do:
(peaks, indexes) = octave.findpeaks(np.array(test), 'DoubleSided', nout=2)

Cannot import mean from statistics

I'm trying to import statistics module in python. It's giving me an error message when i execute the program.
Here is my code:
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
xs = np.array([1,2,3,4,5,6,7,8])
ys = np.array([2,8,5,0,5,7,3,6])
def best_fit_line(xs ,ys):
m = ( ((mean(xs)* mean(ys))- mean(xs*ys)) /
(mean(xs)*mean(xs))-(mean(xs*xs)))
return m
m = best_fit_line(xs,ys)
The error message:
Traceback (most recent call last):
File "/home/kudzai/Documents/Machine Learning/LinearRegAlg.py", line 1, in <module>
from statistics import mean
ImportError: No module named statistics
The statistics module was added in Python 3.4. Perhaps you're using an older Python version.
If you can't upgrade for whatever reason, you can also just use numpy's mean function: np.mean(xs) etc. For numpy arrays, it's probably faster too.

Scipy installation, its installed why the traceback?

I am running the following code (this is the beginning snippet):
# Import necessary modules and functions
########################################
# time module, for timing it.
from time import time
global starttime
starttime = time()
# psutil for indentifying processor and memory usage
#import psutil
# csv module for loading csv files
import csv
# numpy, a standard module for working with arrays
import numpy as np
# for randomly shuffling arrays
from random import shuffle
# for computing mean squared errors
from sklearn import metrics
# importing modules that contain models we will use
from sklearn import linear_model, ensemble, gaussian_process
from sklearn import neural_network, svm
# importing memory profiler to enable us to determine peak memory usage
from memory_profiler import profile
# Helpful functions
###################
#psutilpercent = psutil.virtual_memory()
#print "\n", " --> Memory Check 1 Percent:", str(psutilpercent.percent) + "%\n"
# This function is for simplifying reading CSV files
def readCSV(path):
"""
Read a CSV file of floats, with no headder
"""
data = []
mycsv = csv.reader(open(path), delimiter="|")
for counter, row in enumerate(mycsv):
if counter != 0:
data.append(row)
return np.asarray(data, dtype=np.float32)
However I am receiving the traceback:
Traceback (most recent call last):
File "C:\Users\regression_v6.py", line 38, in <module>
from sklearn import metrics
File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 32, in <module>
from .base import clone
File "C:\Python27\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
ImportError: No module named scipy
But my computer shows Scipy as installed, here is a view of Control Panel\All Control Panel Items\Programs and Features:
Any ideas?
Did you install a special version of scipy? as your screenshot shows 'scipy_umfpack'
I can successfully import sklearn using the official version downloaded from sourceforge: http://sourceforge.net/projects/scipy/files/scipy/

'ValueError: Nothing can be done for the type <class 'numpy.core.records.recarray'> at the moment' error

I am trying to run a simple linear regression (using rpy2 from Python) and encountered a strangely worded error when running the script below:
from numpy import array, rec
from numpy.random import normal as nprandom
from rpy2.robjects import numpy2ri, r
foo = array(range(10))
bar = foo + nprandom(0,1,10)
d = rec.fromarrays([foo, bar], names=('foo','bar'))
fit = r.lm('bar ~ foo', data=d)
print fit.rx2('coefficients')
here is the console output:
>>> from numpy import array, rec
>>> from numpy.random import normal as nprandom
>>> from rpy2.robjects import numpy2ri, r
>>>
>>> foo = array(range(10))
>>> bar = foo + nprandom(0,1,10)
>>>
>>> d = rec.fromarrays([foo, bar], names=('foo','bar'))
>>> fit = r.lm('bar ~ foo', data=d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/rpy2/robjects/functions.py", line 82, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/rpy2/robjects/functions.py", line 33, in __call__
new_kwargs[k] = conversion.py2ri(v)
File "/usr/local/lib/python2.6/dist-packages/rpy2/robjects/__init__.py", line 134, in default_py2ri
raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o))))
ValueError: Nothing can be done for the type <class 'numpy.core.records.recarray'> at the moment.
>>> print fit.rx2('coefficients')
I am running Python 2.6.5 and have numpy version 1.6.1
Does any one know what is causing this error?
You need to add:
rpy2.robjects.activate()
after importing numpy2ri. This SO post references the rpy2 documentation:
That import alone is sufficient to switch an automatic conversion of
numpy objects into rpy2 objects.
Why make this an optional import, while it could have been included in
the function py2ri() (as done in the original patch submitted for that
function) ?
Although both are valid and reasonable options, the design decision
was taken in order to decouple rpy2 from numpy the most, and do not
assume that having numpy installed automatically meant that a
programmer wanted to use it.
Hope this solves your problem.

Categories

Resources