From e510c5e191f5dcbeb13bf06cdedaf6367defd109 Mon Sep 17 00:00:00 2001 From: Yuuichi Asahi Date: Thu, 5 Dec 2024 03:14:01 +0900 Subject: [PATCH] abort if destroy fails --- fft/src/KokkosFFT_Cuda_plans.hpp | 8 -------- fft/src/KokkosFFT_Cuda_types.hpp | 14 ++++++++++++-- fft/src/KokkosFFT_HIP_plans.hpp | 8 -------- fft/src/KokkosFFT_HIP_types.hpp | 14 ++++++++++++-- fft/src/KokkosFFT_ROCM_plans.hpp | 6 +++--- fft/src/KokkosFFT_ROCM_types.hpp | 22 +++++++++++++++++++--- 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/fft/src/KokkosFFT_Cuda_plans.hpp b/fft/src/KokkosFFT_Cuda_plans.hpp index 7dfc00f7..b50f8c44 100644 --- a/fft/src/KokkosFFT_Cuda_plans.hpp +++ b/fft/src/KokkosFFT_Cuda_plans.hpp @@ -35,8 +35,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // cufftResult cufft_rt = cufftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(cufft_rt != CUFFT_SUCCESS, "cufftCreate failed"); cudaStream_t stream = exec_space.cuda_stream(); cufftResult cufft_rt = cufftSetStream((*plan).plan(), stream); @@ -78,8 +76,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // cufftResult cufft_rt = cufftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(cufft_rt != CUFFT_SUCCESS, "cufftCreate failed"); cudaStream_t stream = exec_space.cuda_stream(); cufftResult cufft_rt = cufftSetStream((*plan).plan(), stream); @@ -121,8 +117,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // cufftResult cufft_rt = cufftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(cufft_rt != CUFFT_SUCCESS, "cufftCreate failed"); cudaStream_t stream = exec_space.cuda_stream(); cufftResult cufft_rt = cufftSetStream((*plan).plan(), stream); @@ -186,8 +180,6 @@ auto create_plan(const ExecutionSpace& exec_space, int istride = 1, ostride = 1; plan = std::make_unique(); - // cufftResult cufft_rt = cufftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(cufft_rt != CUFFT_SUCCESS, "cufftCreate failed"); cudaStream_t stream = exec_space.cuda_stream(); cufftResult cufft_rt = cufftSetStream((*plan).plan(), stream); diff --git a/fft/src/KokkosFFT_Cuda_types.hpp b/fft/src/KokkosFFT_Cuda_types.hpp index 6869301f..ebcffd67 100644 --- a/fft/src/KokkosFFT_Cuda_types.hpp +++ b/fft/src/KokkosFFT_Cuda_types.hpp @@ -6,7 +6,9 @@ #define KOKKOSFFT_CUDA_TYPES_HPP #include +#include #include "KokkosFFT_common_types.hpp" +#include "KokkosFFT_asserts.hpp" #if defined(ENABLE_HOST_AND_DEVICE) #include "KokkosFFT_FFTW_Types.hpp" @@ -25,10 +27,18 @@ using FFTDirectionType = int; /// \brief A class that wraps cufft for RAII struct ScopedCufftPlanType { + private: cufftHandle m_plan; - ScopedCufftPlanType() { cufftCreate(&m_plan); } - ~ScopedCufftPlanType() { cufftDestroy(m_plan); } + public: + ScopedCufftPlanType() { + cufftResult cufft_rt = cufftCreate(&m_plan); + KOKKOSFFT_THROW_IF(cufft_rt != CUFFT_SUCCESS, "cufftCreate failed"); + } + ~ScopedCufftPlanType() { + cufftResult cufft_rt = cufftDestroy(m_plan); + if (cufft_rt != CUFFT_SUCCESS) Kokkos::abort("cufftDestroy failed"); + } cufftHandle &plan() { return m_plan; } }; diff --git a/fft/src/KokkosFFT_HIP_plans.hpp b/fft/src/KokkosFFT_HIP_plans.hpp index 770388e3..62766308 100644 --- a/fft/src/KokkosFFT_HIP_plans.hpp +++ b/fft/src/KokkosFFT_HIP_plans.hpp @@ -35,8 +35,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // hipfftResult hipfft_rt = hipfftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(hipfft_rt != HIPFFT_SUCCESS, "hipfftCreate failed"); hipStream_t stream = exec_space.hip_stream(); hipfftResult hipfft_rt = hipfftSetStream((*plan).plan(), stream); @@ -78,8 +76,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // hipfftResult hipfft_rt = hipfftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(hipfft_rt != HIPFFT_SUCCESS, "hipfftCreate failed"); hipStream_t stream = exec_space.hip_stream(); hipfftResult hipfft_rt = hipfftSetStream((*plan).plan(), stream); @@ -121,8 +117,6 @@ auto create_plan(const ExecutionSpace& exec_space, using out_value_type = typename OutViewType::non_const_value_type; plan = std::make_unique(); - // hipfftResult hipfft_rt = hipfftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(hipfft_rt != HIPFFT_SUCCESS, "hipfftCreate failed"); hipStream_t stream = exec_space.hip_stream(); hipfftResult hipfft_rt = hipfftSetStream((*plan).plan(), stream); @@ -186,8 +180,6 @@ auto create_plan(const ExecutionSpace& exec_space, int istride = 1, ostride = 1; plan = std::make_unique(); - // hipfftResult hipfft_rt = hipfftCreate(&(*plan)); - // KOKKOSFFT_THROW_IF(hipfft_rt != HIPFFT_SUCCESS, "hipfftCreate failed"); hipStream_t stream = exec_space.hip_stream(); hipfftResult hipfft_rt = hipfftSetStream((*plan).plan(), stream); diff --git a/fft/src/KokkosFFT_HIP_types.hpp b/fft/src/KokkosFFT_HIP_types.hpp index 2e0d1c01..269a27f4 100644 --- a/fft/src/KokkosFFT_HIP_types.hpp +++ b/fft/src/KokkosFFT_HIP_types.hpp @@ -6,7 +6,9 @@ #define KOKKOSFFT_HIP_TYPES_HPP #include +#include #include "KokkosFFT_common_types.hpp" +#include "KokkosFFT_asserts.hpp" #if defined(ENABLE_HOST_AND_DEVICE) #include "KokkosFFT_FFTW_Types.hpp" @@ -25,10 +27,18 @@ using FFTDirectionType = int; /// \brief A class that wraps hipfft for RAII struct ScopedHIPfftPlanType { + private: hipfftHandle m_plan; - ScopedHIPfftPlanType() { hipfftCreate(&m_plan); } - ~ScopedHIPfftPlanType() { hipfftDestroy(m_plan); } + public: + ScopedHIPfftPlanType() { + hipfftResult hipfft_rt = hipfftCreate(&m_plan); + KOKKOSFFT_THROW_IF(hipfft_rt != HIPFFT_SUCCESS, "hipfftCreate failed"); + } + ~ScopedHIPfftPlanType() { + hipfftResult hipfft_rt = hipfftDestroy(m_plan); + if (hipfft_rt != HIPFFT_SUCCESS) Kokkos::abort("hipfftDestroy failed"); + } hipfftHandle &plan() { return m_plan; } }; diff --git a/fft/src/KokkosFFT_ROCM_plans.hpp b/fft/src/KokkosFFT_ROCM_plans.hpp index 40459511..650b212e 100644 --- a/fft/src/KokkosFFT_ROCM_plans.hpp +++ b/fft/src/KokkosFFT_ROCM_plans.hpp @@ -170,13 +170,13 @@ auto create_plan(const ExecutionSpace& exec_space, ); KOKKOSFFT_THROW_IF(status != rocfft_status_success, "rocfft_plan_create failed"); - plan->m_is_plan_created = true; + plan->set_is_plan_created(); // Prepare workbuffer and set execution information status = rocfft_execution_info_create(&(plan->execution_info())); KOKKOSFFT_THROW_IF(status != rocfft_status_success, "rocfft_execution_info_create failed"); - plan->m_is_info_created = true; + plan->set_is_info_created(); // set stream // NOTE: The stream must be of type hipStream_t. @@ -194,7 +194,7 @@ auto create_plan(const ExecutionSpace& exec_space, if (workbuffersize > 0) { plan->allocate_work_buffer(workbuffersize); status = rocfft_execution_info_set_work_buffer( - plan->execution_info(), (void*)plan->m_buffer.data(), workbuffersize); + plan->execution_info(), (void*)plan->buffer_data(), workbuffersize); KOKKOSFFT_THROW_IF(status != rocfft_status_success, "rocfft_execution_info_set_work_buffer failed"); } diff --git a/fft/src/KokkosFFT_ROCM_types.hpp b/fft/src/KokkosFFT_ROCM_types.hpp index 5bed05d8..e21be5d5 100644 --- a/fft/src/KokkosFFT_ROCM_types.hpp +++ b/fft/src/KokkosFFT_ROCM_types.hpp @@ -7,8 +7,10 @@ #include #include +#include #include "KokkosFFT_common_types.hpp" #include "KokkosFFT_traits.hpp" +#include "KokkosFFT_asserts.hpp" #if defined(ENABLE_HOST_AND_DEVICE) #include "KokkosFFT_FFTW_Types.hpp" #endif @@ -37,6 +39,7 @@ using TransformType = FFTWTransformType; /// \brief A class that wraps rocfft for RAII template struct ScopedRocfftPlanType { + private: using floating_point_type = KokkosFFT::Impl::base_floating_point_type; rocfft_plan m_plan; rocfft_execution_info m_execution_info; @@ -44,23 +47,36 @@ struct ScopedRocfftPlanType { using BufferViewType = Kokkos::View *, ExecutionSpace>; - bool m_is_info_created = false; bool m_is_plan_created = false; + bool m_is_info_created = false; //! Internal work buffer BufferViewType m_buffer; + public: ScopedRocfftPlanType() {} ~ScopedRocfftPlanType() { - if (m_is_info_created) rocfft_execution_info_destroy(m_execution_info); - if (m_is_plan_created) rocfft_plan_destroy(m_plan); + if (m_is_info_created) { + rocfft_status status = rocfft_execution_info_destroy(m_execution_info); + if (status != rocfft_status_success) + Kokkos::abort("rocfft_execution_info_destroy failed"); + } + if (m_is_plan_created) { + rocfft_status status = rocfft_plan_destroy(m_plan); + if (status != rocfft_status_success) + Kokkos::abort("rocfft_plan_destroy failed"); + } } + void set_is_plan_created() { m_is_plan_created = true; } + void set_is_info_created() { m_is_info_created = true; } + void allocate_work_buffer(std::size_t workbuffersize) { m_buffer = BufferViewType("work buffer", workbuffersize); } rocfft_plan &plan() { return m_plan; } rocfft_execution_info &execution_info() { return m_execution_info; } + auto *buffer_data() { return m_buffer.data(); } }; // Define fft transform types