Skip to content

Commit

Permalink
Apply check functions to fft functions (#130)
Browse files Browse the repository at this point in the history
* Improve assertions in fft functions

* format

* use is_complex_v

* fix: typo

* using string_view and remove maybe_unused from assertion helper

---------

Co-authored-by: Yuuichi Asahi <[email protected]>
  • Loading branch information
yasahi-hpc and Yuuichi Asahi authored Sep 10, 2024
1 parent 0dfbae8 commit be5919c
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 163 deletions.
54 changes: 54 additions & 0 deletions common/src/KokkosFFT_asserts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: (C) The Kokkos-FFT development team, see COPYRIGHT.md file
//
// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception

#ifndef KOKKOSFFT_ASSERTS_HPP
#define KOKKOSFFT_ASSERTS_HPP

#include <stdexcept>
#include <sstream>
#include <string_view>

#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
#include <source_location>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition( \
(expression), msg, std::source_location::current().file_name(), \
std::source_location::current().line(), \
std::source_location::current().function_name(), \
std::source_location::current().column())
#else
#include <cstdlib>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition((expression), msg, __FILE__, __LINE__, \
__FUNCTION__)
#endif

namespace KokkosFFT {
namespace Impl {

inline void check_precondition(const bool expression,
const std::string_view& msg,
const char* file_name, int line,
const char* function_name,
const int column = -1) {
// Quick return if possible
if (expression) return;

std::stringstream ss("file: ");
if (column == -1) {
// For C++ 17
ss << file_name << '(' << line << ") `" << function_name << "`: " << msg
<< '\n';
} else {
// For C++ 20 and later
ss << file_name << '(' << line << ':' << column << ") `" << function_name
<< "`: " << msg << '\n';
}
throw std::runtime_error(ss.str());
}

} // namespace Impl
} // namespace KokkosFFT

#endif
37 changes: 1 addition & 36 deletions common/src/KokkosFFT_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,13 @@
#include <set>
#include <algorithm>
#include <numeric>
#include "KokkosFFT_asserts.hpp"
#include "KokkosFFT_traits.hpp"
#include "KokkosFFT_common_types.hpp"

#if defined(__cpp_lib_source_location) && __cpp_lib_source_location >= 201907L
#include <source_location>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition( \
(expression), msg, std::source_location::current().file_name(), \
std::source_location::current().line(), \
std::source_location::current().function_name(), \
std::source_location::current().column())
#else
#include <cstdlib>
#define KOKKOSFFT_EXPECTS(expression, msg) \
KokkosFFT::Impl::check_precondition((expression), msg, __FILE__, __LINE__, \
__FUNCTION__)
#endif

namespace KokkosFFT {
namespace Impl {

inline void check_precondition(const bool expression,
[[maybe_unused]] const std::string& msg,
[[maybe_unused]] const char* file_name, int line,
[[maybe_unused]] const char* function_name,
[[maybe_unused]] const int column = -1) {
// Quick return if possible
if (expression) return;

std::stringstream ss("file: ");
if (column == -1) {
// For C++ 17
ss << file_name << '(' << line << ") `" << function_name << "`: " << msg
<< '\n';
} else {
// For C++ 20 and later
ss << file_name << '(' << line << ':' << column << ") `" << function_name
<< "`: " << msg << '\n';
}
throw std::runtime_error(ss.str());
}

template <typename ViewType>
auto convert_negative_axis(ViewType, int _axis = -1) {
static_assert(Kokkos::is_view_v<ViewType>,
Expand Down
24 changes: 14 additions & 10 deletions fft/src/KokkosFFT_Cuda_plans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <numeric>
#include "KokkosFFT_Cuda_types.hpp"
#include "KokkosFFT_layouts.hpp"
#include "KokkosFFT_asserts.hpp"

namespace KokkosFFT {
namespace Impl {
Expand All @@ -30,7 +31,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
cufftResult cufft_rt = cufftCreate(&(*plan));
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftCreate failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftCreate failed");

cudaStream_t stream = exec_space.cuda_stream();
cufftSetStream((*plan), stream);
Expand All @@ -44,7 +45,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

cufft_rt = cufftPlan1d(&(*plan), nx, type, howmany);
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftPlan1d failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftPlan1d failed");

return fft_size;
}

Expand All @@ -67,7 +69,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
cufftResult cufft_rt = cufftCreate(&(*plan));
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftCreate failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftCreate failed");

cudaStream_t stream = exec_space.cuda_stream();
cufftSetStream((*plan), stream);
Expand All @@ -81,7 +83,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

cufft_rt = cufftPlan2d(&(*plan), nx, ny, type);
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftPlan2d failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftPlan2d failed");

return fft_size;
}

Expand All @@ -104,7 +107,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
cufftResult cufft_rt = cufftCreate(&(*plan));
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftCreate failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftCreate failed");

cudaStream_t stream = exec_space.cuda_stream();
cufftSetStream((*plan), stream);
Expand All @@ -120,7 +123,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

cufft_rt = cufftPlan3d(&(*plan), nx, ny, nz, type);
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftPlan3d failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftPlan3d failed");

return fft_size;
}

Expand Down Expand Up @@ -163,16 +167,16 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
cufftResult cufft_rt = cufftCreate(&(*plan));
if (cufft_rt != CUFFT_SUCCESS) throw std::runtime_error("cufftCreate failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftCreate failed");

cudaStream_t stream = exec_space.cuda_stream();
cufftSetStream((*plan), stream);

cufft_rt = cufftPlanMany(&(*plan), rank, fft_extents.data(),
in_extents.data(), istride, idist,
out_extents.data(), ostride, odist, type, howmany);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftPlanMany failed");

KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftPlanMany failed");

return fft_size;
}
Expand All @@ -186,4 +190,4 @@ void destroy_plan_and_info(std::unique_ptr<PlanType>& plan, InfoType&) {
} // namespace Impl
} // namespace KokkosFFT

#endif
#endif
21 changes: 8 additions & 13 deletions fft/src/KokkosFFT_Cuda_transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,52 @@
#define KOKKOSFFT_CUDA_TRANSFORM_HPP

#include <cufft.h>
#include "KokkosFFT_asserts.hpp"

namespace KokkosFFT {
namespace Impl {
template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftReal* idata, cufftComplex* odata,
int /*direction*/, Args...) {
cufftResult cufft_rt = cufftExecR2C(plan, idata, odata);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecR2C failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecR2C failed");
}

template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftDoubleReal* idata,
cufftDoubleComplex* odata, int /*direction*/, Args...) {
cufftResult cufft_rt = cufftExecD2Z(plan, idata, odata);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecD2Z failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecD2Z failed");
}

template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftComplex* idata, cufftReal* odata,
int /*direction*/, Args...) {
cufftResult cufft_rt = cufftExecC2R(plan, idata, odata);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecC2R failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecC2R failed");
}

