代码如下,最后一行代码运行时会出错。如果交换最后两行则可以正常运行。希望解决的问题是很多场变量在计算过程中是需要的,但是最后存储时是不需要的,如何在计算结束后释放其所占用的显存。感觉to_numpy()也是需要显存的,如果不释放其他的,有可能总显存是不够的。谢谢!
taichi-1.5.0
import taichi as ti
import numpy as np
ti.init(arch=ti.cuda,kernel_profiler=True,device_memory_GB=5.0)
nelx, nely, nelz = 256, 256, 192
elements_box = (nelx,nely,nelz)
nodes_box = (nelx+1,nely+1,nelz+1)
design variable vector and sensitivity
x = ti.field(dtype=ti.f64, shape=elements_box)
xTilde = ti.field(dtype=ti.f64, shape=elements_box)
xPhys = ti.field(dtype=ti.f64, shape=elements_box)
xold = ti.field(dtype=ti.f64, shape=elements_box)
dc = ti.field(dtype=ti.f64, shape=elements_box)
dv = ti.field(dtype=ti.f64, shape=elements_box)
dcPhys = ti.field(dtype=ti.f64, shape=elements_box)
dx = ti.field(dtype=ti.f64, shape=elements_box)
F = ti.Vector.field(3, dtype=ti.f64, shape=nodes_box)
supports = ti.field(dtype=ti.i32, shape=nodes_box)
u = ti.Vector.field(3, dtype=ti.f64, shape=nodes_box)
r = ti.Vector.field(3, dtype=ti.f64, shape=nodes_box)
p = ti.Vector.field(3, dtype=ti.f64, shape=nodes_box)
q = ti.Vector.field(3, dtype=ti.f64, shape=nodes_box)
use_multigrid = True
n_mg_levels = 7
def node_shape(l):
return (nelx//2l+1, nely//2l+1, nelz//2**l+1)
def node_number(l):
return (nelx//2l+1)*(nely//2l+1)*(nelz//2**l+1)
A = [ti.Matrix.field(m=3,n=3,dtype=ti.f64,shape=(node_number(l),27)) for l in range(2,n_mg_levels)] # stencils for coarser grids
M = [ti.Vector.field(3, dtype=ti.f64, shape=node_shape(l)) for l in range(n_mg_levels-1)] # multigird inverse of diagonal of A
Az = [ti.Vector.field(3,dtype=ti.f64,shape=node_shape(l)) for l in range(n_mg_levels)]
rz = [ti.Vector.field(3, dtype=ti.f64, shape=node_shape(l)) for l in range(n_mg_levels)] # multigird residual for Az=r
rAz = [ti.Vector.field(3, dtype=ti.f64, shape=node_shape(l)) for l in range(n_mg_levels)] # multigird residual for Az=r
v = [ti.Vector.field(3, dtype=ti.f64, shape=node_shape(l)) for l in range(n_mg_levels)] # multigird residual for Az=r
z = [ti.Vector.field(3, dtype=ti.f64, shape=node_shape(l)) for l in range(n_mg_levels)] # multigird solution for Az=r
if name == ‘main’:
change = 1.0
while change > 0.01:
change = 0.005
del x,xTilde,xold,dc,dv,dcPhys,dx
del F,supports
del r,p,q
del A,M,Az,rz,rAz,v,z
xPhys_to_show = xPhys.to_numpy()
U_to_show = u.to_numpy()