# GPU - Unified Memory error with NDTensors.cu()

**URL:** <https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802>\
**Category:** ITensor Julia Questions\
**Created:** [June 12, 2024, 10:32pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802 "2024-06-12T22:32:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joacop16](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/joacop16/32/560_2.png) [@Joacop16](https://itensor.discourse.group/u/Joacop16)\
**Post date:** [June 12, 2024, 10:32pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/1 "2024-06-12T22:32:32Z")

</div>

Hi, ITensor Teams！

I am trying to use GPU to improve a code using multiples GPU at the same time trying to follow [Multiple GPUs · CUDA.jl (juliagpu.org)](https://cuda.juliagpu.org/stable/usage/multigpu/). In general is important to use the unified memory, and usually to do that it is enough to put unified = true as an extra parameter. However, in my code I am using NDTensors.cu() instead of cu() because when I use cu() sometimes I got errors as the one showed here: [TEBD with GPU - error with eigen - ITensor Julia Questions - ITensor Discourse](https://itensor.discourse.group/t/tebd-with-gpu-error-with-eigen/1266/5)

My problem is that when I put unified = true inside NDTensors.cu() I got the following error:

```julia
using ITensors

sites = siteinds("S=1/2",50)

#cu()
A = cu(randomMPS(sites); unified = true) 
#This works without problem

#NDTensors.cu()
A = NDTensors.cu(randomMPS(sites); unified=true) 
#Here I got: MethodError: no method matching cu(::MPS; unified::Bool)

```

So, I am not sure how to use unified memory and NDTensors.cu() at the same time.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 12, 2024, 10:40pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/2 "2024-06-12T22:40:14Z")

</div>

Thanks for the report, we’ll have to look into that and think about how we want to support that.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 12, 2024, 10:53pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/3 "2024-06-12T22:53:44Z")

</div>

In the meantime, you could try using `adapt(CuArray{Float64,<:Any,UnifiedMemory}, x)` where `x` is your MPS. `adapt` is defined in the package `Adapt.jl` so you’ll have to install and load that package.

Also note that the latest syntax is `random_mps`, not `randomMPS`, we’ll be deprecating `randomMPS`.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 12, 2024, 11:08pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/4 "2024-06-12T23:08:57Z")

</div>

Note that you may have to use `adapt(CuArray{Float64,1,UnifiedMemory}, x)` instead because of a bug/limitation in CUDA.jl.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 12, 2024, 11:14pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/5 "2024-06-12T23:14:34Z")

</div>

I raised an issue for the Metal.jl backend here: [Generalize `adapt` to allow specifying the storage mode but not the element type and/or number of dimensions · Issue #363 · JuliaGPU/Metal.jl · GitHub](https://github.com/JuliaGPU/Metal.jl/issues/363) but the same issue should be raised in CUDA.jl and other GPU backends.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 13, 2024, 12:44am UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/6 "2024-06-13T00:44:11Z")

</div>

@Joacop16 I think you could also use `NDTensors.cu(x; storagemode=UnifiedMemory)`, I forgot we had added that.

---

<div class="post-metadata">

**Author:** ![kmp5](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kmp5/32/251_2.png) [@kmp5](https://itensor.discourse.group/u/kmp5)\
**Post date:** [June 13, 2024, 11:26pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/7 "2024-06-13T23:26:15Z")

</div>

Hi @joacop16, we did change the implementation of our `NDTensors.cu` function and using the keyword `unified` should throw an error because that is not an acceptable keyword.

```julia
julia> NDTensors.cu(randomMPS(sites); unified=true)
ERROR: MethodError: no method matching cu(::MPS; unified::Bool)

Closest candidates are:
  cu(::Any; storagemode) got unsupported keyword argument "unified"
   @ NDTensorsCUDAExt ~/.julia/dev/ITensors/NDTensors/ext/NDTensorsCUDAExt/adapt.jl:10

Stacktrace:
 [1] kwerr(::@NamedTuple{unified::Bool}, ::Function, ::MPS)
   @ Base ./error.jl:165
 [2] top-level scope

```

@ REPL[24]:1 What version of ITensors/NDTensors are you using?  
I did run your example with the code Matt suggests and it does properly construct an MPS on GPU with `UnifiedMemory`

```julia
julia> sites = siteinds("S=1/2",2)
2-element Vector{Index{Int64}}:
 (dim=2|id=214|"S=1/2,Site,n=1")
 (dim=2|id=199|"S=1/2,Site,n=2")

julia> NDTensors.cu(randomMPS(sites); storagemode=CUDA.UnifiedMemory)[1]
ITensor ord=2 (dim=2|id=214|"S=1/2,Site,n=1") (dim=1|id=406|"Link,l=1")
NDTensors.Dense{Float64, CuArray{Float64, 1, CUDA.UnifiedMemory}}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [July 8, 2024, 9:29pm UTC](https://itensor.discourse.group/t/gpu-unified-memory-error-with-ndtensors-cu/1802/8 "2024-07-08T21:29:45Z")

</div>

Closed on July 8, 2024, 9:29pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
