Page MenuHomec4science

fft_plan_manager.hh
No OneTemporary

File Metadata

Created
Mon, May 27, 15:46

fft_plan_manager.hh

/**
* @file
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://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
/* -------------------------------------------------------------------------- */
__BEGIN_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);
}
}
/* -------------------------------------------------------------------------- */
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif /* FFT_PLAN_MANAGER_H */

Event Timeline