22#include <boost/container/small_vector.hpp>
26#ifdef DEAL_II_WITH_TRILINOS
28# ifdef DEAL_II_WITH_MPI
29# include <Epetra_MpiComm.h>
31# include <Epetra_Map.h>
32# include <Epetra_SerialComm.h>
33# ifdef DEAL_II_TRILINOS_WITH_TPETRA
34# include <Tpetra_Map.hpp>
43#ifdef DEAL_II_WITH_TRILINOS
45# ifdef DEAL_II_TRILINOS_WITH_TPETRA
47template <
typename NodeType>
50 const Tpetra::Map<int, types::signed_global_dof_index, NodeType>> &map)
55 Assert(map->getMinAllGlobalIndex() == 0,
57 "The Tpetra::Map does not contain the global index 0, "
58 "which means some entries are not present on any processor."));
61 if (map->isContiguous())
66# if DEAL_II_TRILINOS_VERSION_GTE(13, 4, 0)
67 const size_type n_indices = map->getLocalNumElements();
69 const size_type n_indices = map->getNodeNumElements();
72 map->getMyGlobalIndices().data();
84# ifdef DEAL_II_WITH_64BIT_INDICES
88 , index_space_size(1 + map.MaxAllGID64())
89 , largest_range(
numbers::invalid_unsigned_int)
91 Assert(map.MinAllGID64() == 0,
93 "The Epetra_BlockMap does not contain the global index 0, "
94 "which means some entries are not present on any processor."));
101 const size_type n_indices = map.NumMyElements();
103 reinterpret_cast<size_type *
>(map.MyGlobalElements64());
118 Assert(map.MinAllGID() == 0,
120 "The Epetra_BlockMap does not contain the global index 0, "
121 "which means some entries are not present on any processor."));
128 const size_type n_indices = map.NumMyElements();
129 unsigned int *indices =
130 reinterpret_cast<unsigned int *
>(map.MyGlobalElements());
157 std::vector<Range>::iterator store =
ranges.begin();
158 for (std::vector<Range>::iterator i =
ranges.begin(); i !=
ranges.end();)
160 std::vector<Range>::iterator next = i;
167 while (next !=
ranges.end() && (next->begin <= last_index))
169 last_index =
std::max(last_index, next->end);
175 *store =
Range(first_index, last_index);
179 if (store !=
ranges.end())
181 std::vector<Range> new_ranges(
ranges.begin(), store);
186 size_type next_index = 0, largest_range_size = 0;
187 for (std::vector<Range>::iterator i =
ranges.begin(); i !=
ranges.end();
192 i->nth_index_in_set = next_index;
193 next_index += (i->end - i->begin);
194 if (i->end - i->begin > largest_range_size)
196 largest_range_size = i->end - i->begin;
213 for (
const auto &range :
ranges)
217 "In the process of creating the current IndexSet "
218 "object, you added indices beyond the size of the index "
219 "space. Specifically, you added elements that form the "
221 std::to_string(range.begin) +
"," + std::to_string(range.end) +
222 "), but the size of the index space is only " +
238 std::vector<Range>::const_iterator r1 =
ranges.begin(),
246 if (r1->end <= r2->begin)
248 else if (r2->end <= r1->begin)
253 Assert(((r1->begin <= r2->begin) && (r1->end > r2->begin)) ||
254 ((r2->begin <= r1->begin) && (r2->end > r1->begin)),
258 result.add_range(
std::max(r1->begin, r2->begin),
264 if (r1->end <= r2->end)
282 ExcMessage(
"End index needs to be larger or equal to begin index!"));
284 ExcMessage(
"You are asking for a view into an IndexSet object "
285 "that would cover the sub-range [" +
286 std::to_string(
begin) +
',' + std::to_string(
end) +
287 "). But this is not a subset of the range "
288 "of the current object, which is [0," +
289 std::to_string(
size()) +
")."));
292 std::vector<Range>::const_iterator r1 =
ranges.begin();
294 while (r1 !=
ranges.end())
296 if ((r1->end >
begin) && (r1->begin <
end))
301 else if (r1->begin >=
end)
317 ExcMessage(
"The mask must have the same size index space "
318 "as the index set it is applied to."));
330 if (mask.ranges.size() == 1)
331 return get_view(mask.ranges[0].begin, mask.ranges[0].end);
340 std::vector<Range> new_ranges;
342 std::vector<Range>::iterator own_it =
ranges.begin();
343 std::vector<Range>::iterator mask_it = mask.ranges.begin();
345 while ((own_it !=
ranges.end()) && (mask_it != mask.ranges.end()))
351 if (own_it->end <= mask_it->begin)
359 if (mask_it->end <= own_it->begin)
378 if ((own_it->begin <= mask_it->begin) && (own_it->end <= mask_it->end))
380 new_ranges.emplace_back(mask_it->begin - mask_it->nth_index_in_set,
381 own_it->end - mask_it->nth_index_in_set);
385 if ((mask_it->begin <= own_it->begin) && (mask_it->end <= own_it->end))
387 const size_type offset_within_mask_interval =
388 own_it->begin - mask_it->begin;
389 new_ranges.emplace_back(mask_it->nth_index_in_set +
390 offset_within_mask_interval,
391 mask_it->nth_index_in_set +
392 (mask_it->end - mask_it->begin));
396 if ((own_it->begin <= mask_it->begin) &&
397 (own_it->end >= mask_it->end))
399 new_ranges.emplace_back(mask_it->begin -
400 mask_it->nth_index_in_set,
401 mask_it->end - mask_it->nth_index_in_set);
405 if ((mask_it->begin <= own_it->begin) &&
406 (mask_it->end >= own_it->end))
408 const size_type offset_within_mask_interval =
409 own_it->begin - mask_it->begin;
410 new_ranges.emplace_back(mask_it->nth_index_in_set +
411 offset_within_mask_interval,
412 mask_it->nth_index_in_set +
413 offset_within_mask_interval +
414 (own_it->end - own_it->begin));
423 if (own_it->end < mask_it->end)
425 else if (mask_it->end < own_it->end)
440 for (
const auto &range : new_ranges)
441 result.
add_range(range.begin, range.end);
451 const std::vector<types::global_dof_index> &n_indices_per_block)
const
453 std::vector<IndexSet> partitioned;
454 const unsigned int n_blocks = n_indices_per_block.size();
456 partitioned.reserve(n_blocks);
458 for (
const auto n_block_indices : n_indices_per_block)
460 partitioned.push_back(this->
get_view(start, start + n_block_indices));
461 start += n_block_indices;
467 for (
const auto &partition : partitioned)
469 sum += partition.size();
489 std::vector<Range> new_ranges;
491 std::vector<Range>::iterator own_it =
ranges.begin();
492 std::vector<Range>::iterator other_it = other.
ranges.begin();
494 while (own_it !=
ranges.end() && other_it != other.
ranges.end())
497 if (own_it->end <= other_it->begin)
499 new_ranges.push_back(*own_it);
504 if (own_it->begin >= other_it->end)
512 if (own_it->begin < other_it->begin)
514 Range r(own_it->begin, other_it->begin);
516 new_ranges.push_back(r);
521 own_it->begin = other_it->end;
522 if (own_it->begin > own_it->end)
524 own_it->begin = own_it->end;
533 for (; own_it !=
ranges.end(); ++own_it)
534 new_ranges.push_back(*own_it);
539 const std::vector<Range>::iterator
end = new_ranges.end();
540 for (std::vector<Range>::iterator it = new_ranges.begin(); it !=
end; ++it)
552 for (
const auto el : *
this)
565 "pop_back() failed, because this IndexSet contains no entries."));
583 "pop_front() failed, because this IndexSet contains no entries."));
606 const auto insert_position =
608 if (insert_position ==
ranges.end() ||
609 insert_position->begin > new_range.
begin ||
610 insert_position->end < new_range.
end)
611 ranges.insert(insert_position, new_range);
618 boost::container::small_vector<std::pair<size_type, size_type>, 200>
620 const bool ranges_are_sorted)
622 if (!ranges_are_sorted)
623 std::sort(tmp_ranges.begin(), tmp_ranges.end());
633 if (tmp_ranges.size() > 9)
636 tmp_set.
ranges.reserve(tmp_ranges.size());
637 for (
const auto &i : tmp_ranges)
642 if (this->
ranges.size() <= 1)
644 if (this->
ranges.size() == 1)
646 std::swap(*
this, tmp_set);
652 for (
const auto &i : tmp_ranges)
661 if ((
this == &other) && (offset == 0))
664 if (other.
ranges.size() != 0)
672 std::vector<Range>::const_iterator r1 =
ranges.begin(),
673 r2 = other.
ranges.begin();
675 std::vector<Range> new_ranges;
682 if (r2 == other.
ranges.end() ||
683 (r1 !=
ranges.end() && r1->end < (r2->begin + offset)))
685 new_ranges.push_back(*r1);
688 else if (r1 ==
ranges.end() || (r2->end + offset) < r1->begin)
690 new_ranges.emplace_back(r2->begin + offset, r2->end + offset);
698 std::max(r1->end, r2->end + offset));
699 new_ranges.push_back(next);
716 ExcMessage(
"One index set can only be a subset of another if they "
717 "describe index spaces of the same size. The ones in "
718 "question here have sizes " +
719 std::to_string(
size()) +
" and " +
720 std::to_string(other.
size()) +
"."));
740 out <<
size() <<
" ";
741 out <<
ranges.size() << std::endl;
742 std::vector<Range>::const_iterator r =
ranges.begin();
743 for (; r !=
ranges.end(); ++r)
745 out << r->begin <<
" " << r->end << std::endl;
757 unsigned int n_ranges;
762 for (
unsigned int i = 0; i < n_ranges; ++i)
779 std::size_t n_ranges =
ranges.size();
780 out.write(
reinterpret_cast<const char *
>(&n_ranges),
sizeof(n_ranges));
781 if (
ranges.empty() ==
false)
782 out.write(
reinterpret_cast<const char *
>(&*
ranges.begin()),
791 std::size_t n_ranges;
792 in.read(
reinterpret_cast<char *
>(&
size),
sizeof(
size));
793 in.read(
reinterpret_cast<char *
>(&n_ranges),
sizeof(n_ranges));
799 in.read(
reinterpret_cast<char *
>(&*
ranges.begin()),
822 std::vector<Range>::const_iterator p = std::upper_bound(
830 return ((index >= p->begin) && (index < p->
end));
838 return (p->end > index);
855 return p->begin + (n - p->nth_index_in_set);
867 std::vector<Range>::const_iterator p =
871 if (p ==
ranges.end() || p->end == n || p->begin > n)
877 return (n - p->begin) + p->nth_index_in_set;
891 std::vector<Range>::const_iterator main_range =
894 Range r(global_index, global_index + 1);
897 std::vector<Range>::const_iterator range_begin, range_end;
898 if (global_index < main_range->
begin)
900 range_begin =
ranges.begin();
901 range_end = main_range;
905 range_begin = main_range;
911 const std::vector<Range>::const_iterator p =
924 if (global_index < p->
begin)
927 return {
this,
static_cast<size_type>(p -
ranges.begin()), global_index};
932std::vector<IndexSet::size_type>
937 std::vector<size_type> indices;
940 for (
const auto &range :
ranges)
941 for (
size_type entry = range.begin; entry < range.end; ++entry)
942 indices.push_back(entry);
959#ifdef DEAL_II_WITH_TRILINOS
960# ifdef DEAL_II_TRILINOS_WITH_TPETRA
962template <
typename NodeType>
963Tpetra::Map<int, types::signed_global_dof_index, NodeType>
972template <
typename NodeType>
973Teuchos::RCP<Tpetra::Map<int, types::signed_global_dof_index, NodeType>>
987 ExcMessage(
"You are trying to create an Tpetra::Map object "
988 "that partitions elements of an index set "
989 "between processors. However, the union of the "
990 "index sets on different processors does not "
991 "contain all indices exactly once: the sum of "
992 "the number of entries the various processors "
993 "want to store locally is " +
994 std::to_string(n_global_elements) +
995 " whereas the total size of the object to be "
997 std::to_string(
size()) +
998 ". In other words, there are "
999 "either indices that are not spoken for "
1000 "by any processor, or there are indices that are "
1001 "claimed by multiple processors."));
1011 Tpetra::Map<int, types::signed_global_dof_index, NodeType>>(
1015# ifdef DEAL_II_WITH_MPI
1025 std::vector<types::signed_global_dof_index> int_indices(indices.size());
1026 std::copy(indices.begin(), indices.end(), int_indices.begin());
1027 const Teuchos::ArrayView<types::signed_global_dof_index> arr_view(
1031 Tpetra::Map<int, types::signed_global_dof_index, NodeType>>(
1035# ifdef DEAL_II_WITH_MPI
1062 ExcMessage(
"You are trying to create an Epetra_Map object "
1063 "that partitions elements of an index set "
1064 "between processors. However, the union of the "
1065 "index sets on different processors does not "
1066 "contain all indices exactly once: the sum of "
1067 "the number of entries the various processors "
1068 "want to store locally is " +
1069 std::to_string(n_global_elements) +
1070 " whereas the total size of the object to be "
1072 std::to_string(
size()) +
1073 ". In other words, there are "
1074 "either indices that are not spoken for "
1075 "by any processor, or there are indices that are "
1076 "claimed by multiple processors."));
1089# ifdef DEAL_II_WITH_MPI
1090 Epetra_MpiComm(communicator)
1106# ifdef DEAL_II_WITH_MPI
1107 Epetra_MpiComm(communicator)
1117#ifdef DEAL_II_WITH_PETSC
1121 std::vector<size_type> indices;
1127 const auto local_size =
static_cast<PetscInt
>(
n_elements());
1131 std::vector<PetscInt> petsc_indices(
n_elements());
1132 for (
const auto &index : *
this)
1134 const auto petsc_index =
static_cast<PetscInt
>(index);
1136 petsc_indices[i] = petsc_index;
1141 PetscErrorCode ierr = ISCreateGeneral(
1142 communicator, local_size, petsc_indices.data(), PETSC_COPY_VALUES, &is);
1158 if (n_global_elements !=
size())
1161 if (n_global_elements == 0)
1164#ifdef DEAL_II_WITH_MPI
1166 const bool all_contiguous =
1168 if (!all_contiguous)
1171 bool is_globally_ascending =
true;
1178 const std::vector<types::global_dof_index> global_dofs =
1188 for (; index < global_dofs.size(); ++index)
1193 if (new_dof <= old_dof)
1195 is_globally_ascending =
false;
1205 int is_ascending = is_globally_ascending ? 1 : 0;
1206 int ierr = MPI_Bcast(&is_ascending, 1, MPI_INT, 0, communicator);
1209 return (is_ascending == 1);
1229# ifdef DEAL_II_WITH_TRILINOS
1230# ifdef DEAL_II_TRILINOS_WITH_TPETRA
1233 const Teuchos::RCP<
const Tpetra::Map<
1239# if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || \
1240 defined(KOKKOS_ENABLE_SYCL)
1242 const Teuchos::RCP<
const Tpetra::Map<
1255# if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || \
1256 defined(KOKKOS_ENABLE_SYCL)
1265template Teuchos::RCP<
1272# if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || \
1273 defined(KOKKOS_ENABLE_SYCL)
1274template Teuchos::RCP<
bool is_subset_of(const IndexSet &other) const
bool is_element_binary_search(const size_type local_index) const
size_type index_within_set_binary_search(const size_type global_index) const
IS make_petsc_is(const MPI_Comm communicator=MPI_COMM_WORLD) const
bool is_ascending_and_one_to_one(const MPI_Comm communicator) const
bool is_contiguous() const
Tpetra::Map< int, types::signed_global_dof_index, NodeType > make_tpetra_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
ElementIterator at(const size_type global_index) const
void fill_index_vector(std::vector< size_type > &indices) const
std::vector< IndexSet > split_by_block(const std::vector< types::global_dof_index > &n_indices_per_block) const
size_type n_elements() const
void add_range_lower_bound(const Range &range)
ElementIterator begin() const
void set_size(const size_type size)
void read(std::istream &in)
Epetra_Map make_trilinos_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
IndexSet tensor_product(const IndexSet &other) const
void write(std::ostream &out) const
void block_read(std::istream &in)
void add_ranges_internal(boost::container::small_vector< std::pair< size_type, size_type >, 200 > &tmp_ranges, const bool ranges_are_sorted)
IntervalIterator begin_intervals() const
std::vector< Range > ranges
void subtract_set(const IndexSet &other)
ElementIterator end() const
Threads::Mutex compress_mutex
size_type index_space_size
void block_write(std::ostream &out) const
IndexSet get_view(const size_type begin, const size_type end) const
void add_range(const size_type begin, const size_type end)
std::size_t memory_consumption() const
size_type nth_index_in_set_binary_search(const size_type local_index) const
std::vector< size_type > get_index_vector() const
Teuchos::RCP< Tpetra::Map< int, types::signed_global_dof_index, NodeType > > make_tpetra_map_rcp(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
types::global_dof_index size_type
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
IndexSet operator&(const IndexSet &is) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#define AssertIntegerConversion(index1, index2)
#define DEAL_II_ASSERT_UNREACHABLE()
#define AssertThrowIntegerConversion(index1, index2)
static ::ExceptionBase & ExcIO()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Tpetra::KokkosCompat::KokkosDeviceWrapperNode< typename MemorySpace::kokkos_space::execution_space, typename MemorySpace::kokkos_space > NodeType
Tpetra::Map< LO, GO, NodeType< MemorySpace > > MapType
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
T sum(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
std::vector< T > gather(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
Teuchos::RCP< T > make_rcp(Args &&...args)
Iterator lower_bound(Iterator first, Iterator last, const T &val)
constexpr types::global_dof_index invalid_dof_index
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
int signed_global_dof_index
unsigned int global_dof_index
static bool end_compare(const IndexSet::Range &x, const IndexSet::Range &y)
static bool nth_index_compare(const IndexSet::Range &x, const IndexSet::Range &y)
size_type nth_index_in_set