Debian Codegraph @

dune/istl/test/vectorcommtest.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #include <dune/common/parallel/indexset.hh>
6 #include <dune/common/parallel/communicator.hh>
7 #include <dune/common/parallel/remoteindices.hh>
8 #include <vector>
9 #include <dune/common/enumset.hh>
10 #include <dune/common/fvector.hh>
11 #include <algorithm>
12 #include <iostream>
13 #include "mpi.h"
14
15 enum GridFlags {
16 owner, overlap, border
17 };
18
19 void testIndices(MPI_Comm comm)
20 {
21 //using namespace Dune;
22
23 // The global grid size
24 const int Nx = 20;
25 const int Ny = 2;
26
27 // Process configuration
28 int procs, rank, master=0;
29 MPI_Comm_size(comm, &procs);
30 MPI_Comm_rank(comm, &rank);
31
32 // shift the ranks
33 //rank = (rank + 1) % procs;
34 //master= (master+1) %procs;
35
36 // The local grid
37 int nx = Nx/procs;
38 // distributed indexset
39 // typedef ParallelLocalIndex<GridFlags> LocalIndexType;
40
41 typedef Dune::ParallelIndexSet<int,Dune::ParallelLocalIndex<GridFlags>,45> ParallelIndexSet;
42
43 ParallelIndexSet distIndexSet;
44 // global indexset
45 ParallelIndexSet globalIndexSet;
46
47 // Set up the indexsets.
48 int start = std::max(rank*nx-1,0);
49 int end = std::min((rank + 1) * nx+1, Nx);
50
51 distIndexSet.beginResize();
52
53 int localIndex=0;
54 int size = Ny*(end-start);
55
56 typedef Dune::FieldVector<int,5> Vector;
57 typedef std::vector<Vector> Array;
58
59 Array distArray(size);
60 Array* globalArray;
61 int index=0;
62
63 for(int j=0; j<Ny; j++)
64 for(int i=start; i<end; i++) {
65 bool isPublic = (i<=start+1)||(i>=end-2);
66 GridFlags flag = owner;
67 if((i==start && i!=0)||(i==end-1 && i!=Nx-1)) {
68 distArray[index++]=-(i+j*Nx+rank*Nx*Ny);
69 flag = overlap;
70 }else
71 distArray[index++]=i+j*Nx+rank*Nx*Ny;
72
73 distIndexSet.add(i+j*Nx, Dune::ParallelLocalIndex<GridFlags> (localIndex++,flag,isPublic));
74 }
75
76 distIndexSet.endResize();
77
78 if(rank==master) {
79 // build global indexset on first process
80 globalIndexSet.beginResize();
81 globalArray=new Array(Nx*Ny);
82
83 for(int j=0; j<Ny; j++)
84 for(int i=0; i<Nx; i++) {
85 globalIndexSet.add(i+j*Nx, Dune::ParallelLocalIndex<GridFlags> (i+j*Nx,owner,false));
86 globalArray->operator[](i+j*Nx)=-(i+j*Nx);
87 }
88
89 globalIndexSet.endResize();
90 }else
91 globalArray=new Array(1); // Size one is needed for CommPolicy
92
93 typedef Dune::RemoteIndices<ParallelIndexSet> RemoteIndices;
94
95 RemoteIndices accuIndices(distIndexSet, globalIndexSet, comm);
96 RemoteIndices overlapIndices(distIndexSet, distIndexSet, comm);
97 accuIndices.rebuild<true>();
98 overlapIndices.rebuild<false>();
99
100 Dune::DatatypeCommunicator<ParallelIndexSet> accumulator, overlapExchanger;
101
102 Dune::EnumItem<GridFlags,owner> sourceFlags;
103 Dune::Combine<Dune::EnumItem<GridFlags,overlap>,Dune::EnumItem<GridFlags,owner>,GridFlags> destFlags;
104
105 accumulator.build(accuIndices, sourceFlags, distArray, destFlags, *globalArray);
106
107 overlapExchanger.build(overlapIndices, Dune::EnumItem<GridFlags,owner>(), distArray, Dune::EnumItem<GridFlags,overlap>(), distArray);
108
109 std::cout<< rank<<": before forward distArray=";
110 std::copy(distArray.begin(), distArray.end(), std::ostream_iterator<Vector>(std::cout, " "));
111
112 // Exchange the overlap
113 overlapExchanger.forward();
114
115 std::cout<<rank<<": overlap exchanged distArray=";
116 std::copy(distArray.begin(), distArray.end(), std::ostream_iterator<Vector>(std::cout, " "));
117
118 if(rank==master)
119 {
120 std::cout<<": before forward globalArray=";
121 std::copy(globalArray->begin(), globalArray->end(), std::ostream_iterator<Vector>(std::cout, " "));
122 }
123
124 accumulator.forward();
125
126
127 if(rank==master) {
128 std::cout<<"after forward global: ";
129 std::copy(globalArray->begin(), globalArray->end(), std::ostream_iterator<Vector>(std::cout, " "));
130 struct Twice
131 {
132 void operator()(Vector& v)
133 {
134 v*=2;
135 }
136 };
137
138 std::for_each(globalArray->begin(), globalArray->end(), Twice());
139 std::cout<<" Multiplied by two: globalArray=";
140 std::copy(globalArray->begin(), globalArray->end(), std::ostream_iterator<Vector>(std::cout, " "));
141 }
142
143 accumulator.backward();
144 std::cout<< rank<<": after backward distArray=";
145 std::copy(distArray.begin(), distArray.end(), std::ostream_iterator<Vector>(std::cout, " "));
146
147
148 // Exchange the overlap
149 overlapExchanger.forward();
150
151 std::cout<<rank<<": overlap exchanged distArray=";
152 std::copy(distArray.begin(), distArray.end(), std::ostream_iterator<Vector>(std::cout, " "));
153
154 //std::cout << rank<<": source and dest are the same:"<<std::endl;
155 //std::cout << remote<<std::endl<<std::flush;
156 if(rank==master)
157 delete globalArray;
158 }
159
160 int main(int argc, char** argv)
161 {
162 MPI_Init(&argc, &argv);
163 testIndices(MPI_COMM_WORLD);
164 MPI_Finalize();
165
166 }