# CUBLASNotInitialized error

**URL:** <https://pyfr.discourse.group/t/cublasnotinitialized-error/299>\
**Category:** General\
**Created:** [26 September 2020 01:11 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299 "2020-09-26T01:11:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 01:11 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/1 "2020-09-26T01:11:42Z")

</div>

I’m trying to run the Couette flow example using the CUDA backend via the following command:

$ pyfr run -b cuda -p couette\_flow\_2d.pyfrm couette\_flow\_2d.ini

But I get the following error:

File “/home/cades/miniconda3/lib/python3.8/site-packages/pyfr/backends/cuda/cublas.py”, line 78, in \_errcheck

raise self.\_statuses[status]

pyfr.backends.cuda.cublas.CUBLASNotInitialized

Any suggestions on how to fix this?

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [26 September 2020 01:58 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/2 "2020-09-26T01:58:27Z")

</div>

Hi Gavin,

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 03:35 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/3 "2020-09-26T03:35:04Z")

</div>

I ran some of the PyCuda examples at [https://github.com/inducer/pycuda/tree/master/examples](https://github.com/inducer/pycuda/tree/master/examples) without any problems.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 04:25 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/4 "2020-09-26T04:25:34Z")

</div>

I used Numba to get some system info (see below). Looks like the cublas library is working fine.

System info:

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [26 September 2020 12:16 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/5 "2020-09-26T12:16:53Z")

</div>

Hi Gavin,

Looking at the output below it appears as if Numba is opening up and loading the CUBLAS shared library, although it does not state if it is actually calling any of the methods. Would you be able to run a test case in Numba which calls down to CUBLAS?

Regards, Freddie.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 14:56 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/6 "2020-09-26T14:56:09Z")

</div>

I have never used Numba (other than the above) so I don’t have a readily available example to use.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 15:54 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/7 "2020-09-26T15:54:09Z")

</div>

I created the following example using CuPy which uses Cuda to calculate the norm of a matrix. I think this uses cublas. This example works fine.

# Compare CuPy and NumPy

import cupy as cp  
import numpy as np  
import time

n = 100\_000\_000  
ns = 10\_000

# Using NumPy

x\_cpu = np.arange(n) - 4  
x\_cpu.reshape((ns, ns))

ti = time.perf\_counter()  
norm\_cpu = np.linalg.norm(x\_cpu)  
tf = time.perf\_counter()

print(f’NumPy result: \t{norm\_cpu}’)  
print(f’NumPy time: \t{tf - ti:.4g}’)

# Using CuPy

x\_gpu = cp.arange(n) - 4  
x\_gpu.reshape((ns, ns))

ti = time.perf\_counter()  
norm\_gpu = cp.linalg.norm(x\_gpu)  
tf = time.perf\_counter()

print(f’CuPy result: \t{norm\_gpu}’)  
print(f’CuPy time: \t{tf - ti:.4g}’)

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [26 September 2020 16:10 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/8 "2020-09-26T16:10:23Z")

</div>

Hi Gavin,

So it looks as if the cupy.linalg.norm function makes does not call out to CUBLAS. See:

[https://github.com/cupy/cupy/blob/master/cupy/linalg/norms.py#L54](https://github.com/cupy/cupy/blob/master/cupy/linalg/norms.py#L54)

Regards, Freddie.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 16:20 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/9 "2020-09-26T16:20:52Z")

</div>

Here’s another example that uses the CuPy solve function which does use cublas. At least from what I can tell in the CuPy source code. This example works fine too.

# Compare CuPy and NumPy

import cupy as cp  
import numpy as np  
import time

# Using NumPy

a\_cpu = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])  
b\_cpu = np.array([25, -10, -4])

ti = time.perf\_counter()  
x\_cpu = np.linalg.solve(a\_cpu, b\_cpu)  
tf = time.perf\_counter()

print(f’NumPy result: \t{x\_cpu}’)  
print(f’NumPy time: \t{tf - ti:.4g}’)

# Using CuPy

a\_gpu = cp.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])  
b\_gpu = cp.array([25, -10, -4])

ti = time.perf\_counter()  
x\_gpu = cp.linalg.solve(a\_gpu, b\_gpu)  
tf = time.perf\_counter()

print(f’CuPy result: \t{x\_gpu}’)  
print(f’CuPy time: \t{tf - ti:.4g}’)

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [26 September 2020 17:50 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/10 "2020-09-26T17:50:48Z")

</div>

Hi Gavin,

So I believe that here CuPy is using cusolver as opposed to CUBLAS to  
solve the system.

Would you be able to run the following snippet for me?

from ctypes import CDLL, POINTER, c\_void\_p

lib = CDLL('libcublas.so')  
create = lib.cublasCreate\_v2  
create.argtypes = [POINTER(c\_void\_p)]

handle = c\_void\_p()  
print(create(handle))

This should print 0.

Regards, Freddie.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 18:18 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/11 "2020-09-26T18:18:55Z")

</div>

Freddie, I ran your script and it prints 1.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [26 September 2020 18:29 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/12 "2020-09-26T18:29:35Z")

</div>

In my bashrc file, I’m pointing to the CUDA environment as follows:

export PATH="$PATH:/usr/local/cuda-9.2/bin"  
export LD\_LIBRARY\_PATH="/usr/local/cuda-9.2/lib64"

The libcublas.so is located in /usr/local/cuda-9.2/lib64/ as:

lrwxrwxrwx 1 root root 16 Sep 25 19:45 libcublas.so -\> libcublas.so.9.2\*  
lrwxrwxrwx 1 root root 19 Sep 25 19:45 libcublas.so.9.2 -\> libcublas.so.9.2.88\*

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [26 September 2020 19:04 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/13 "2020-09-26T19:04:23Z")

</div>

Hi Gavin,

It appears as if either your CUDA install or environment is broken. It  
could be the version of CUBLAS the loader is finding is incompatible  
with your main CUDA version.

Regards, Freddie.

---

<div class="post-metadata">

**Author:** ![Gavin\_Wiggins](https://avatars.discourse-cdn.com/v4/letter/g/cdc98d/32.png) [@Gavin\_Wiggins](https://pyfr.discourse.group/u/Gavin_Wiggins)\
**Post date:** [30 September 2020 14:53 UTC](https://pyfr.discourse.group/t/cublasnotinitialized-error/299/14 "2020-09-30T14:53:05Z")

</div>

So I wiped the system I was working on and did a fresh install of CUDA. Everything seems to be working fine now.
