libMesh
linear_partitioner.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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/enum_partitioner_type.h"
25 #include "libmesh/parallel.h"
26 
27 namespace libMesh
28 {
29 
31 {
32  return LINEAR_PARTITIONER;
33 }
34 
35 
39  const unsigned int n)
40 {
41  const bool mesh_is_serial = mesh.is_serial();
42 
43  // Check for easy returns
44  if (it == end && mesh_is_serial)
45  return;
46 
47  if (n == 1)
48  {
49  this->single_partition_range (it, end);
50  return;
51  }
52 
53  libmesh_assert_greater (n, 0);
54 
55  // Create a simple linear partitioning
56  LOG_SCOPE ("partition_range()", "LinearPartitioner");
57 
58  // This has to be an ordered set
59  std::set<dof_id_type> element_ids;
60 
61  // If we're on a serialized mesh, we know our range is the same on
62  // every processor.
63  if (mesh_is_serial)
64  {
65  const dof_id_type blksize = cast_int<dof_id_type>
66  (std::distance(it, end) / n);
67 
68  dof_id_type e = 0;
69  for (auto & elem : as_range(it, end))
70  {
71  if ((e/blksize) < n)
72  elem->processor_id() = cast_int<processor_id_type>(e/blksize);
73  else
74  elem->processor_id() = 0;
75 
76  e++;
77  }
78  }
79  // If we're on a replicated mesh, we might have different ranges on
80  // different processors, and we'll need to gather the full range.
81  //
82  // This is not an efficient way to do this, but if you want to be
83  // efficient then you want to be using a different partitioner to
84  // begin with; LinearPartitioner is more for debugging than
85  // performance.
86  else
87  {
88  for (const auto & elem : as_range(it, end))
89  element_ids.insert(elem->id());
90 
91  mesh.comm().set_union(element_ids);
92 
93  const dof_id_type blksize = cast_int<dof_id_type>
94  (element_ids.size() / n);
95 
96  dof_id_type e = 0;
97  for (auto eid : element_ids)
98  {
99  Elem * elem = mesh.query_elem_ptr(eid);
100  if (elem)
101  {
102  if ((e/blksize) < n)
103  elem->processor_id() = cast_int<processor_id_type>(e/blksize);
104  else
105  elem->processor_id() = 0;
106  }
107 
108  e++;
109  }
110  }
111 }
112 
113 
114 
116  const unsigned int n)
117 {
118  this->partition_range(mesh,
119  mesh.active_elements_begin(),
120  mesh.active_elements_end(),
121  n);
122 }
123 
124 } // namespace libMesh
The definition of the element_iterator struct.
Definition: mesh_base.h:2103
bool 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:319
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
The libMesh namespace provides an interface to certain functionality in the library.
Real distance(const Point &p)
This is the MeshBase class.
Definition: mesh_base.h:74
PartitionerType
Defines an enum for mesh partitioner types.
virtual PartitionerType type() const override
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
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).
processor_id_type processor_id() const
Definition: dof_object.h:898
uint8_t dof_id_type
Definition: id_types.h:67
virtual void _do_partition(MeshBase &mesh, const unsigned int n) override
Partition the MeshBase into n subdomains.