Page MenuHomec4science

fft_plan_manager.hh
No OneTemporary

File Metadata

Created
Sun, Nov 10, 14:28

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();
public:
virtual ~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>*,
FFTransform<Real, 3>*>>;
FFTMap plans;
static FFTPlanManager* singleton;
};
/* -------------------------------------------------------------------------- */
template <UInt dim>
FFTransform<Real, dim>&
FFTPlanManager::createPlan(Grid<Real, dim>& input,
GridHermitian<Real, dim>& output) {
static_assert(dim <= 3, "Cannot do FFT of dimension higher than 3");
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) {
#ifdef USE_CUDA
std::get<dim - 1>(plans[index]) =
new FFTransformCUFFT<Real, dim>(input, output); // TODO cuda
#else
std::get<dim - 1>(plans[index]) =
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>(plans[index]);
plans.erase(index);
}
}
/* -------------------------------------------------------------------------- */
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif /* FFT_PLAN_MANAGER_H */

Event Timeline