Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tpetra: (local map) device default constructor fix for #11186 #11218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/tpetra/core/src/Tpetra_Details_FixedHashTable_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "Teuchos_FancyOStream.hpp"
#include "Teuchos_VerbosityLevel.hpp"
#include "Kokkos_Core.hpp"
#include "Kokkos_ArithTraits.hpp"

namespace Tpetra {
namespace Details {
Expand Down Expand Up @@ -127,7 +128,7 @@ class FixedHashTable {
typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type> keys_type;

//! Default constructor; makes an empty table.
FixedHashTable ();
KOKKOS_DEFAULTED_FUNCTION FixedHashTable() = default;

/// \brief Constructor for arbitrary keys and contiguous values
/// starting with zero.
Expand Down Expand Up @@ -415,61 +416,70 @@ class FixedHashTable {
///
/// In Tpetra::Map, this corresponds to the minimum global index
/// (local to the MPI process).
KeyType minKey_;
/// @remark It will be set in @ref init.
KeyType minKey_ = ::Kokkos::Details::ArithTraits<KeyType>::max();

/// \brief Maximum key (computed in init()).
///
/// In Tpetra::Map, this corresponds to the maximum global index
/// (local to the MPI process).
KeyType maxKey_;
/// @remark It will be set in @ref init.
KeyType maxKey_ = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min() :
-::Kokkos::Details::ArithTraits<KeyType>::max();

/// \brief Minimum value.
///
/// In Tpetra::Map, this corresponds to the minimum local index
/// (local to the MPI process).
ValueType minVal_;
ValueType minVal_ = ::Kokkos::Details::ArithTraits<ValueType>::max();

/// \brief Maximum value.
///
/// In Tpetra::Map, this corresponds to the maximum local index
/// (local to the MPI process).
ValueType maxVal_;
ValueType maxVal_ = ::Kokkos::Details::ArithTraits<ValueType>::is_integer ?
::Kokkos::Details::ArithTraits<ValueType>::min() :
-::Kokkos::Details::ArithTraits<ValueType>::max();

/// \brief First key in any initial contiguous sequence.
///
/// This only has a defined value if the number of keys is nonzero.
/// In that case, the initial contiguous sequence of keys may have
/// length 1 or more. Length 1 means that the sequence is trivial
/// (there are no initial contiguous keys).
KeyType firstContigKey_;
KeyType firstContigKey_ = ::Kokkos::Details::ArithTraits<KeyType>::max();

/// \brief Last key in any initial contiguous sequence.
///
/// This only has a defined value if the number of keys is nonzero.
/// In that case, the initial contiguous sequence of keys may have
/// length 1 or more. Length 1 means that the sequence is trivial
/// (there are no initial contiguous keys).
KeyType lastContigKey_;
KeyType lastContigKey_ = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min() :
-::Kokkos::Details::ArithTraits<KeyType>::max();

/// \brief Whether the table was created using one of the
/// constructors that assume contiguous values.
///
/// This is false if this object was created using the two-argument
/// (keys, vals) constructor (that takes lists of both keys and
/// values), else true.
bool contiguousValues_;
bool contiguousValues_ = true;

/// \brief Whether the table has checked for duplicate keys.
///
/// This is set at the end of the first call to hasDuplicateKeys().
/// The results of that method are cached in hasDuplicateKeys_ (see
/// below).
bool checkedForDuplicateKeys_;
/// @remark It will be revised in @ref hasDuplicateKeys.
bool checkedForDuplicateKeys_ = true;

/// \brief Whether the table noticed any duplicate keys.
///
/// This is only valid if checkedForDuplicateKeys_ (above) is true.
bool hasDuplicateKeys_;
bool hasDuplicateKeys_ = false;

/// \brief Whether the table has duplicate keys.
///
Expand Down
100 changes: 7 additions & 93 deletions packages/tpetra/core/src/Tpetra_Details_FixedHashTable_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,45 +540,14 @@ class CheckForDuplicateKeys {
// Here begins the actual implementation of FixedHashTable.
//

template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable () :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (::Kokkos::Details::ArithTraits<ValueType>::max ()),
maxVal_ (::Kokkos::Details::ArithTraits<ValueType>::is_integer ?
::Kokkos::Details::ArithTraits<ValueType>::min () :
-::Kokkos::Details::ArithTraits<ValueType>::max ()),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (true), // trivially
checkedForDuplicateKeys_ (true), // it's an empty table; no need to check
hasDuplicateKeys_ (false)
{
}

template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable (const keys_type& keys) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()), // to be set in init()
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()), // to be set in init()
minVal_ (0),
maxVal_ (keys.size () == 0 ?
static_cast<ValueType> (0) :
static_cast<ValueType> (keys.size () - 1)),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
const ValueType startingValue = static_cast<ValueType> (0);
const KeyType initMinKey = this->minKey_;
Expand All @@ -590,21 +559,11 @@ FixedHashTable (const keys_type& keys) :
template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()), // to be set in init()
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()), // to be set in init()
minVal_ (0),
maxVal_ (keys.size () == 0 ?
static_cast<ValueType> (0) :
static_cast<ValueType> (keys.size () - 1)),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
typedef typename keys_type::non_const_type nonconst_keys_type;

Expand All @@ -629,21 +588,11 @@ template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
const ValueType startingValue) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (startingValue),
maxVal_ (keys.size () == 0 ?
startingValue :
static_cast<ValueType> (startingValue + keys.size () - 1)),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
typedef typename keys_type::non_const_type nonconst_keys_type;

