40#ifndef TPETRA_UTIL_HPP
41#define TPETRA_UTIL_HPP
52#include "Tpetra_ConfigDefs.hpp"
53#include "Kokkos_DualView.hpp"
54#include "KokkosCompat_View.hpp"
55#include "Teuchos_Assert.hpp"
56#include "Teuchos_CommHelpers.hpp"
57#include "Teuchos_OrdinalTraits.hpp"
58#include "Teuchos_TypeNameTraits.hpp"
59#include "Teuchos_Utils.hpp"
66#if defined(HAVE_TPETRA_PRINT_EFFICIENCY_WARNINGS)
91#define TPETRA_EFFICIENCY_WARNING(throw_exception_test,msg) \
93 const bool tpetraEfficiencyWarningTest = (throw_exception_test); \
94 if (tpetraEfficiencyWarningTest) { \
95 std::ostringstream errStream; \
96 errStream << Teuchos::typeName(*this) << ":" << std::endl; \
97 errStream << "Efficiency warning: " << #throw_exception_test << std::endl; \
99 std::string err = errStream.str(); \
100 if (TPETRA_PRINTS_EFFICIENCY_WARNINGS && tpetraEfficiencyWarningTest) { \
101 std::cerr << err << std::endl; \
130#define TPETRA_EFFICIENCY_WARNING(throw_exception_test,msg)
134#if defined(HAVE_TPETRA_THROW_ABUSE_WARNINGS) || defined(HAVE_TPETRA_PRINT_ABUSE_WARNINGS)
136#define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg) \
138 std::ostringstream errStream; \
139 errStream << Teuchos::typeName(*this) << msg; \
140 std::string err = errStream.str(); \
141 if (TPETRA_PRINTS_ABUSE_WARNINGS && (throw_exception_test)) { \
142 std::cerr << err << std::endl; \
144 TEUCHOS_TEST_FOR_EXCEPTION(TPETRA_THROWS_ABUSE_WARNINGS && (throw_exception_test), Exception, err); \
148#define TPETRA_ABUSE_WARNING(throw_exception_test,Exception,msg)
180#define SHARED_TEST_FOR_EXCEPTION(throw_exception_test,Exception,msg,comm) \
182 using Teuchos::outArg; \
183 const int lcl_throw_exception = (throw_exception_test) ? Teuchos::rank(comm)+1 : 0; \
185 Teuchos::reduceAll(comm,Teuchos::REDUCE_MAX,lcl_throw_exception,outArg(gbl_throw)); \
186 TEUCHOS_TEST_FOR_EXCEPTION(gbl_throw,Exception, \
187 msg << " Failure on at least one process, including process " << gbl_throw-1 << "." << std::endl); \
203 template<
typename MapType,
typename KeyArgType,
typename ValueArgType>
208 typename MapType::iterator
lb =
m.lower_bound(
k);
209 if(
lb !=
m.end() && !(
m.key_comp()(
k,
lb->first))) {
214 typedef typename MapType::value_type
MVT;
238 typedef typename std::iterator_traits<IT1>::difference_type DT;
239 DT
myit = Teuchos::OrdinalTraits<DT>::one();
280 template<
class IT1,
class IT2>
288 typename std::iterator_traits<IT1>::value_type
piv(*
pivot);
320 template<
class IT1,
class IT2,
class IT3>
330 typename std::iterator_traits<IT1>::value_type
piv(*
pivot);
359 template<
class IT1,
class IT2>
366 typedef typename std::iterator_traits<IT1>::difference_type DT;
367 DT
DT1 = Teuchos::OrdinalTraits<DT>::one();
388 template<
class IT1,
class IT2,
class IT3>
397 typedef typename std::iterator_traits<IT1>::difference_type DT;
398 DT
DT1 = Teuchos::OrdinalTraits<DT>::one();
413 template<
class IT1,
class IT2,
class IT3>
422 typedef typename std::iterator_traits<IT1>::difference_type DT;
425 DT
z = Teuchos::OrdinalTraits<DT>::zero();
429 for (DT
j = 0;
j <
max;
j++)
431 for (DT
k =
j;
k >= 0;
k-=
m)
450 template<
class IT1,
class IT2>
457 typedef typename std::iterator_traits<IT1>::difference_type DT;
460 DT
z = Teuchos::OrdinalTraits<DT>::zero();
464 for (DT
j = 0;
j <
max;
j++)
466 for (DT
k =
j;
k >= 0;
k-=
m)
499 template<
class IT1,
class IT2>
509#ifdef HAVE_TPETRA_DEBUG
511 std::cout <<
"Trouble: sort() did not sort !!" << std::endl;
534 template<
class View1,
class View2>
539 Teuchos::ArrayRCP<typename View1::non_const_value_type>
view1_rcp = Kokkos::Compat::persistingView(
view1, 0,
size);
540 Teuchos::ArrayRCP<typename View2::non_const_value_type>
view2_rcp = Kokkos::Compat::persistingView(
view2, 0,
size);
558 Teuchos::ArrayRCP<typename View::non_const_value_type>
view_rcp = Kokkos::Compat::persistingView(view, 0,
size);
575 Teuchos::ArrayRCP<typename View::non_const_value_type>
view_rcp = Kokkos::Compat::persistingView(view, 0,
size);
598 template<
class IT1,
class IT2,
class IT3>
612#ifdef HAVE_TPETRA_DEBUG
614 std::cout <<
" Trouble sort did not actually sort... !!!!!!" <<
664 template<
class IT1,
class IT2>
753 template<
class IT1,
class IT2,
class BinaryFunction>
819 template<
class KeyInputIterType,
class ValueInputIterType,
820 class KeyOutputIterType,
class ValueOutputIterType,
821 class BinaryFunction>
853 template<
class KeyInputIterType>
855 keyMergeCount (KeyInputIterType keyBeg1, KeyInputIterType keyEnd1,
856 KeyInputIterType keyBeg2, KeyInputIterType keyEnd2)
859 while (keyBeg1 != keyEnd1 && keyBeg2 != keyEnd2) {
860 if (*keyBeg1 < *keyBeg2) {
862 }
else if (*keyBeg1 > *keyBeg2) {
871 count +=
static_cast<size_t> (keyEnd1 - keyBeg1) +
872 static_cast<size_t> (keyEnd2 - keyBeg2);
897 congruent (
const Teuchos::Comm<int>& comm1,
898 const Teuchos::Comm<int>& comm2);
909 template<
class DualViewType>
910 Teuchos::ArrayView<typename DualViewType::t_dev::value_type>
913 static_assert (
static_cast<int> (DualViewType::t_dev::rank) == 1,
914 "The input DualView must have rank 1.");
916 (
x.need_sync_host (), std::logic_error,
"The "
917 "input Kokkos::DualView was most recently modified on device, but this "
918 "function needs the host view of the data to be the most recently "
922 typedef typename DualViewType::t_dev::value_type value_type;
927 return Teuchos::ArrayView<value_type> (
len != 0 ?
x_host.data () :
nullptr,
947 template<
class T,
class DT>
948 Kokkos::DualView<T*, DT>
953 using Kokkos::MemoryUnmanaged;
954 typedef typename DT::memory_space
DMS;
955 typedef typename DT::execution_space execution_space;
956 typedef Kokkos::HostSpace
HMS;
958 const size_t len =
static_cast<size_t> (
x_av.size ());
959 Kokkos::View<const T*, HMS, MemoryUnmanaged>
x_in (
x_av.getRawPtr (),
len);
960 Kokkos::DualView<T*, DT>
x_out (label,
len);
962 x_out.modify_host ();
964 Kokkos::deep_copy (
x_out.view_host (),
x_in);
981 template<
class DualViewType>
984 const auto host =
dv.need_sync_device();
985 const auto dev =
dv.need_sync_host();
987 std::ostringstream
os;
988 os << name <<
": {size: " <<
dv.extent (0)
989 <<
", sync: {host: " <<
host <<
", dev: " <<
dev <<
"}";
997 template<
class ArrayType>
1004 out << name <<
": [";
1006 const size_t numEnt(
x.size());
1032 std::unique_ptr<std::string>
1043 std::unique_ptr<std::string>
1053 std::unique_ptr<std::string>
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...
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.
IT getPivot(const IT &first, const IT &last)
Determines the pivot point as part of the quicksort routine.
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 sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT3 &first3)
Sort the first array, and apply the same permutation to the second and third arrays.
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)
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.