Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a00d1a1
Define type traits to check the nature of ALP containers
Jun 10, 2022
bb81462
Specialize type traits for ALP containers
Jun 10, 2022
06c35e8
Add type traits to extract certain static information about ALP conta…
Jun 13, 2022
446c052
Add type trait to inspect ALP container's properties
Jun 13, 2022
7df983b
Add backend-agnostic type trait specializations for ALP containers
Jun 13, 2022
909716a
Base new ALP type traits on std type traits
Jun 13, 2022
b9ea108
Improve static_assert message
Jun 13, 2022
aa10a4e
Improve naming of type traits for ALP containers
Jun 13, 2022
a97d3bc
Add type trait to inspect whether an ALP container is original or a v…
Jun 13, 2022
01554aa
Add a unit test for new type traits
Jun 13, 2022
7df5dcd
Fix the type of transposed view for a square matrix
Jun 13, 2022
947f991
Minor fixes in newly added type traits
Jun 13, 2022
22cfe83
Move type traits for internal use to internal namespace
Jun 14, 2022
d4363f9
Offer more understandable type traits
Jun 14, 2022
a455110
Adjust unit test to new type traits
Jun 15, 2022
b732dde
Remove unnecessary is_container specialization for internal containers
Jun 15, 2022
795d3a2
Fix minor English language issues
Jun 15, 2022
f22984d
Revert "Remove unnecessary is_container specialization for internal c…
Jun 16, 2022
80ea7b7
Expose only internally the type trait for internal containers
Jun 16, 2022
f23f64d
Expose stucture related type trait through alp namespace
Jun 16, 2022
c6d6948
Remove unnecessary left-over code
Jun 16, 2022
ac4d478
Rename type traits to more intuitive names
Jun 16, 2022
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
11 changes: 11 additions & 0 deletions include/alp/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ namespace alp {
enum Backend backend = config::default_backend >
class Matrix;

/** Specializations of ALP backend-agnostic type traits */
template< typename T, typename Structure, enum Density density, typename View, typename ImfR, typename ImfC, enum Backend backend >
struct inspect_structure< Matrix< T, Structure, density, View, ImfR, ImfC, backend > > {
typedef Structure type;
};

template< typename T, typename Structure, enum Density density, typename View, typename ImfR, typename ImfC, enum Backend backend >
struct internal::inspect_view< Matrix< T, Structure, density, View, ImfR, ImfC, backend > > {
typedef View type;
};

} // namespace alp
#endif

Expand Down
19 changes: 11 additions & 8 deletions include/alp/reference/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,18 @@ namespace alp {
size_t ncols( const Matrix< T, reference > & m ) noexcept {
return m.n;
}

/**
* Identifies any reference internal matrix is an internal container.
*/
template< typename T >
struct is_container< internal::Matrix< T, reference > > : std::true_type {};

} // namespace internal

/**
* @brief A reference Matrix is an ALP object.
*/
template< typename T >
struct is_container< internal::Matrix< T, reference > > {
static const constexpr bool value = true;
};
/** Identifies any reference implementation of ALP matrix as an ALP matrix. */
template< typename T, typename Structure, enum Density density, typename View, typename ImfR, typename ImfC >
struct is_matrix< Matrix< T, Structure, density, View, ImfR, ImfC, reference > > : std::true_type {};

// Matrix-related implementation

