This page was generated from unit-5a.2-pardofs/pardofs.ipynb.

# Parallel dofs and Vector-types¶

In this tutorial we learn how NGSolve represents distributed finite element spaces.

```
[1]:
```

```
from ipyparallel import Client
c = Client(profile='mpi')
```

```
[2]:
```

```
%%px
from ngsolve import *
from netgen.geom2d import unit_square
comm = MPI.COMM_WORLD
if comm.rank == 0:
mesh = Mesh(unit_square.GenerateMesh(maxh=0.1).Distribute(comm))
else:
mesh = Mesh(netgen.meshing.Mesh.Receive(comm))
```

Create the space on the distributed mesh. All processes agree on the global number of dofs. Locally, each rank has access only to the subset of dofs associated with its elements. Some dofs are shared by several ranks:

```
[3]:
```

```
%%px
fes = H1(mesh, order=1)
# fes = L2(mesh, order=0)
print ("global dofs =", fes.ndofglobal, ", local dofs =", fes.ndof, \
", sum of local dofs =", comm.allreduce(fes.ndof))
```

```
[stdout:0] global dofs = 136 , local dofs = 0 , sum of local dofs = 156
[stdout:1] global dofs = 136 , local dofs = 51 , sum of local dofs = 156
[stdout:2] global dofs = 136 , local dofs = 53 , sum of local dofs = 156
[stdout:3] global dofs = 136 , local dofs = 52 , sum of local dofs = 156
```

## Parallel Dofs¶

A ParallelDofs object maintains information how dofs are connected across the cluster. The ParallelDofs object is generated by the FESpace, which has access to the connectivity of the mesh.

```
[4]:
```

```
%%px
pardofs = fes.ParallelDofs()
for k in range(pardofs.ndoflocal):
print ("dof", k, "is shard with ranks", list(pardofs.Dof2Proc(k)))
```

