Hi everyone,
I’m trying to gather a function in parallel, but the vector doesn’t get updated. Any tips would be appreciated, here is a MWE:
from dolfin import *
mesh = UnitIntervalMesh(5)
V = FunctionSpace(mesh, 'CG', 1)
u = Function(V)
v = Vector()
v.init(V.dim())
u.vector()[:] = 1
u.vector().gather(v)
print("Original vs copied: ", u.vector().get_local(), v.get_local()) # [1, 1], [0, 0, 0]
MiroK
July 8, 2019, 3:54pm
2
Hi, consider
from dolfin import *
mesh = UnitIntervalMesh(5)
V = FunctionSpace(mesh, 'CG', 1)
u = Function(V)
u_vec = u.vector()
u_vec[:] = MPI.comm_world.rank + 1
v_vec = Vector(MPI.comm_self, u_vec.local_size())
u_vec.gather(v_vec, V.dofmap().dofs())
print("Original vs copied: ", u_vec.get_local(), v_vec.get_local()) # [1, 1], [0, 0, 0]
1 Like
Thank you MiroK, this solves the question posed. If I may, I wanted this to work to share the entire function phi along my processors, i.e the 5 entries of the vector on each process. Is it possible to use gather for this? I have seen scripts but they are from the old forums and they don’t seem to work anymore.
MiroK
July 8, 2019, 4:27pm
4
Getting the coefficient vector of the function to all the CPUs is shown in the following
from dolfin import *
mesh = UnitIntervalMesh(5)
V = FunctionSpace(mesh, 'CG', 1)
f = Expression('x[0]', degree=1)
u = interpolate(f, V)
u_vec = u.vector()
u_vec[:] = MPI.comm_world.rank + 1
v_vec = Vector(MPI.comm_self, u_vec.local_size())
u_vec.gather(v_vec, V.dofmap().dofs())
print("Original vs copied: ", u_vec.get_local(), v_vec.get_local()) # [1, 1], [0, 0, 0]
g_vec = Vector(MPI.comm_world, V.dim())
# Values from everywhere on 0
g_vec = u_vec.gather_on_zero()
# To everywhere from 0
mine = MPI.comm_world.bcast(g_vec)
# Reconstruct
if MPI.comm_world.rank == 0:
my_g = g_vec
else:
my_g = Vector(MPI.comm_self, V.dim())
my_g.set_local(mine)
I suppose you want next to rebuild from the coefficient vector the function locally on each CPU, so that something like this would return 0
mesh = UnitIntervalMesh(MPI.comm_self, 5)
V = FunctionSpace(mesh, 'CG', 1)
fh = Function(V, my_g)
a = inner(f-fh, f-fh)*dx
print(assemble(a))
Is that the case? Perhaps post the link to the old forum.
Thank you very much MiroK, this is crystal clear and exactly what I needed.