Transferring data between CPU and GPU

Hi, ITensor Teams!

Thanks for sharing such highly efficient code~ I am recently trying to use GPU to accelerate the Julia ITensor code.

I can realize the Time Evolution on GPU by the follow codes,

using ITensors
using ITensorsGPU

L = 5
Nx,Ny = L,L 
s = siteinds("S=1/2", Nx*Ny)

gates = ITensor[]
push!(gates,exp(1im * op("S+",s[1])*op("S-",s[2])))
X = cu.(gates)

state0=["Dn" for i in 1:Nx*Ny] 
psi=productCuMPS(s,state0);
psi = apply(X,psi)

These codes work well.

But, I want to calculate the entropy, and the svd function seem can’t work on GPU. The follow codes meet problem,

b = round(Int,Nx*Ny/2)
U,S,V = svd(psi[b], (linkind(psi, b-1), siteind(psi,b)))

I want to transfer the MPS from GPU to CPU. And I have seen that you mention that Adapt.adapt_structure() can realize the idea.

But, I can’t use it well. Could you please give me a simple and constract example about how to transfer data from GPU to CPU. For example, calculating an entropy starting from GPU MPS

Thank you very much and looking forward to your reply!

Hello Xenon,

Thank you for your question! I see that you have loaded the ITensorGPU package. This package is now deprecated and we now suggest loading the necessary GPU package directly. After loading say CUDA.jl, ITensors.jl will load the appropriate GPU extensions in the backend. Also the MPS code has now migrated to the module ITensorMPS. Finally you can simply move code from GPU to CPU using the ITensors.cpu function
So here is an updated version of your sample code.

using ITensors
using ITensorMPS
using CUDA

L = 5
Nx,Ny = L,L 
s = siteinds("S=1/2", Nx*Ny)

gates = ITensor[]
push!(gates,exp(1im * op("S+",s[1])*op("S-",s[2])))
X = cu.(gates)

state0=["Dn" for i in 1:Nx*Ny] 
psi= cu(MPS(s,state0));
psi = apply(X,psi)

b = round(Int,Nx*Ny/2)
U,S,V = svd(psi[b], (linkind(psi, b-1), siteind(psi,b)));
U * S * V ≈ psi[b] ## true

## convert the SVD to CPU
Ucpu, Scpu, Vcpu = ITensors.cpu.((U,S,V))

Please let me know if you need any assistance or have further questions!

All the best,
Karl