Page MenuHomec4science

fft_plan_manager.hh
No OneTemporary

File Metadata

Created
Sat, May 4, 14:40

fft_plan_manager.hh

/**
* @file
* @section LICENSE
*
* Copyright (©) 2016-2020 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef FFT_PLAN_MANAGER_H
#define FFT_PLAN_MANAGER_H
/* -------------------------------------------------------------------------- */
#include "fftransform.hh"
#include "fftransform_fftw.hh"
#include <map>
#ifdef USE_CUDA
#include "fftransform_cufft.hh"
#endif
/* -------------------------------------------------------------------------- */
namespace tamaas {
/* -------------------------------------------------------------------------- */
/**
* @brief Singleton class for FFT plan management
*/
class FFTPlanManager {
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
protected:
FFTPlanManager() = default;
public:
~FFTPlanManager();
public:
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
/// Get singleton instance
static FFTPlanManager& get();
/// Create/retrieve a plan from two surfaces
template <UInt dim>
FFTransform<Real, dim>& createPlan(Grid<Real, dim>& input,
GridHermitian<Real, dim>& output);
/// Remove all plans
void clean();
/// Destroy a plan from two surfaces
template <UInt dim>
void destroyPlan(Grid<Real, dim>& input, GridHermitian<Real, dim>& output);
/// Destroy any plan containing a given data pointer
template <typename T>
void destroyPlan(const T* some);
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
private:
using FFTMap =
std::map<std::pair<Real*, Complex*>,
std::tuple<FFTransform<Real, 1>*, FFTransform<Real, 2>*>>;
FFTMap plans;
static std::unique_ptr<FFTPlanManager> singleton;
};
/* -------------------------------------------------------------------------- */
template <UInt dim>
FFTransform<Real, dim>&
FFTPlanManager::createPlan(Grid<Real, dim>& input,
GridHermitian<Real, dim>& output) {
static_assert(dim <= 2, "Cannot do FFT of dimension higher than 2");
auto index = std::make_pair(const_cast<Real*>(input.getInternalData()),
const_cast<Complex*>(output.getInternalData()));
auto it = plans.find(index);
auto end = plans.end();
if (it == end) { // we need to create a plan
std::get<dim - 1>(plans[index]) =
#ifdef USE_CUDA
new FFTransformCUFFT<Real, dim>(input, output);
#else
new FFTransformFFTW<Real, dim>(input, output);
#endif
}
return *std::get<dim - 1>(plans[index]);
}
/* -------------------------------------------------------------------------- */
template <UInt dim>
void FFTPlanManager::destroyPlan(Grid<Real, dim>& input,
GridHermitian<Real, dim>& output) {
auto index = std::make_pair(const_cast<Real*>(input.getInternalData()),
const_cast<Complex*>(output.getInternalData()));
auto it = plans.find(index);
auto end = plans.end();
if (it != end) {
delete std::get<dim - 1>(plans[index]);
plans.erase(index);
}
}
/* -------------------------------------------------------------------------- */
} // namespace tamaas
/* -------------------------------------------------------------------------- */
#endif /* FFT_PLAN_MANAGER_H */

Event Timeline