libMesh
linear_partitioner.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // Local Includes
21 #include "libmesh/linear_partitioner.h"
22 #include "libmesh/libmesh_logging.h"
23 #include "libmesh/elem.h"
24 #include "libmesh/parallel.h"
25 
26 namespace libMesh
27 {
28 
32  const unsigned int n)
33 {
34  const bool mesh_is_serial = mesh.is_serial();
35 
36  // Check for easy returns
37  if (it == end && mesh_is_serial)
38  return;
39 
40  if (n == 1)
41  {
42  this->single_partition_range (it, end);
43  return;
44  }
45 
46  libmesh_assert_greater (n, 0);
47 
48  // Create a simple linear partitioning
49  LOG_SCOPE ("partition_range()", "LinearPartitioner");
50 
51  // This has to be an ordered set
52  std::set<dof_id_type> element_ids;
53 
54  // If we're on a serialized mesh, we know our range is the same on
55  // every processor.
56  if (mesh_is_serial)
57  {
58  const dof_id_type blksize = cast_int<dof_id_type>
59  (std::distance(it, end) / n);
60 
61  dof_id_type e = 0;
62  for (auto & elem : as_range(it, end))
63  {
64  if ((e/blksize) < n)
65  elem->processor_id() = cast_int<processor_id_type>(e/blksize);
66  else
67  elem->processor_id() = 0;
68 
69  e++;
70  }
71  }
72  // If we're on a replicated mesh, we might have different ranges on
73  // different processors, and we'll need to gather the full range.
74  //
75  // This is not an efficient way to do this, but if you want to be
76  // efficient then you want to be using a different partitioner to
77  // begin with; LinearPartitioner is more for debugging than
78  // performance.
79  else
80  {
81  for (const auto & elem : as_range(it, end))
82  element_ids.insert(elem->id());
83 
84  mesh.comm().set_union(element_ids);
85 
86  const dof_id_type blksize = cast_int<dof_id_type>
87  (element_ids.size());
88 
89  dof_id_type e = 0;
90  for (auto eid : element_ids)
91  {
92  Elem * elem = mesh.query_elem_ptr(eid);
93  if (elem)
94  {
95  if ((e/blksize) < n)
96  elem->processor_id() = cast_int<processor_id_type>(e/blksize);
97  else
98  elem->processor_id() = 0;
99  }
100 
101  e++;
102  }
103  }
104 }
105 
106 
107 
109  const unsigned int n)
110 {
111  this->partition_range(mesh,
112  mesh.active_elements_begin(),
113  mesh.active_elements_end(),
114  n);
115 }
116 
117 } // namespace libMesh
libMesh::Partitioner::single_partition_range
void single_partition_range(MeshBase::element_iterator it, MeshBase::element_iterator end)
Slightly generalized version of single_partition which acts on a range of elements defined by the pai...
Definition: partitioner.C:179
libMesh::LinearPartitioner::partition_range
virtual void partition_range(MeshBase &mesh, MeshBase::element_iterator it, MeshBase::element_iterator end, const unsigned int n) override
Called by the SubdomainPartitioner to partition elements in the range (it, end).
Definition: linear_partitioner.C:29
libMesh::dof_id_type
uint8_t dof_id_type
Definition: id_types.h:67
libMesh::MeshBase::element_iterator
The definition of the element_iterator struct.
Definition: mesh_base.h:1873
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
end
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end.
Definition: variant_filter_iterator.h:343
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::DofObject::processor_id
processor_id_type processor_id() const
Definition: dof_object.h:829
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::as_range
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
Helper function that allows us to treat a homogenous pair as a range.
Definition: simple_range.h:57
libMesh::LinearPartitioner::_do_partition
virtual void _do_partition(MeshBase &mesh, const unsigned int n) override
Partition the MeshBase into n subdomains.
Definition: linear_partitioner.C:108
distance
Real distance(const Point &p)
Definition: subdomains_ex3.C:50
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100