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

Fixed print function to work on device in certain cases #436

Merged
merged 1 commit into from
Jun 9, 2023
Merged
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
40 changes: 27 additions & 13 deletions include/matx/core/tensor_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "matx/core/make_tensor.h"
#include "matx/kernels/utility.cuh"

static constexpr bool PRINT_ON_DEVICE = false; ///< print() uses printf on device

namespace matx
{
/**
Expand Down Expand Up @@ -667,10 +669,23 @@ namespace detail {
}
}
}
} // end namespace detail

static constexpr bool PRINT_ON_DEVICE = false; ///< print() uses printf on device

template <typename Op,
typename... Args,
std::enable_if_t<((std::is_integral_v<Args>)&&...) &&
(Op::Rank() == 0 || sizeof...(Args) > 0),
bool> = true>
void DevicePrint(const Op &op, Args... dims) {
if constexpr (PRINT_ON_DEVICE) {
PrintKernel<<<1, 1>>>(op, dims...);
}
else {
auto tmpv = make_tensor<typename Op::scalar_type>(op.Shape());
(tmpv = op).run();
PrintData(tmpv, dims...);
}
}
} // end namespace detail

/**
* @brief Print a tensor's values to stdout
Expand Down Expand Up @@ -714,22 +729,21 @@ void PrintData(const Op &op, Args... dims) {
data,
reinterpret_cast<CUdeviceptr>(op.Data()));
MATX_ASSERT_STR_EXP(ret, CUDA_SUCCESS, matxCudaError, "Failed to get memory type");
MATX_ASSERT_STR(mtype == CU_MEMORYTYPE_HOST || mtype == 0, matxNotSupported, "Invalid memory type for printing");
MATX_ASSERT_STR(mtype == CU_MEMORYTYPE_HOST || mtype == 0 || mtype == CU_MEMORYTYPE_DEVICE,
matxNotSupported, "Invalid memory type for printing");

detail::InternalPrint(op, dims...);
if (mtype == CU_MEMORYTYPE_DEVICE) {
detail::DevicePrint(op, dims...);
}
else {
detail::InternalPrint(op, dims...);
}
}
else if (kind == MATX_INVALID_MEMORY || HostPrintable(kind)) {
detail::InternalPrint(op, dims...);
}
else if (DevicePrintable(kind)) {
if constexpr (PRINT_ON_DEVICE) {
PrintKernel<<<1, 1>>>(op, dims...);
}
else {
auto tmpv = make_tensor<typename Op::scalar_type>(op.Shape());
(tmpv = op).run();
PrintData(tmpv, dims...);
}
detail::DevicePrint(op, dims...);
}
}
else {
Expand Down