template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftDoubleComplex* idata,
cufftDoubleReal* odata, int /*direction*/, Args...) {
cufftResult cufft_rt = cufftExecZ2D(plan, idata, odata);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecZ2D failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecZ2D failed");
}

template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftComplex* idata,
cufftComplex* odata, int direction, Args...) {
cufftResult cufft_rt = cufftExecC2C(plan, idata, odata, direction);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecC2C failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecC2C failed");
}

template <typename... Args>
inline void exec_plan(cufftHandle& plan, cufftDoubleComplex* idata,
cufftDoubleComplex* odata, int direction, Args...) {
cufftResult cufft_rt = cufftExecZ2Z(plan, idata, odata, direction);
if (cufft_rt != CUFFT_SUCCESS)
throw std::runtime_error("cufftExecZ2Z failed");
KOKKOSFFT_EXPECTS(cufft_rt == CUFFT_SUCCESS, "cufftExecZ2Z failed");
}
} // namespace Impl
} // namespace KokkosFFT

#endif
#endif
31 changes: 14 additions & 17 deletions fft/src/KokkosFFT_HIP_plans.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <numeric>
#include "KokkosFFT_HIP_types.hpp"
#include "KokkosFFT_layouts.hpp"
#include "KokkosFFT_asserts.hpp"

namespace KokkosFFT {
namespace Impl {
Expand All @@ -30,8 +31,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
hipfftResult hipfft_rt = hipfftCreate(&(*plan));
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftCreate failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftCreate failed");

hipStream_t stream = exec_space.hip_stream();
hipfftSetStream((*plan), stream);
Expand All @@ -45,8 +45,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

hipfft_rt = hipfftPlan1d(&(*plan), nx, type, howmany);
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftPlan1d failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftPlan1d failed");

return fft_size;
}

Expand All @@ -69,8 +69,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
hipfftResult hipfft_rt = hipfftCreate(&(*plan));
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftCreate failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftCreate failed");

hipStream_t stream = exec_space.hip_stream();
hipfftSetStream((*plan), stream);
Expand All @@ -84,8 +83,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

hipfft_rt = hipfftPlan2d(&(*plan), nx, ny, type);
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftPlan2d failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftPlan2d failed");

return fft_size;
}

Expand All @@ -108,8 +107,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
hipfftResult hipfft_rt = hipfftCreate(&(*plan));
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftCreate failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftCreate failed");

hipStream_t stream = exec_space.hip_stream();
hipfftSetStream((*plan), stream);
Expand All @@ -125,8 +123,8 @@ auto create_plan(const ExecutionSpace& exec_space,
std::multiplies<>());

hipfft_rt = hipfftPlan3d(&(*plan), nx, ny, nz, type);
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftPlan3d failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftPlan3d failed");

return fft_size;
}

Expand Down Expand Up @@ -169,8 +167,7 @@ auto create_plan(const ExecutionSpace& exec_space,

plan = std::make_unique<PlanType>();
hipfftResult hipfft_rt = hipfftCreate(&(*plan));
if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftCreate failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftCreate failed");

hipStream_t stream = exec_space.hip_stream();
hipfftSetStream((*plan), stream);
Expand All @@ -179,8 +176,8 @@ auto create_plan(const ExecutionSpace& exec_space,
in_extents.data(), istride, idist,
out_extents.data(), ostride, odist, type, howmany);

if (hipfft_rt != HIPFFT_SUCCESS)
throw std::runtime_error("hipfftPlan failed");
KOKKOSFFT_EXPECTS(hipfft_rt == HIPFFT_SUCCESS, "hipfftPlanMany failed");

return fft_size;
}

Expand All @@ -193,4 +190,4 @@ void destroy_plan_and_info(std::unique_ptr<PlanType>& plan, InfoType&) {
} // namespace Impl
} // namespace KokkosFFT

#endif
#endif
Loading

0 comments on commit be5919c

Please sign in to comment.