10#ifndef TPETRA_UTIL_HPP
11#define TPETRA_UTIL_HPP
22#include "Tpetra_ConfigDefs.hpp"
23#include "Kokkos_DualView.hpp"
24#include "KokkosCompat_View.hpp"
25#include "Teuchos_Assert.hpp"
26#include "Teuchos_CommHelpers.hpp"
27#include "Teuchos_OrdinalTraits.hpp"
28#include "Teuchos_TypeNameTraits.hpp"
29#include "Teuchos_Utils.hpp"
37#if defined(HAVE_TPETRA_PRINT_EFFICIENCY_WARNINGS)
62#define TPETRA_EFFICIENCY_WARNING(throw_exception_test,msg) \
64 const bool tpetraEfficiencyWarningTest = (throw_exception_test); \
65 if (tpetraEfficiencyWarningTest) { \
66 std::ostringstream errStream; \
67 errStream << Teuchos::typeName(*this) << ":" << std::endl; \
68 errStream << "Efficiency warning: " << #throw_exception_test << std::endl; \
70 std::string err = errStream.str(); \
71 if (TPETRA_PRINTS_EFFICIENCY_WARNINGS && tpetraEfficiencyWarningTest) { \
72 std::cerr << err << std::endl; \
101#define TPETRA_EFFICIENCY_WARNING(throw_exception_test,msg)
105#if defined(HAVE_TPETRA_THROW_ABUSE_WARNINGS) || defined(HAVE_TPETRA_PRINT_ABUSE_WARNINGS)
107#define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg) \
109 std::ostringstream errStream; \
110 errStream << Teuchos::typeName(*this) << msg; \
111 std::string err = errStream.str(); \
112 if (TPETRA_PRINTS_ABUSE_WARNINGS && (throw_exception_test)) { \
113 std::cerr << err << std::endl; \
115 TEUCHOS_TEST_FOR_EXCEPTION(TPETRA_THROWS_ABUSE_WARNINGS && (throw_exception_test), Exception, err); \
119#define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg)
151#define SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
153 using Teuchos::outArg; \
154 const int lcl_throw_exception = (throw_exception_test) ? Teuchos::rank(comm)+1 : 0; \
156 Teuchos::reduceAll(comm,Teuchos::REDUCE_MAX,lcl_throw_exception,outArg(gbl_throw)); \
157 TEUCHOS_TEST_FOR_EXCEPTION(gbl_throw,Exception, \
158 msg << " Failure on at least one process, including process " << gbl_throw-1 << "." << std::endl); \
174 template<
typename MapType,
typename KeyArgType,
typename ValueArgType>
179 typename MapType::iterator
lb =
m.lower_bound(
k);
180 if(
lb !=
m.end() && !(
m.key_comp()(
k,
lb->first))) {
185 typedef typename MapType::value_type
MVT;
209 typedef typename std::iterator_traits<IT1>::difference_type DT;
210 DT
myit = Teuchos::OrdinalTraits<DT>::one();
251 template<
class IT1,
class IT2>
259 typename std::iterator_traits<IT1>::value_type
piv(*
pivot);
291 template<
class IT1,
class IT2,
class IT3>
301 typename std::iterator_traits<IT1>::value_type
piv(*
pivot);
330 template<
class IT1,
class IT2>
337 typedef typename std::iterator_traits<IT1>::difference_type DT;
338 DT
DT1 = Teuchos::OrdinalTraits<DT>::one();
359 template<
class IT1,
class IT2,
class IT3>
368 typedef typename std::iterator_traits<IT1>::difference_type DT;
369 DT
DT1 = Teuchos::OrdinalTraits<DT>::one();
384 template<
class IT1,
class IT2,
class IT3>
393 typedef typename std::iterator_traits<IT1>::difference_type DT;
396 DT
z = Teuchos::OrdinalTraits<DT>::zero();
400 for (DT
j = 0;
j <
max;
j++)
402 for (DT
k =
j;
k >= 0;
k-=
m)
421 template<
class IT1,
class IT2>
428 typedef typename std::iterator_traits<IT1>::difference_type DT;
431 DT
z = Teuchos::OrdinalTraits<DT>::zero();
435 for (DT
j = 0;
j <
max;
j++)
437 for (DT
k =
j;
k >= 0;
k-=
m)
453 template<
typename IT1>
456 typedef typename std::iterator_traits<IT1>::difference_type DT;
459 std::vector<DT> idx(
length);
460 std::iota(idx.begin(), idx.end(), 0);
462 std::stable_sort(idx.begin(), idx.end(),
463 [&
first](
size_t i1,
size_t i2) {return first[i1] < first[i2];});
471 template<
typename IT1,
typename IT2>
478 typedef typename std::iterator_traits<IT1>::difference_type DT;
479 typedef typename std::iterator_traits<IT1>::value_type
T;
483 Kokkos::View<T*, Kokkos::HostSpace>
tmp(Kokkos::view_alloc(Kokkos::WithoutInitializing,
"tmp"),
n);
485 Kokkos::parallel_for(
"apply_permutation_1",
486 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(0,
n),
490 Kokkos::parallel_for(
"apply_permutation_2",
491 Kokkos::RangePolicy<Kokkos::DefaultHostExecutionSpace>(0,
n),
503 template<
class IT1,
class IT2>
520 template<
class IT1,
class IT2,
class IT3>
556 template<
class IT1,
class IT2>
569#ifdef HAVE_TPETRA_DEBUG
571 std::cout <<
"Trouble: sort() did not sort !!" << std::endl;
594 template<
class View1,
class View2>
599 Teuchos::ArrayRCP<typename View1::non_const_value_type>
view1_rcp = Kokkos::Compat::persistingView(
view1, 0,
size);
600 Teuchos::ArrayRCP<typename View2::non_const_value_type>
view2_rcp = Kokkos::Compat::persistingView(
view2, 0,
size);
618 Teuchos::ArrayRCP<typename View::non_const_value_type>
view_rcp = Kokkos::Compat::persistingView(view, 0,
size);
635 Teuchos::ArrayRCP<typename View::non_const_value_type>
view_rcp = Kokkos::Compat::persistingView(view, 0,
size);
658 template<
class IT1,
class IT2,
class IT3>
675#ifdef HAVE_TPETRA_DEBUG
677 std::cout <<
" Trouble sort did not actually sort... !!!!!!" <<
727 template<
class IT1,
class IT2>
816 template<
class IT1,
class IT2,
class BinaryFunction>
882 template<
class KeyInputIterType,
class ValueInputIterType,
883 class KeyOutputIterType,
class ValueOutputIterType,
884 class BinaryFunction>
916 template<
class KeyInputIterType>
918 keyMergeCount (KeyInputIterType keyBeg1, KeyInputIterType keyEnd1,
919 KeyInputIterType keyBeg2, KeyInputIterType keyEnd2)
922 while (keyBeg1 != keyEnd1 && keyBeg2 != keyEnd2) {
923 if (*keyBeg1 < *keyBeg2) {
925 }
else if (*keyBeg1 > *keyBeg2) {
934 count +=
static_cast<size_t> (keyEnd1 - keyBeg1) +
935 static_cast<size_t> (keyEnd2 - keyBeg2);
960 congruent (
const Teuchos::Comm<int>& comm1,
961 const Teuchos::Comm<int>& comm2);
972 template<
class DualViewType>
973 Teuchos::ArrayView<typename DualViewType::t_dev::value_type>
976 static_assert (
static_cast<int> (DualViewType::t_dev::rank) == 1,
977 "The input DualView must have rank 1.");
979 (
x.need_sync_host (), std::logic_error,
"The "
980 "input Kokkos::DualView was most recently modified on device, but this "
981 "function needs the host view of the data to be the most recently "
985 typedef typename DualViewType::t_dev::value_type value_type;
990 return Teuchos::ArrayView<value_type> (
len != 0 ?
x_host.data () :
nullptr,
1010 template<
class T,
class DT>
1011 Kokkos::DualView<T*, DT>
1016 using Kokkos::MemoryUnmanaged;
1017 typedef typename DT::memory_space
DMS;
1018 typedef typename DT::execution_space execution_space;
1019 typedef Kokkos::HostSpace
HMS;
1021 const size_t len =
static_cast<size_t> (
x_av.size ());
1022 Kokkos::View<const T*, HMS, MemoryUnmanaged>
x_in (
x_av.getRawPtr (),
len);
1023 Kokkos::DualView<T*, DT>
x_out (label,
len);
1025 x_out.modify_host ();
1027 Kokkos::deep_copy (
x_out.view_host (),
x_in);
1044 template<
class DualViewType>
1047 const auto host =
dv.need_sync_device();
1048 const auto dev =
dv.need_sync_host();
1050 std::ostringstream
os;
1051 os << name <<
": {size: " <<
dv.extent (0)
1052 <<
", sync: {host: " <<
host <<
", dev: " <<
dev <<
"}";
1060 template<
class ArrayType>
1067 out << name <<
": [";
1069 const size_t numEnt(
x.size());
1095 std::unique_ptr<std::string>
1106 std::unique_ptr<std::string>
1116 std::unique_ptr<std::string>
void std_sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2, const IT3 &first3, const IT3 &last3)
Sort the first array using std sort, and apply the resulting permutation to the second and third arra...
void sh_sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &, const IT3 &first3, const IT3 &)
Sort the first array using shell sort, and apply the resulting permutation to the second and third ar...
void std_sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2)
Sort the first array using std sort, and apply the resulting permutation to the second array.
IT1 partition2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2, const IT1 &pivot)
Partition operation for quicksort2().
void quicksort2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2)
Sort the first array using Quicksort, and apply the resulting permutation to the second array.
void apply_permutation(IT1 first, IT1 last, IT2 indices)
Apply a permutation of the ordering of an array in place.
IT getPivot(const IT &first, const IT &last)
Determines the pivot point as part of the quicksort routine.
std::vector< typename std::iterator_traits< IT1 >::difference_type > sort_indexes(IT1 first, IT1 last)
Compute the permutation of the ordering that sorts the array using a stable sort.
IT1 partition3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2, const IT3 &first3, const IT3 &last3, const IT1 &pivot)
Partition operation for quicksort3().
void sh_sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &)
Sort the first array using shell sort, and apply the resulting permutation to the second array.
bool isAlreadySorted(const IT1 &first, const IT1 &last)
Determines whether or not a random access sequence is already sorted.
void quicksort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT2 &last2, const IT3 &first3, const IT3 &last3)
Sort the first array using Quicksort, and apply the resulting permutation to the second and third arr...
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
Implementation details of sort routines used by Tpetra.
void verbosePrintArray(std::ostream &out, const ArrayType &x, const char name[], const size_t maxNumToPrint)
Print min(x.size(), maxNumToPrint) entries of x.
Teuchos::ArrayView< typename DualViewType::t_dev::value_type > getArrayViewFromDualView(const DualViewType &x)
Get a Teuchos::ArrayView which views the host Kokkos::View of the input 1-D Kokkos::DualView.
std::unique_ptr< std::string > createPrefix(const int myRank, const char prefix[])
Create string prefix for each line of verbose output.
bool congruent(const Teuchos::Comm< int > &comm1, const Teuchos::Comm< int > &comm2)
Whether the two communicators are congruent.
Kokkos::DualView< T *, DT > getDualViewCopyFromArrayView(const Teuchos::ArrayView< const T > &x_av, const char label[], const bool leaveOnHost)
Get a 1-D Kokkos::DualView which is a deep copy of the input Teuchos::ArrayView (which views host mem...
std::string dualViewStatusToString(const DualViewType &dv, const char name[])
Return the status of the given Kokkos::DualView, as a human-readable string.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
void sort(View &view, const size_t &size)
Convenience wrapper for std::sort for host-accessible views.
void reverse_sort(View &view, const size_t &size)
Convenience wrapper for a reversed std::sort for host-accessible views.
void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const bool stableSort=false)
Sort the first array, and apply the resulting permutation to the second array.
MapType::iterator efficientAddOrUpdate(MapType &m, const KeyArgType &k, const ValueArgType &v)
Efficiently insert or replace an entry in an std::map.
void merge2(IT1 &indResultOut, IT2 &valResultOut, IT1 indBeg, IT1 indEnd, IT2 valBeg, IT2)
Merge values in place, additively, with the same index.
void keyValueMerge(KeyInputIterType keyBeg1, KeyInputIterType keyEnd1, ValueInputIterType valBeg1, ValueInputIterType valEnd1, KeyInputIterType keyBeg2, KeyInputIterType keyEnd2, ValueInputIterType valBeg2, ValueInputIterType valEnd2, KeyOutputIterType keyOut, ValueOutputIterType valOut, BinaryFunction f)
Merge two sorted (by keys) sequences of unique (key,value) pairs by combining pairs with equal keys.
void sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT3 &first3, const bool stableSort=false)
Sort the first array, and apply the same permutation to the second and third arrays.