# GPU support in Knet: Cannot allocate before CuArrays has been initialized

**URL:** <https://discourse.julialang.org/t/gpu-support-in-knet-cannot-allocate-before-cuarrays-has-been-initialized/30859>\
**Category:** New to Julia\
**Tags:** knet, gpuarrays\
**Created:** [November 8, 2019, 1:07pm UTC](https://discourse.julialang.org/t/gpu-support-in-knet-cannot-allocate-before-cuarrays-has-been-initialized/30859 "2019-11-08T13:07:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter.Zentis](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Peter.Zentis](https://discourse.julialang.org/u/Peter.Zentis)\
**Post date:** [November 8, 2019, 1:07pm UTC](https://discourse.julialang.org/t/gpu-support-in-knet-cannot-allocate-before-cuarrays-has-been-initialized/30859/1 "2019-11-08T13:07:26Z")

</div>

Hi all,  
I am trying to work through the Knet tutorial ([https://github.com/denizyuret/Knet.jl/blob/master/tutorial/50.cnn.ipynb](https://github.com/denizyuret/Knet.jl/blob/master/tutorial/50.cnn.ipynb)) which worked great, just until I needed/wanted to use gpu support i.e. use a KnetArray. I suppose the problem is related to my CUDA setup. The output of nvidia-smi looks good to me and I do not know what else to look at:

```julia
nvidia-smi
Fri Nov 8 13:39:57 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.87.00 Driver Version: 418.87.00 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:03:00.0 On | N/A |
| 0% 43C P8 14W / 250W | 233MiB / 11169MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 2606 G /usr/bin/X 38MiB |
| 0 2805 G /usr/bin/gnome-shell 47MiB |
| 0 14149 C /opt/julia-1.2.0/bin/julia 135MiB |
+-----------------------------------------------------------------------------+

```

Also I successfully tested nvcc as described here: [https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#compiling-examples](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#compiling-examples)

I am not sure whether this is relevant for this issue, but I am on a Opensuse Leap 15.0 system, and tried to install CUDA according to the official instructions.

Anyway, this line from the Knet tutorial fails with an error:

```julia
x = KnetArray(reshape([1.0:6.0...], (6,1,1,1)))

```

And I get the following error:

```julia
ERROR: AssertionError: Cannot allocate before CuArrays has been initialized.
Stacktrace:
 [1] CuArrays.CuArray{UInt8,1,P} where P(::UndefInitializer, ::Tuple{Int64}) at /root/.julia/packages/CuArrays/BcKUl/src/memory.jl:120
 [2] Type at /root/.julia/packages/CuArrays/BcKUl/src/array.jl:98 [inlined]
 [3] Type at /root/.julia/packages/CuArrays/BcKUl/src/array.jl:99 [inlined]
 [4] KnetPtrCu(::Int64) at /root/.julia/packages/Knet/HRYiN/src/cuarray.jl:90
 [5] Type at /root/.julia/packages/Knet/HRYiN/src/kptr.jl:102 [inlined]
 [6] Type at /root/.julia/packages/Knet/HRYiN/src/karray.jl:95 [inlined]
 [7] KnetArray(::Array{Float64,4}) at /root/.julia/packages/Knet/HRYiN/src/karray.jl:93
 [8] top-level scope at REPL[4]:1

```

Apparently, the problem is due to my CUDA setup?! At least I get a bunch of errors on pkg\> test CuArrays:

```julia
Test Summary: | Pass Fail Error Total
CuArrays | 63 1 1124 1188
  GPUArrays test suite | 25 431 456
    construction | 15 7 22
      constructors + similar | 1 1
      comparison against Array | 15 1 16
      conversion | 1 1
      value constructors | 1 1
      iterator constructors | 3 3

  ...

    Batchnorm | 1 1
  CUTENSOR | No tests
  ForwardDiff | 10 9 19
    UNARY | No tests
    POW | 8 8
    LITERAL_POW | 10 10
    Broadcast Fix | 1 1
ERROR: LoadError: Some tests did not pass: 63 passed, 1 failed, 1124 errored, 0 broken.
in expression starting at /home/kzentis/.julia/packages/CuArrays/BcKUl/test/runtests.jl:23
ERROR: Package CuArrays errored during testing

```

This is just the end of the output, I can post it all if required.  
It would be really great, if someone could point me where to search for the underlying cause of my problem.

Thanks, Peter

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [November 8, 2019, 2:47pm UTC](https://discourse.julialang.org/t/gpu-support-in-knet-cannot-allocate-before-cuarrays-has-been-initialized/30859/2 "2019-11-08T14:47:26Z")

</div>

This has been fixed a couple of hours ago, please update CuArrays.jl.

@denizyuret: Knet shouldn’t use CuArrays if `CuArrays.functional() == false`. Either you should disable your GPU support if that’s the case, or refuse to initialize Knet if GPU support is mandatory. (This is the downside of CuArrays being loadable at all times.)

---

<div class="post-metadata">

**Author:** ![Peter.Zentis](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@Peter.Zentis](https://discourse.julialang.org/u/Peter.Zentis)\
**Post date:** [November 8, 2019, 3:04pm UTC](https://discourse.julialang.org/t/gpu-support-in-knet-cannot-allocate-before-cuarrays-has-been-initialized/30859/3 "2019-11-08T15:04:21Z")

</div>

Indeed, I updated to CuArray v.1.4.4 and now it works! Marvelous!  
Thanks a lot!  
Peter
