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

Feature/add complex cast operators #633

Merged
merged 2 commits into from
May 28, 2024
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
28 changes: 28 additions & 0 deletions include/matx/operators/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace matx
template<typename T> __MATX_INLINE__ std::string as_type_str() { return "as_type"; }
template<> __MATX_INLINE__ std::string as_type_str<float>() { return "as_float"; }
template<> __MATX_INLINE__ std::string as_type_str<double>() { return "as_double"; }
template<> __MATX_INLINE__ std::string as_type_str<cuda::std::complex<double>>() { return "cuda::std::complex<double>"; }
template<> __MATX_INLINE__ std::string as_type_str<cuda::std::complex<float>>() { return "cuda::std::complex<float>"; }
template<> __MATX_INLINE__ std::string as_type_str<int32_t>() { return "as_int32_t"; }
template<> __MATX_INLINE__ std::string as_type_str<uint32_t>() { return "as_uint32_t"; }
template<> __MATX_INLINE__ std::string as_type_str<int16_t>() { return "as_int16_t"; }
Expand Down Expand Up @@ -155,6 +157,19 @@ namespace matx
return as_type<float>(t);
};

/**
* @brief Helper function to cast an input operator to a cuda::std::complex<float>
*
* @tparam T Input type
* @param t Input operator
* @return Operator output casted to cuda::std::complex<float>
*/
template <typename T>
auto __MATX_INLINE__ as_complex_float(T t)
{
return as_type<cuda::std::complex<float>>(t);
};

/**
* @brief Helper function to cast an input operator to an double
*
Expand All @@ -168,6 +183,19 @@ namespace matx
return as_type<double>(t);
};

/**
* @brief Helper function to cast an input operator to a cuda::std::complex<double>
*
* @tparam T Input type
* @param t Input operator
* @return Operator output casted to cuda::std::complex<double>
*/
template <typename T>
auto __MATX_INLINE__ as_complex_double(T t)
{
return as_type<cuda::std::complex<double>>(t);
};

/**
* @brief Helper function to cast an input operator to an uint32_t
*
Expand Down
25 changes: 25 additions & 0 deletions test/00_operators/OperatorTests.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,31 @@ TEST(OperatorTests, Cast)
ASSERT_EQ(to(i), -4); // -4 from 126 + 126 wrap-around
}

auto c32 = make_tensor<cuda::std::complex<float>>({});
auto c64 = make_tensor<cuda::std::complex<double>>({});
auto s32 = make_tensor<float>({});
auto s64 = make_tensor<double>({});
s32.SetVals({3.0f});
s64.SetVals({5.0});

(c32 = as_complex_float(s32)).run();
(c64 = as_complex_double(s32)).run();
cudaStreamSynchronize(0);

ASSERT_EQ(c32().real(), 3.0f);
ASSERT_EQ(c32().imag(), 0.0f);
ASSERT_EQ(c64().real(), 3.0);
ASSERT_EQ(c64().imag(), 0.0);

(c32 = as_complex_float(s64)).run();
(c64 = as_complex_double(s64)).run();
cudaStreamSynchronize(0);

ASSERT_EQ(c32().real(), 5.0f);
ASSERT_EQ(c32().imag(), 0.0f);
ASSERT_EQ(c64().real(), 5.0);
ASSERT_EQ(c64().imag(), 0.0);

MATX_EXIT_HANDLER();
}

Expand Down