Expand Down Expand Up @@ -686,19 +635,13 @@ FixedHashTable (const keys_type& keys,
const KeyType firstContigKey,
const KeyType lastContigKey,
const ValueType startingValue) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (startingValue),
maxVal_ (keys.size () == 0 ?
startingValue :
static_cast<ValueType> (startingValue + keys.size () - 1)),
firstContigKey_ (firstContigKey),
lastContigKey_ (lastContigKey),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
// min() for a floating-point type returns the minimum _positive_
Expand Down Expand Up @@ -726,19 +669,13 @@ FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
const KeyType firstContigKey,
const KeyType lastContigKey,
const ValueType startingValue) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (startingValue),
maxVal_ (keys.size () == 0 ?
startingValue :
static_cast<ValueType> (startingValue + keys.size () - 1)),
firstContigKey_ (firstContigKey),
lastContigKey_ (lastContigKey),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
typedef typename keys_type::non_const_type nonconst_keys_type;

Expand Down Expand Up @@ -777,21 +714,11 @@ template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable (const keys_type& keys,
const ValueType startingValue) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (startingValue),
maxVal_ (keys.size () == 0 ?
startingValue :
static_cast<ValueType> (startingValue + keys.size () - 1)),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (true),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
// min() for a floating-point type returns the minimum _positive_
Expand All @@ -817,21 +744,8 @@ template<class KeyType, class ValueType, class DeviceType>
FixedHashTable<KeyType, ValueType, DeviceType>::
FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
const Teuchos::ArrayView<const ValueType>& vals) :
minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
minVal_ (::Kokkos::Details::ArithTraits<ValueType>::max ()),
maxVal_ (::Kokkos::Details::ArithTraits<ValueType>::is_integer ?
::Kokkos::Details::ArithTraits<ValueType>::min () :
-::Kokkos::Details::ArithTraits<ValueType>::max ()),
firstContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
lastContigKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
::Kokkos::Details::ArithTraits<KeyType>::min () :
-::Kokkos::Details::ArithTraits<KeyType>::max ()),
contiguousValues_ (false),
checkedForDuplicateKeys_ (false),
hasDuplicateKeys_ (false) // to revise in hasDuplicateKeys()
checkedForDuplicateKeys_ (false)
{
// mfh 01 May 2015: I don't trust that
// Teuchos::ArrayView::getRawPtr() returns NULL when the size is 0,
Expand Down
26 changes: 10 additions & 16 deletions packages/tpetra/core/src/Tpetra_Details_LocalMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,9 @@ class LocalMap {
using no_uvm_device_type = device_type;
#endif

LocalMap () :
indexBase_ (0),
myMinGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
myMaxGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
firstContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
lastContiguousGid_ (Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid ()),
numLocalElements_ (0),
contiguous_ (false)
{}
//! Default constructor.
KOKKOS_DEFAULTED_FUNCTION LocalMap() = default;

LocalMap (const ::Tpetra::Details::FixedHashTable<GlobalOrdinal, LocalOrdinal, no_uvm_device_type>& glMap,
const ::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, no_uvm_device_type>& lgMap,
const GlobalOrdinal indexBase,
Expand Down Expand Up @@ -222,13 +216,13 @@ class LocalMap {
/// copying from the default to the nondefault layout.
::Kokkos::View<const GlobalOrdinal*, ::Kokkos::LayoutLeft, no_uvm_device_type> lgMap_;

GlobalOrdinal indexBase_;
GlobalOrdinal myMinGid_;
GlobalOrdinal myMaxGid_;
GlobalOrdinal firstContiguousGid_;
GlobalOrdinal lastContiguousGid_;
LocalOrdinal numLocalElements_;
bool contiguous_;
GlobalOrdinal indexBase_ = 0;
GlobalOrdinal myMinGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
GlobalOrdinal myMaxGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
GlobalOrdinal firstContiguousGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
GlobalOrdinal lastContiguousGid_ = Tpetra::Details::OrdinalTraits<GlobalOrdinal>::invalid();
LocalOrdinal numLocalElements_ = 0;
bool contiguous_ = false;
};

} // namespace Details
Expand Down
24 changes: 23 additions & 1 deletion packages/tpetra/core/test/Map/Map_LocalMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,36 @@ TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( LocalMap, Noncontig, LO, GO, NT )
}
}

/**
* @test This test ensures that the fix brought by trilinos/Trilinos#11218 is tested.
*
* Mainly, it creates a dual view of local maps, assigns on the host view and syncs.
* Without the fix from trilinos/Trilinos#11218, the assignment would crash at runtime
* when running with Cuda UVM.
*/
TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL( LocalMap, KokkosView, LO, GO, NT )
{
using execution_space = typename NT::execution_space;
using map_t = Tpetra::Map<LO, GO, NT>;
using local_map_t = typename map_t::local_map_type;
using dual_view_t = Kokkos::DualView<local_map_t*, execution_space>;

dual_view_t my_dual_view("test view with local maps",1);

my_dual_view.h_view(0) = local_map_t();

my_dual_view.sync_device();
}

//
// INSTANTIATIONS
//

#define UNIT_TEST_GROUP( LO, GO, NT ) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( LocalMap, UniformContig, LO, GO, NT ) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( LocalMap, NonuniformContig, LO, GO, NT ) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( LocalMap, Noncontig, LO, GO, NT )
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( LocalMap, Noncontig, LO, GO, NT ) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( LocalMap, KokkosView, LO, GO, NT )

TPETRA_ETI_MANGLING_TYPEDEFS()

Expand Down