```
[stdout:1]
dof 0 is shard with ranks []
dof 1 is shard with ranks [2]
dof 2 is shard with ranks []
dof 3 is shard with ranks []
dof 4 is shard with ranks []
dof 5 is shard with ranks []
dof 6 is shard with ranks []
dof 7 is shard with ranks []
dof 8 is shard with ranks []
dof 9 is shard with ranks []
dof 10 is shard with ranks []
dof 11 is shard with ranks []
dof 12 is shard with ranks [3]
dof 13 is shard with ranks [2]
dof 14 is shard with ranks []
dof 15 is shard with ranks []
dof 16 is shard with ranks []
dof 17 is shard with ranks []
dof 18 is shard with ranks []
dof 19 is shard with ranks []
dof 20 is shard with ranks []
dof 21 is shard with ranks []
dof 22 is shard with ranks []
dof 23 is shard with ranks []
dof 24 is shard with ranks [3]
dof 25 is shard with ranks [2]
dof 26 is shard with ranks []
dof 27 is shard with ranks []
dof 28 is shard with ranks []
dof 29 is shard with ranks []
dof 30 is shard with ranks []
dof 31 is shard with ranks []
dof 32 is shard with ranks []
dof 33 is shard with ranks []
dof 34 is shard with ranks []
dof 35 is shard with ranks [3]
dof 36 is shard with ranks []
dof 37 is shard with ranks []
dof 38 is shard with ranks []
dof 39 is shard with ranks []
dof 40 is shard with ranks []
dof 41 is shard with ranks []
dof 42 is shard with ranks [3]
dof 43 is shard with ranks [2]
dof 44 is shard with ranks [3]
dof 45 is shard with ranks []
dof 46 is shard with ranks []
dof 47 is shard with ranks [2]
dof 48 is shard with ranks []
dof 49 is shard with ranks [2, 3]
dof 50 is shard with ranks [2]
[stdout:2]
dof 0 is shard with ranks [1]
dof 1 is shard with ranks [1]
dof 2 is shard with ranks [1]
dof 3 is shard with ranks [1]
dof 4 is shard with ranks [1]
dof 5 is shard with ranks [1, 3]
dof 6 is shard with ranks [1]
dof 7 is shard with ranks []
dof 8 is shard with ranks []
dof 9 is shard with ranks []
dof 10 is shard with ranks []
dof 11 is shard with ranks []
dof 12 is shard with ranks []
dof 13 is shard with ranks [3]
dof 14 is shard with ranks []
dof 15 is shard with ranks []
dof 16 is shard with ranks []
dof 17 is shard with ranks []
dof 18 is shard with ranks []
dof 19 is shard with ranks []
dof 20 is shard with ranks []
dof 21 is shard with ranks []
dof 22 is shard with ranks []
dof 23 is shard with ranks []
dof 24 is shard with ranks [3]
dof 25 is shard with ranks []
dof 26 is shard with ranks []
dof 27 is shard with ranks []
dof 28 is shard with ranks []
dof 29 is shard with ranks []
dof 30 is shard with ranks []
dof 31 is shard with ranks []
dof 32 is shard with ranks []
dof 33 is shard with ranks [3]
dof 34 is shard with ranks []
dof 35 is shard with ranks []
dof 36 is shard with ranks []
dof 37 is shard with ranks []
dof 38 is shard with ranks []
dof 39 is shard with ranks []
dof 40 is shard with ranks []
dof 41 is shard with ranks [3]
dof 42 is shard with ranks []
dof 43 is shard with ranks []
dof 44 is shard with ranks []
dof 45 is shard with ranks []
dof 46 is shard with ranks [3]
dof 47 is shard with ranks []
dof 48 is shard with ranks []
dof 49 is shard with ranks [3]
dof 50 is shard with ranks []
dof 51 is shard with ranks []
dof 52 is shard with ranks [3]
[stdout:3]
dof 0 is shard with ranks [1]
dof 1 is shard with ranks [1]
dof 2 is shard with ranks [1]
dof 3 is shard with ranks [1]
dof 4 is shard with ranks [1]
dof 5 is shard with ranks [1, 2]
dof 6 is shard with ranks [2]
dof 7 is shard with ranks [2]
dof 8 is shard with ranks [2]
dof 9 is shard with ranks [2]
dof 10 is shard with ranks [2]
dof 11 is shard with ranks [2]
dof 12 is shard with ranks [2]
dof 13 is shard with ranks []
dof 14 is shard with ranks []
dof 15 is shard with ranks []
dof 16 is shard with ranks []
dof 17 is shard with ranks []
dof 18 is shard with ranks []
dof 19 is shard with ranks []
dof 20 is shard with ranks []
dof 21 is shard with ranks []
dof 22 is shard with ranks []
dof 23 is shard with ranks []
dof 24 is shard with ranks []
dof 25 is shard with ranks []
dof 26 is shard with ranks []
dof 27 is shard with ranks []
dof 28 is shard with ranks []
dof 29 is shard with ranks []
dof 30 is shard with ranks []
dof 31 is shard with ranks []
dof 32 is shard with ranks []
dof 33 is shard with ranks []
dof 34 is shard with ranks []
dof 35 is shard with ranks []
dof 36 is shard with ranks []
dof 37 is shard with ranks []
dof 38 is shard with ranks []
dof 39 is shard with ranks []
dof 40 is shard with ranks []
dof 41 is shard with ranks []
dof 42 is shard with ranks []
dof 43 is shard with ranks []
dof 44 is shard with ranks []
dof 45 is shard with ranks []
dof 46 is shard with ranks []
dof 47 is shard with ranks []
dof 48 is shard with ranks []
dof 49 is shard with ranks []
dof 50 is shard with ranks []
dof 51 is shard with ranks []
```

```
[5]:
```

```
%%px
print ("I share dofs with ranks:", list(pardofs.ExchangeProcs()))
for k in range(MPI.COMM_WORLD.size):
print ("with rank", k, "I share dofs", list(pardofs.Proc2Dof(k)))
```

```
[stdout:0]
I share dofs with ranks: []
with rank 0 I share dofs []
with rank 1 I share dofs []
with rank 2 I share dofs []
with rank 3 I share dofs []
[stdout:1]
I share dofs with ranks: [2, 3]
with rank 0 I share dofs []
with rank 1 I share dofs []
with rank 2 I share dofs [1, 13, 25, 43, 47, 49, 50]
with rank 3 I share dofs [12, 24, 35, 42, 44, 49]
[stdout:2]
I share dofs with ranks: [1, 3]
with rank 0 I share dofs []
with rank 1 I share dofs [0, 1, 2, 3, 4, 5, 6]
with rank 2 I share dofs []
with rank 3 I share dofs [5, 13, 24, 33, 41, 46, 49, 52]
[stdout:3]
I share dofs with ranks: [1, 2]
with rank 0 I share dofs []
with rank 1 I share dofs [0, 1, 2, 3, 4, 5]
with rank 2 I share dofs [5, 6, 7, 8, 9, 10, 11, 12]
with rank 3 I share dofs []
```

