Skip to content

Commit

Permalink
review updates:
Browse files Browse the repository at this point in the history
- rename extension base header
- update ginkgo header script
- use allocator when creating executor
- renaming
- documentation

Co-authored-by: Yu-Hsiang M. Tsai <yhmtsai@gmail.com>
  • Loading branch information
MarcelKoch and yhmtsai committed Aug 2, 2023
1 parent 584c027 commit 05588a0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 71 deletions.
3 changes: 2 additions & 1 deletion dev_tools/scripts/update_ginkgo_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ fi
# Requires detected files (including the path) to not contain newlines
if ! find "${TOP_HEADER_FOLDER}" -name '*.hpp' -type f -print | \
grep -v 'residual_norm_reduction.hpp' | \
grep -v 'solver/.*_trs.hpp' > "${HEADER_LIST}"; then
grep -v 'solver/.*_trs.hpp' | \
grep -v 'ginkgo/extensions' > "${HEADER_LIST}"; then
echo "${WARNING_PREFIX} "'The `find` command returned with an error!' 1>&2
rm "${RM_PARAMETER}" "${HEADER_LIST}"
exit 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <omp.h>
#include <Kokkos_Core.hpp>
#include <ginkgo/extensions.hpp>
#include <ginkgo/extensions/kokkos.hpp>
#include <ginkgo/ginkgo.hpp>


Expand Down Expand Up @@ -167,9 +167,7 @@ int main(int argc, char* argv[])
auto rhs = vec::create(exec, gko::dim<2>(discretization_points, 1));
generate_rhs(f, u0, u1, rhs.get());
auto u = vec::create(exec, gko::dim<2>(discretization_points, 1));
for (int i = 0; i < u->get_size()[0]; ++i) {
u->get_values()[i] = 0.0;
}
u->fill(0.0);