Expand Down Expand Up @@ -903,7 +906,7 @@ namespace alp {

template < bool d >
struct view_type< view::transpose, d > {
using type = Matrix< T, structures::Square, Density::Dense, View, ImfR, ImfC, reference >;
using type = Matrix< T, structures::Square, Density::Dense, view::Transpose< self_type >, ImfR, ImfC, reference >;
};

/** Constructor for an original matrix. */
Expand Down
5 changes: 1 addition & 4 deletions include/alp/reference/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ namespace alp {

/** Identifies any reference scalar as an ALP scalar. */
template< typename T, typename Structure >
struct is_container< Scalar< T, Structure, reference > > {
/** A scalar is an ALP object. */
static const constexpr bool value = true;
};
struct is_scalar< Scalar< T, Structure, reference > > : std::true_type {};

namespace internal {
template< typename T, typename Structure >
Expand Down
19 changes: 7 additions & 12 deletions include/alp/reference/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,12 @@ namespace alp {
// }

};
} // end namespace ``alp::internal''

/** Identifies any reference vector as an ALP vector. */
template< typename T >
struct is_container< internal::Vector< T, reference > > {
/** A reference_vector is an ALP object. */
static const constexpr bool value = true;
};
/** Identifies any reference internal vector as an internal container. */
template< typename T >
struct is_container< internal::Vector< T, reference > > : std::true_type {};

} // end namespace ``alp::internal''

namespace internal {

Expand Down Expand Up @@ -451,12 +449,9 @@ namespace alp {

}; // class Vector with physical container

/** Identifies any reference vector as an ALP vector. */
/** Identifies any reference ALP vector as an ALP vector. */
template< typename T, typename Structure, typename View, typename Imf >
struct is_container< Vector< T, Structure, Density::Dense, View, Imf, reference > > {
/** A reference_vector is an ALP object. */
static const constexpr bool value = true;
};
struct is_vector< Vector< T, Structure, Density::Dense, View, Imf, reference > > : std::true_type {};

/**
* @brief Generate an original view of the input Vector. The function guarantees
Expand Down
6 changes: 6 additions & 0 deletions include/alp/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ namespace alp {
template< typename T, typename Structure = structures::General, enum Backend backend = config::default_backend >
class Scalar;

/** Specializations of ALP backend-agnostic type traits */
template< typename T, typename Structure, enum Backend backend >
struct inspect_structure< Scalar< T, Structure, backend > > {
typedef Structure type;
};

}
#endif

Expand Down
188 changes: 176 additions & 12 deletions include/alp/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,78 @@
#ifndef _H_ALP_TYPE_TRAITS
#define _H_ALP_TYPE_TRAITS

#include <type_traits>
#include <alp/views.hpp>

namespace alp {

/**
* Used to inspect whether a given type is a GraphBLAS container.
* Used to inspect whether a given type is an ALP scalar.
*
* @tparam T The type to inspect.
*
* \note An arbitrary type is not an ALP scalar.
*
*/
template< typename T >
struct is_scalar : std::false_type {};

/**
* Used to inspect whether a given type is an ALP vector.
*
* @tparam T The type to inspect.
*
* \note An arbitrary type is not an ALP vector.
*
*/
template< typename T >
struct is_vector : std::false_type {};

/**
* Used to inspect whether a given type is an ALP matrix.
*
* @tparam T The type to inspect.
*
* There are only two GraphBLAS containers:
* \note An arbitrary type is not an ALP matrix.
*
*/
template< typename T >
struct is_matrix : std::false_type {};

/**
* Used to inspect whether a given type is an ALP container.
*
* @tparam T The type to inspect.
*
* There are only three ALP containers:
* -# alp::Scalar,
* -# alp::Vector, and
* -# alp::Matrix.
*/
template< typename T >
struct is_container {
/** Base case: an arbitrary type is not a GraphBLAS object. */
static const constexpr bool value = false;
};
struct is_container : std::integral_constant<
bool,
is_scalar< T >::value || is_vector< T >::value || is_matrix< T >::value
> {};

namespace internal {

/**
* Used to inspect whether a given type is an internal container.
*
* @tparam T The type to inspect.
*
* There are only two internal containers:
* -# alp::internal::Vector, and
* -# alp::internal::Matrix.
*/
template< typename T >
struct is_container : std::false_type {};

} // namespace internal

/**
* Used to inspect whether a given type is a GraphBLAS semiring.
* Used to inspect whether a given type is an ALP semiring.
*
* @tparam T The type to inspect.
*/
Expand All @@ -52,7 +105,7 @@ namespace alp {
};

/**
* Used to inspect whether a given type is a GraphBLAS monoid.
* Used to inspect whether a given type is an ALP monoid.
*
* @tparam T The type to inspect.
*/
Expand All @@ -63,7 +116,7 @@ namespace alp {
};

/**
* Used to inspect whether a given type is a GraphBLAS operator.
* Used to inspect whether a given type is an ALP operator.
*
* @tparam T The type to inspect.
*/
Expand All @@ -74,11 +127,11 @@ namespace alp {
};

/**
* Used to inspect whether a given type is a GraphBLAS object.
* Used to inspect whether a given type is an ALP object.
*
* @tparam T The type to inspect.
*
* A GraphBLAS object is either a container, a semiring, a monoid, or an
* A ALP object is either a container, a semiring, a monoid, or an
* operator.
*
* @see #is_monoid
Expand All @@ -88,7 +141,7 @@ namespace alp {
*/
template< typename T >
struct is_object {
/** A GraphBLAS object is either a container, a semiring, a monoid, or an operator. */
/** A ALP object is either a container, a semiring, a monoid, or an operator. */
static const constexpr bool value = is_container< T >::value ||
is_semiring< T >::value ||
is_monoid< T >::value ||
Expand Down Expand Up @@ -148,6 +201,117 @@ namespace alp {

} // end namespace alp::internal

/**
* Used to get a structure type of the given ALP container
*
* @tparam T The ALP container to inspect.
*
*/
template< typename Container >
struct inspect_structure {};

namespace internal {

/**
* Used to get a View type of the given ALP container
*
* @tparam T The ALP container to inspect.
*
*/
template< typename Container >
struct inspect_view {};

/**
* Inspects whether a view corresponds to a storage-based ALP container.
*
* ALP containers can either be storage-based or functor-based.
*
* @tparam T The view to inspect.
*
* \note A Matrix is storage-based if it has
* - an original view over void, or
* - any type of view over another storage-based matrix.
*
*/
template< typename View >
struct is_view_over_storage : is_view_over_storage<
typename inspect_view< typename View::applied_to >::type
> {};

/** Original view over void is by definition storage based ALP container. */
template<>
struct is_view_over_storage< view::Original< void > > : std::true_type {};

/** Functor views are not storage-based ALP containers. */
template< typename LambdaType >
struct is_view_over_storage< view::Functor< LambdaType > > : std::false_type {};

/**
* A helper type trait for \a is_view_over_functor.
* Needed to expose the type the provided view is applied to.
*
* @tparam View The view to inspect.
* @tparam AppliedTo The type that View is applied to.
*
* @see is_view_over_functor
*
*/
template< typename View, typename AppliedTo >
struct is_view_over_functor_helper : is_view_over_functor_helper<
/** The view of the ALP container this view is applied to */
typename inspect_view< typename View::applied_to >::type,
/** What the above view is applied to */
typename inspect_view< typename View::applied_to >::type::applied_to
> {};

/** Functor view over a lambda type is by definition functor-based ALP container. */
template< typename AppliedTo >
struct is_view_over_functor_helper< view::Functor< AppliedTo >, AppliedTo > : std::true_type {};

template< typename AppliedTo >
struct is_view_over_functor_helper< view::Original< void >, AppliedTo > : std::false_type {};

/**
* Inspects whether a view corresponds to a functor-based ALP container.
*
* ALP containers can either be storage-based or functor-based.
*
* @tparam View The view to inspect.
*
* \note A Matrix is functor-based if it has
* - a functor view over a lambda type, or
* - any type of view over another functor-based matrix.
*
* @see is_view_over_functor_helper
*
*/
template< typename View >
struct is_view_over_functor : is_view_over_functor_helper< View, typename View::applied_to > {};

/**
* Inspects whether a provided view is associated with an ALP container
* that allocates the container data-related memory (either the storage
* or the functor), or, in other words,
* whether it is a view over another ALP container.
*
* @tparam T The view type to inspect.
*
* The value is true if the provided view corresponds to an ALP container that
* - allocates memory for container storage, or
* - allocates memory for a functor
* The value is false otherwise, i.e., if the provided view type corresponds
* to a view over another ALP container, and, therefore, does not need to
* allocate memory for storage/functor.
*
*/
template< typename View >
struct requires_allocation : std::integral_constant<
bool,
std::is_same< view::Original< void >, View >::value ||
std::is_same< view::Functor< typename View::applied_to >, View >::value
> {};
} // namespace internal

} // namespace alp

#endif
12 changes: 12 additions & 0 deletions include/alp/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ namespace alp {
>
class Vector;

/** Specializations of ALP backend-agnostic type traits */
template< typename T, typename Structure, enum Density density, typename View, typename Imf, enum Backend backend >
struct inspect_structure< Vector< T, Structure, density, View, Imf, backend > > {
typedef Structure type;
};

template< typename T, typename Structure, enum Density density, typename View, typename Imf, enum Backend backend >
struct internal::inspect_view< Vector< T, Structure, density, View, Imf, backend > > {
typedef View type;
};


}
#endif

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ add_grb_executables( add15m add15m.cpp
# BACKENDS alp_reference
#)

add_grb_executables( alp_type_traits alp_type_traits.cpp
BACKENDS alp_reference
)

add_grb_executables( argmax argmax.cpp
BACKENDS reference reference_omp bsp1d hybrid
)
Expand Down
Loading