```
[6]:
```

```
%%px
u,v = fes.TnT()
M = BilinearForm(u*v*dx).Assemble().mat
gfu = GridFunction(fes)
gfu.Set (1)
print (gfu.vec)
```

```
[stdout:0]
CUMULATED
[stdout:1]
CUMULATED
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
[stdout:2]
CUMULATED
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
[stdout:3]
CUMULATED
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
```

We see that all values are set to 1, i.e. joint dofs have the same value. We call such a vector ‘cumulated’. The matrix M is stored locally assembled, i.e. every rank has the contributions from its elements. When we multiply this matrix with a cumulated vector, every rank performs a local matrix vector product. The resulting vector is stored ‘distributed’, i.e. the true values are obtained by adding up rank-local contributions for joint dofs:

```
[7]:
```

```
%%px
r = M.CreateColVector()
r.data = M*gfu.vec
print (r)
```

```
[stdout:0]
DISTRIBUTED
[stdout:1]
DISTRIBUTED
0.0027802
0.00283416
0.0039725
0.00375361
0.00388861
0.00376563
0.00349928
0.00368291
0.00404408
0.00426292
0.00437311
0.00444139
0.0014658
0.00433301
0.0083204
0.00724547
0.00688114
0.00748799
0.00658645
0.00675712
0.00775267
0.00839715
0.00871993
0.0090319
0.00451914
0.00428666
0.00912264
0.00849292
0.00549338
0.00648841
0.00777816
0.00529007
0.008567
0.00884303
0.00917972
0.00468477
0.0118124
0.00864018
0.00991884
0.00919031
0.0092169
0.00980695
0.00469722
0.00279595
0.00460698
0.00968027
0.00981614
0.00407152
0.00719934
0.00322055
0.00648991
[stdout:2]
DISTRIBUTED
0.00146518
0.00437393
0.00431373
0.0051848
0.00411535
0.00336642
0.00582709
0.00166667
0.00562709
0.0050217
0.00468867
0.00450762
0.00440335
0.00261423
0.00405037
0.00415878
0.00418516
0.00458225
0.00295645
0.00795127
0.0118424
0.0098731
0.00925447
0.00885406
0.00385107
0.00794122
0.00823007
0.00853115
0.0088331
0.0120828
0.00985308
0.0101019
0.00807761
0.00277782
0.00802492
0.00870047
0.00899733
0.00738066
0.00885212
0.00876737
0.0118635
0.00461611
0.00923301
0.0097591
0.00710458
0.00608186
0.00914589
0.0092282
0.0107192
0.00325411
0.0126267
0.0097095
0.00265338
[stdout:3]
DISTRIBUTED
0.00287912
0.00453599
0.00518222
0.0052177
0.00428098
0.00255828
0.00129684
0.00368141
0.00478087
0.00680982
0.00372835
0.00512269
0.00302992
0.00166667
0.00268337
0.00500037
0.00644384
0.00409095
0.00461854
0.00458749
0.00440086
0.00433327
0.00420676
0.00388916
0.00384047
0.00372249
0.00364716
0.00918092
0.00669926
0.00913567
0.00973333
0.00893083
0.00863608
0.00852597
0.00803114
0.00703305
0.0070936
0.0067694
0.00918769
0.00938333
0.0125598
0.0083769
0.00849984
0.00890247
0.00776319
0.00506096
0.00759804
0.00750222
0.00918872
0.00970756
0.00501103
0.00718479
```

This cumulated/distributed pair of vectors is prefect for computing inner products. We can compute inner products of local vectors, and sum up (i.e. reduce in MPI terms) across all ranks:

```
[8]:
```

```
%%px
print ("global ip =", InnerProduct(gfu.vec, r))
localip = InnerProduct(r.local_vec, gfu.vec.local_vec)
print ("local contribution:", localip)
print ("cummulated:", comm.allreduce(localip, MPI.SUM))
```

```
[stdout:0]
global ip = 1.0
local contribution: 0.0
cummulated: 1.0
[stdout:1]
global ip = 1.0
local contribution: 0.3221867580747561
cummulated: 1.0
[stdout:2]
global ip = 1.0
local contribution: 0.36188191680234977
cummulated: 1.0
[stdout:3]
global ip = 1.0
local contribution: 0.31593132512289407
cummulated: 1.0
```

```
[ ]:
```

```
```