From 46d2faf217ae2164884681d5444bd7b05520852e Mon Sep 17 00:00:00 2001 From: Yuuichi Asahi Date: Mon, 6 Jan 2025 14:35:27 +0900 Subject: [PATCH] call fftw_cleanup_threads only once --- fft/src/KokkosFFT_FFTW_Types.hpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/fft/src/KokkosFFT_FFTW_Types.hpp b/fft/src/KokkosFFT_FFTW_Types.hpp index c605f51f..7e739d66 100644 --- a/fft/src/KokkosFFT_FFTW_Types.hpp +++ b/fft/src/KokkosFFT_FFTW_Types.hpp @@ -5,6 +5,7 @@ #ifndef KOKKOSFFT_FFTW_TYPES_HPP #define KOKKOSFFT_FFTW_TYPES_HPP +#include #include #include #include "KokkosFFT_common_types.hpp" @@ -68,13 +69,15 @@ struct ScopedFFTWPlan { std::conditional_t, fftwf_plan, fftw_plan>; plan_type m_plan; + const int m_local_id; public: template ScopedFFTWPlan(const ExecutionSpace &exec_space, int rank, const int *n, int howmany, InScalarType *in, const int *inembed, int istride, int idist, OutScalarType *out, const int *onembed, int ostride, - int odist, [[maybe_unused]] int sign, unsigned flags) { + int odist, [[maybe_unused]] int sign, unsigned flags) + : m_local_id(global_id()) { init_threads(exec_space); constexpr auto type = fftw_transform_type::type(); if constexpr (type == KokkosFFT::Impl::FFTWTransformType::R2C) { @@ -121,6 +124,13 @@ struct ScopedFFTWPlan { plan_type plan() const noexcept { return m_plan; } private: + static int global_id() { + static int global_id = 0; + static std::mutex mtx; + std::lock_guard lock(mtx); + return global_id++; + } + static void init_threads([[maybe_unused]] const ExecutionSpace &exec_space) { #if defined(KOKKOS_ENABLE_OPENMP) || defined(KOKKOS_ENABLE_THREADS) if constexpr (std::is_same_v) { - fftwf_init_threads(); + if (m_local_id == 0) fftwf_init_threads(); fftwf_plan_with_nthreads(nthreads); } else { - fftw_init_threads(); + if (m_local_id == 0) fftw_init_threads(); fftw_plan_with_nthreads(nthreads); } } @@ -143,9 +153,9 @@ struct ScopedFFTWPlan { if constexpr (std::is_same_v) { if constexpr (std::is_same_v) { - fftwf_cleanup_threads(); + if (m_local_id == 0) fftwf_cleanup_threads(); } else { - fftw_cleanup_threads(); + if (m_local_id == 0) fftw_cleanup_threads(); } } #endif