// initialize the stencil matrix
auto A = share(mtx::create(
Expand Down
5 changes: 5 additions & 0 deletions include/ginkgo/core/base/native_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


namespace gko {


/**
* @brief This namespace contains views on the internal data of selected
* Ginkgo types.
Expand All @@ -56,6 +58,7 @@ namespace gko {
*/
namespace layout {


/**
* A view of gko::device_matrix_data.
*
Expand Down Expand Up @@ -270,5 +273,7 @@ struct native {
}
};


} // namespace gko

#endif // GKO_PUBLIC_CORE_BASE_NATIVE_TYPE_HPP_
File renamed without changes.
136 changes: 82 additions & 54 deletions include/ginkgo/extensions/kokkos/spaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,48 +67,41 @@ struct compatible_space<Kokkos::HostSpace, gko::ReferenceExecutor>
: std::true_type {};

template <typename MemorySpace>
struct compatible_space<MemorySpace, gko::ReferenceExecutor>
: std::integral_constant<
bool, Kokkos::SpaceAccessibility<Kokkos::HostSpace,
MemorySpace>::accesible>::value {};

struct compatible_space<MemorySpace, gko::ReferenceExecutor> {
// need manual implementation of std::integral_constant because,
// while compiling for cuda, somehow bool is replaced by __nv_bool
static constexpr bool value =
Kokkos::SpaceAccessibility<Kokkos::HostSpace, MemorySpace>::accessible;
};
#ifdef KOKKOS_ENABLE_OPENMP
template <typename MemorySpace>
struct compatible_space<MemorySpace, gko::OmpExecutor>
: compatible_space<MemorySpace, gko::ReferenceExecutor> {};
#endif

#ifdef KOKKOS_ENABLE_CUDA
template <>
struct compatible_space<Kokkos::CudaSpace, gko::CudaExecutor> : std::true_type {
template <typename MemorySpace>
struct compatible_space<MemorySpace, gko::CudaExecutor> {
static constexpr bool value =
Kokkos::SpaceAccessibility<Kokkos::Cuda, MemorySpace>::accessible;
};

template <>
struct compatible_space<Kokkos::CudaHostPinnedSpace, gko::CudaExecutor>
: std::true_type {};

template <>
struct compatible_space<Kokkos::CudaUVMSpace, gko::CudaExecutor>
: std::true_type {};
#endif

#ifdef KOKKOS_ENABLE_HIP
template <>
struct compatible_space<Kokkos::HIPSpace, gko::HipExecutor> : std::true_type {};

template <>
struct compatible_space<Kokkos::HIPHostPinnedSpace, gko::HipExecutor>
: std::true_type {};

template <>
struct compatible_space<Kokkos::HIPManagedSpace, gko::HipExecutor>
: std::true_type {};
template <typename MemorySpace>
struct compatible_space<MemorySpace, gko::HipExecutor> {
static constexpr bool value =
Kokkos::SpaceAccessibility<Kokkos::HIP, MemorySpace>::accessible;
};
#endif

#ifdef KOKKOS_ENABLE_SYCL
template <>
struct compatible_space<Kokkos::SYCLSpace, gko::DpcppExecutor>
: std::true_type {};
template <typename MemorySpace>
struct compatible_space<MemorySpace, gko::DpcppExecutor> {
static constexpr bool value =
Kokkos::SpaceAccessibility<Kokkos::Experimental::SYCL,
MemorySpace>::accessible;
};
#endif


Expand All @@ -121,7 +114,7 @@ struct compatible_space<Kokkos::SYCLSpace, gko::DpcppExecutor>
* @return true if the memory space is accessible by the executor
*/
template <typename MemorySpace, typename ExecType>
bool check_compatibility(MemorySpace, const ExecType*)
inline bool check_compatibility(MemorySpace, std::shared_ptr<const ExecType>)
{
return detail::compatible_space<MemorySpace, ExecType>::value;
}
Expand All @@ -137,21 +130,23 @@ bool check_compatibility(MemorySpace, const ExecType*)
*/

template <typename MemorySpace>
bool check_compatibility(MemorySpace, const gko::Executor* exec)
inline bool check_compatibility(MemorySpace,
std::shared_ptr<const gko::Executor> exec)
{
if (auto p = dynamic_cast<const gko::ReferenceExecutor*>(exec)) {
if (auto p =
std::dynamic_pointer_cast<const gko::ReferenceExecutor>(exec)) {
return check_compatibility(MemorySpace{}, p);
}
if (auto p = dynamic_cast<const gko::OmpExecutor*>(exec)) {
if (auto p = std::dynamic_pointer_cast<const gko::OmpExecutor>(exec)) {
return check_compatibility(MemorySpace{}, p);
}
if (auto p = dynamic_cast<const gko::CudaExecutor*>(exec)) {
if (auto p = std::dynamic_pointer_cast<const gko::CudaExecutor>(exec)) {
return check_compatibility(MemorySpace{}, p);
}
if (auto p = dynamic_cast<const gko::HipExecutor*>(exec)) {
if (auto p = std::dynamic_pointer_cast<const gko::HipExecutor>(exec)) {
return check_compatibility(MemorySpace{}, p);
}
if (auto p = dynamic_cast<const gko::DpcppExecutor*>(exec)) {
if (auto p = std::dynamic_pointer_cast<const gko::DpcppExecutor>(exec)) {
return check_compatibility(MemorySpace{}, p);
}
GKO_NOT_IMPLEMENTED;
Expand All @@ -170,9 +165,9 @@ bool check_compatibility(MemorySpace, const gko::Executor* exec)
* @param space The Kokkos memory space to compare agains.
*/
template <typename MemorySpace, typename T>
void ensure_compatibility(T&& obj, MemorySpace space)
inline void ensure_compatibility(T&& obj, MemorySpace space)
{
if (!check_compatibility(space, obj.get_executor().get())) {
if (!check_compatibility(space, obj.get_executor())) {
throw gko::Error(__FILE__, __LINE__,
"Executor type and memory space are incompatible");
}
Expand All @@ -189,17 +184,17 @@ void ensure_compatibility(T&& obj, MemorySpace space)
*
* @return An executor of type either ReferenceExecutor or OmpExecutor.
*/
std::shared_ptr<Executor> create_default_host_executor()
inline std::shared_ptr<Executor> create_default_host_executor()
{
#ifdef KOKKOS_ENABLE_SERIAL
if (std::is_same<Kokkos::DefaultHostExecutionSpace,
Kokkos::Serial>::value) {
if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
Kokkos::Serial>) {
return ReferenceExecutor::create();
}
#endif
#ifdef KOKKOS_ENABLE_OPENMP
if (std::is_same<Kokkos::DefaultHostExecutionSpace,
Kokkos::OpenMP>::value) {
if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace,
Kokkos::OpenMP>) {
return OmpExecutor::create();
}
#endif
Expand All @@ -224,33 +219,66 @@ std::shared_ptr<Executor> create_default_host_executor()
*
* @return An executor matching the type of the ExecSpace.
*/
template <typename ExecSpace>
std::shared_ptr<Executor> create_executor(ExecSpace)
template <typename ExecSpace,
typename MemorySpace = typename ExecSpace::memory_space>
inline std::shared_ptr<Executor> create_executor(ExecSpace, MemorySpace = {})
{
static_assert(
Kokkos::SpaceAccessibility<ExecSpace, MemorySpace>::accessible);
#ifdef KOKKOS_ENABLE_SERIAL
if (std::is_same<ExecSpace, Kokkos::Serial>::value) {
if constexpr (std::is_same_v<ExecSpace, Kokkos::Serial>) {
return ReferenceExecutor::create();
}
#endif
#ifdef KOKKOS_ENABLE_OPENMP
if (std::is_same<ExecSpace, Kokkos::OpenMP>::value) {
if constexpr (std::is_same_v<ExecSpace, Kokkos::OpenMP>) {
return OmpExecutor::create();
}
#endif
#ifdef KOKKOS_ENABLE_CUDA
if (std::is_same<ExecSpace, Kokkos::Cuda>::value) {
return CudaExecutor::create(Kokkos::device_id(),
create_default_host_executor());
if constexpr (std::is_same_v<ExecSpace, Kokkos::Cuda>) {
if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaSpace>) {
return CudaExecutor::create(Kokkos::device_id(),
create_default_host_executor(),
std::make_shared<gko::CudaAllocator>());
}
if constexpr (std::is_same_v<MemorySpace, Kokkos::CudaUVMSpace>) {
return CudaExecutor::create(
Kokkos::device_id(), create_default_host_executor(),
std::make_shared<gko::CudaUnifiedAllocator>(
Kokkos::device_id()));
}
if constexpr (std::is_same_v<MemorySpace,
Kokkos::CudaHostPinnedSpace>) {
return CudaExecutor::create(
Kokkos::device_id(), create_default_host_executor(),
std::make_shared<gko::CudaHostAllocator>(Kokkos::device_id()));
}
}
#endif
#ifdef KOKKOS_ENABLE_HIP
if (std::is_same<ExecSpace, Kokkos::HIP>::value) {
return HipExecutor::create(Kokkos::device_id(),
create_default_host_executor());
if constexpr (std::is_same_v<ExecSpace, Kokkos::HIP>) {
if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPSpace>) {
return HipExecutor::create(Kokkos::device_id(),
create_default_host_executor(),
std::make_shared<gko::HipAllocator>());
}
if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPManagedSpace>) {
return HipExecutor::create(
Kokkos::device_id(), create_default_host_executor(),
std::make_shared<gko::HipUnifiedAllocator>(
Kokkos::device_id()));
}
if constexpr (std::is_same_v<MemorySpace, Kokkos::HIPHostPinnedSpace>) {
return HipExecutor::create(
Kokkos::device_id(), create_default_host_executor(),
std::make_shared<gko::HipHostAllocator>(Kokkos::device_id()));
}
}
#endif
#ifdef KOKKOS_ENABLE_SYCL
if (std::is_same<ExecSpace, Kokkos::Experimental::SYCL>::value) {
if constexpr (std::is_same_v<ExecSpace, Kokkos::Experimental::SYCL>) {
// for now Ginkgo doesn't support different allocators for SYCL
return DpcppExecutor::create(Kokkos::device_id(),
create_default_host_executor());
}
Expand All @@ -264,7 +292,7 @@ std::shared_ptr<Executor> create_executor(ExecSpace)
*
* @return An executor matching the type of Kokkos::DefaultExecutionSpace.
*/
std::shared_ptr<Executor> create_default_executor()
inline std::shared_ptr<Executor> create_default_executor()
{
return create_executor(Kokkos::DefaultExecutionSpace{});
}
Expand Down
47 changes: 35 additions & 12 deletions include/ginkgo/extensions/kokkos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ namespace detail {
* @tparam T An arithmetic type.
*/
template <typename T>
struct native_type {
struct native_value_type {
using type = T;
};

template <typename T>
struct native_type<std::complex<T>> {
struct native_value_type<std::complex<T>> {
using type = Kokkos::complex<T>;
};

template <typename T>
struct native_type<const std::complex<T>> {
struct native_value_type<const std::complex<T>> {
using type = const Kokkos::complex<T>;
};

Expand All @@ -88,7 +88,7 @@ template <typename MemorySpace>
struct array_mapper {
template <typename ValueType>
using type =
Kokkos::View<typename detail::native_type<ValueType>::type*,
Kokkos::View<typename detail::native_value_type<ValueType>::type*,
MemorySpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>;

template <typename ValueType>
Expand Down Expand Up @@ -123,9 +123,10 @@ struct array_mapper {
template <typename MemorySpace>
struct dense_mapper {
template <typename ValueType>
using type = Kokkos::View<typename detail::native_type<ValueType>::type**,
Kokkos::LayoutStride, MemorySpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
using type =
Kokkos::View<typename detail::native_value_type<ValueType>::type**,
Kokkos::LayoutStride, MemorySpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>;

template <typename ValueType>
static type<ValueType> map(gko::matrix::Dense<ValueType>& mtx)
Expand Down Expand Up @@ -161,7 +162,7 @@ struct dense_mapper {

//!< specialization of gko::native for Kokkos
template <typename MemorySpace>
using native_type =
using kokkos_type =
gko::native<array_mapper<MemorySpace>, dense_mapper<MemorySpace>>;


Expand All @@ -170,24 +171,46 @@ using native_type =
*
* @tparam T The Ginkgo type.
* @tparam MemorySpace The Kokkos memory space that will be used
*
* @param data The Ginkgo object.
*
* @return A wrapper for the Ginkgo object that is compatible with Kokkos
*/
template <typename T,
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
auto map_data(T* data, MemorySpace = {})
inline auto map_data(T* data, MemorySpace = {})
{
return kokkos_type<MemorySpace>::map(*data);
}

/**
* @copydoc map_data(T*, MemorySpace)
*/
template <typename T,
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
inline auto map_data(std::unique_ptr<T>& data, MemorySpace = {})
{
return kokkos_type<MemorySpace>::map(*data);
}

/**
* @copydoc map_data(T*, MemorySpace)
*/
template <typename T,
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
inline auto map_data(std::shared_ptr<T>& data, MemorySpace = {})
{
return native_type<MemorySpace>::map(*data);
return kokkos_type<MemorySpace>::map(*data);
}

/**
* @copydoc map_data(T*, MemorySpace)
*/
template <typename T,
typename MemorySpace = Kokkos::DefaultExecutionSpace::memory_space>
auto map_data(T&& data, MemorySpace = {})
inline auto map_data(T&& data, MemorySpace = {})
{
return native_type<MemorySpace>::map(std::forward<T>(data));
return kokkos_type<MemorySpace>::map(std::forward<T>(data));
}


Expand Down

0 comments on commit 05588a0

Please sign in to comment.