title: How to properly use anaconda accelerate for GPU
body: <p>I am trying to get fast computations of matrices with anaconda accelerate. I started with very basic example: multiply 2 matrices.</p>
  
  <p>My goal is to somehow get GPU-multiplication which is better than usual numpy.dot</p>
  
  <p>Here is my basic example, based on this <a href="http://docs.continuum.io/numbapro/quickstart.html#example-batch-matrix-multiplication" rel="nofollow">documentation</a>.</p>
  
  <pre><code>from numbapro import guvectorize
  from numpy import arange
  
  @guvectorize(['void(float32[:,:], float32[:,:], float32[:,:])'], '(m,n),(n,p)-&gt;(m,p)', target='gpu')
  def matmul(A, B, C):
      m, n = A.shape
      n, p = B.shape
      for i in range(m):
          for j in range(p):
              C[i, j] = 0
              for k in range(n):
                  C[i, j] += A[i, k] * B[k, j]
  
  import numpy as np
  import time
  
  for dim in [50, 100, 200]:
      rnd = np.random.RandomState(0)
      a = rnd.rand(dim, dim).astype(np.float32)
      b = rnd.rand(dim, dim).astype(np.float32)
      resgpu = np.zeros_like(a)
  
      start = time.time()
      rescpu = np.dot(a, b)
      print('CPU:', time.time() - start)
  
      start = time.time()
      resgpu = matmul(a, b)
      print('GPU:', time.time() - start)
  
      print(np.allclose(rescpu, resgpu))
      print(np.allclose(resgpu, rescpu))
  </code></pre>
  
  <p>Results are too bad: GPU is incredibly slower than CPU</p>
  
  <pre><code>CPU: 0.00011801719665527344
  GPU: 0.05677294731140137
  True
  True
  CPU: 0.00011205673217773438
  GPU: 0.3881375789642334
  True
  True
  CPU: 0.00038933753967285156
  GPU: 3.018171787261963
  True
  True
  </code></pre>
  
  <p>Of course I understand that internal numpy realization is well optimized, but I expected anaconda official example to be good. I am using <strong>python 3.4.3</strong> and got errors with using these two helping libs: <a href="http://www.cs.toronto.edu/~tijmen/gnumpy.html" rel="nofollow">http://www.cs.toronto.edu/~tijmen/gnumpy.html</a> and <a href="https://github.com/rctn/gpupy" rel="nofollow">https://github.com/rctn/gpupy</a></p>
  
  <p>I should say that with gpupy I had successful speedup on python 2.7.</p>
  
  <p>So my question is: how can I get matrix multiplication better than numpy-CPU by using GPU? What is wrong with anaconda official example and if there a working library for python3 that allows to use GPU in numpy way?</p>
  
  <p>===</p>
  
  <p>RESULTS</p>
  
  <p><strong>Unfortunately, there is no simple and good way for python 3, use 2.7 instead</strong></p>
  
  <p>Thanks to @rth for recommendint awesome library <a href="https://github.com/lebedov/scikits.cuda" rel="nofollow">scikits.cuda</a></p>
  
  <p><a href="http://scikit-cuda.readthedocs.org/en/latest/reference.html#reference" rel="nofollow">Available functions</a></p>
  
  <p>Some benchmark (tested with using anaconda mkl, so numpy is fast too)</p>
  
  <pre><code>dim = 10000
  rnd = np.random.RandomState(0)
  a = rnd.rand(dim, dim).astype(np.float32)
  b = rnd.rand(dim, dim).astype(np.float32)
  a_gpu = gpuarray.to_gpu(a)
  b_gpu = gpuarray.to_gpu(b)
  
  start = time.time()
  rescpu = np.dot(a, b)
  print 'CPU:', time.time() - start
  
  start = time.time()
  resgpu = culinalg.dot(a_gpu, b_gpu)
  print 'GPU:', time.time() - start
  
  resgpu = resgpu.get()
  print np.allclose(rescpu, resgpu)
  print np.allclose(resgpu, rescpu)
  </code></pre>
  
  <p>And results</p>
  
  <pre><code>CPU: 16.4765479565
  GPU: 0.000520944595337
  </code></pre>