MeshPartitioner.h 6.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file MeshPartitioner.h */

#ifndef AMDIS_MESH_PARTITIONER_H
#define AMDIS_MESH_PARTITIONER_H

#include <map>
Thomas Witkowski's avatar
Thomas Witkowski committed
27
#include <set>
28
29
#include <mpi.h>

30
31
#include "AMDiS_fwd.h"
#include "Mesh.h"
32
#include "parallel/MpiHelper.h"
33
#include "parallel/ParallelDofMapping.h"
34
35


36
namespace AMDiS {
37
38
39
40
41
42
43
44
45
46

  using namespace std;

  enum PartitionMode {
    INITIAL = 0,          // initial partitioning of a unpartitioned mesh 
    ADAPTIVE_REPART = 1,  // repartitioning of a adaptively refined mesh
    REFINE_PART = 2       // quality improvement of the current partitioning
  };


Thomas Witkowski's avatar
Thomas Witkowski committed
47
48
49
50
51
52
  /**
   * Abstract class for mesh partitioning. This class provides only a function
   * for a random initial partitioning. A concrete partition must override the 
   * functions \ref MeshPartitioner::partition and 
   * \ref MeshPartitioner::createPartitionMap.
   */
53
54
55
  class MeshPartitioner
  {
  public:
56
57
58
    MeshPartitioner(string name, MPI::Intracomm *comm)
      : initFileStr(name),
	mpiComm(comm),
59
	mesh(NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
60
	boxPartitioning(false),
61
62
63
64
65
	mapLocalGlobal(NULL)
    {}

    virtual ~MeshPartitioner() {}

Thomas Witkowski's avatar
Thomas Witkowski committed
66
67
68
69
70
71
72
73
74
75
    /** \brief
     * Creates an initial paritioning of the AMDiS mesh. This partitioning
     * can be arbitrary, the only requirement is that each macro element
     * must be uniquely assign to a rank.
     *
     * \return   If the function returns true, the initial partitioning should
     *           be used for the very first computations. This can be e.g. due
     *           to the usage of an initial partitioning file.
     */
    virtual bool createInitialPartitioning();
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    /** \brief
     * Function the takes a weighted set of macro elements and returns a 
     * macro mesh partitioning. This function is virtual and must be implemented
     * for a specific algorithm or an external partitioning library.
     *
     * \param[in]  elemWeights   Maps to each macro element in rank's subdomain
     *                           a weight, which is usually the number of leaf
     *                           elements in this macro element.
     * \param[in]  mode          Most external partitioning libraries can make
     *                           a difference whether we want to create a
     *                           first partitioning or we alread have created
     *                           one using this library but due to some mesh
     *                           adaptivity we want to repartition the mesh. In
     *                           the later case, the libraries also consider the
     *                           time for redistribution of the new partitioning.
     * \return     Returns a boolean value if the partitioning algorithm created
     *             a correct partitioning. If it is so, the partitioning is 
     *             stored in \ref elementInRank and \ref partitionMap.
     */
96
97
98
    virtual bool partition(map<int, double> &elemWeights,
			   PartitionMode mode = INITIAL) = 0;

99
100
    virtual void createPartitionMap(map<int, int>& partitionMap) = 0;

101
102
103
104
105
106
107
108
109
110
111
    /// Write partitioner state to disk.
    void serialize(ostream &out);

    /// Read partitioner state from disk.
    void deserialize(istream &in);

    void setMesh(Mesh *m)
    {
      mesh = m;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
112
113
114
115
116
    void setBoxPartitioning(bool b)
    {
      boxPartitioning = b;
    }

117
    void setLocalGlobalDofMap(DofMap *m)
118
119
120
121
    {
      mapLocalGlobal = m;
    }

122
123
124
125
126
    Mesh *getMesh()
    {
      return mesh;
    }

127
128
129
130
131
132
133
134
135
136
137
138
139
140
    map<int, bool>& getElementInRank()
    {
      return elementInRank;
    }

    map<int, vector<int> >& getRecvElements()
    {
      return recvElements;
    }

    map<int, vector<int> >& getSendElements()
    {
      return sendElements;
    }
141
142
143
144
145
146
147
148
149
150
    
    /// After mesh repartition this function returns true if the mesh must be 
    /// redistributed on at least one rank.
    bool meshChanged()
    {
      int nChanges = recvElements.size() + sendElements.size();
      mpi::globalAdd(nChanges);

      return static_cast<bool>(nChanges);
    }
151
152

  protected:
153
154
155
    /// Prefix for reading parameters from init file.
    string initFileStr;

156
    /// Pointer to the MPI communicator the mesh partitioner should make use of.
157
158
    MPI::Intracomm *mpiComm;

159
    /// Pointer to the AMDiS mesh.
160
161
    Mesh *mesh;

162
163
164
165
166
167
    /// The mesh partitioner can be used in to different modes, the standard
    /// mode and the so called "box partitioning". The standard mode assigns
    /// macro elements to ranks. If box partitioning is enabled, which makes
    /// only sence if the macro mesh results from meshconv's "lego mesher",
    /// then in 2D boxed (2 macro elements) and in 3D cubes (6 macro 
    /// elements) are assigned as a uniion to ranks. 
Thomas Witkowski's avatar
Thomas Witkowski committed
168
169
    bool boxPartitioning;

170
171
    /// In box partitioning mode this map stores for each box number the set
    /// of macro element indices the box consists of.
Thomas Witkowski's avatar
Thomas Witkowski committed
172
    map<int, std::set<int> > boxSplitting;
173
174
175
    
    /// In box partitioning mode this map stores to each box number the set
    /// of neighbouring boxes.
Thomas Witkowski's avatar
Thomas Witkowski committed
176
177
    map<int, std::set<int> > boxNeighbours;

178
179
    /// Is the reverse of the map \ref boxSplitting. Thus, it stores for each
    /// macro element index the box number it belongs to.
Thomas Witkowski's avatar
Thomas Witkowski committed
180
181
    map<int, int> elInBox;

182
    DofMap *mapLocalGlobal;
183

184
185
    map<int, vector<int> > elNeighbours;

186
187
    /// Maps to each macro element index (or box index in box 
    /// partitioning mode) if it is in rank's partition or not.
188
189
    map<int, bool> elementInRank;

190
191
    /// Maps to each macro element index (or box index in box
    /// partitiong mode) the rank number the element belongs to.
192
193
    map<int, int> partitionMap;

194
195
    /// After mesh repartitioning these maps stores which elements are communicated
    /// from this rank to other ranks.
196
197
198
199
200
    map<int, vector<int> > recvElements, sendElements;
  };
}

#endif