ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKFFT.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <config/config.hpp>
23
24#ifdef ESPRESSO_WALBERLA_FFT
25
26#include "EKPoissonSolver.hpp"
27
28#include "LatticeWalberla.hpp"
29
30#include "core/MpiCallbacks.hpp"
32
35
39
41
43
44#include <memory>
45#include <string>
46#include <vector>
47
49
50class EKFFT : public EKPoissonSolver {
51protected:
52 std::unique_ptr<ResourceManager> m_resources_lock;
54 bool m_gpu;
56
57protected:
58 void make_instance(VariantMap const &args) override {
59 // unit conversions
60 auto const agrid = get_value<double>(m_lattice->get_parameter("agrid"));
61 auto const tau = get_value<double>(args, "tau");
62 m_tau = tau;
63 m_conv_permittivity = Utils::int_pow<3>(agrid) / Utils::int_pow<2>(tau);
65 auto const permittivity =
67 auto *make_new_instance = &::walberla::new_ek_poisson_fft;
68 if (m_gpu) {
69 std::vector<std::string> required_features;
70 required_features.emplace_back("CUDA");
72#ifdef ESPRESSO_CUDA
73 make_new_instance = &::walberla::new_ek_poisson_fft_cuda;
74#endif
75 }
76 m_instance = make_new_instance(m_lattice->lattice(), permittivity,
78 {
79#ifdef ESPRESSO_FPE
80 // cuFFT builds device kernels using CUDA-JIT
81 // (https://docs.nvidia.com/cuda/archive/13.1.1/cufft/#plan-initialization-time)
82 // please note this operation is not guaranteed to succeed for all
83 // mesh sizes, and in rare cases, it can send the SIGFPE signal
85#endif
86 auto const use_gpu_aware =
87 m_gpu and ::communication_environment->is_mpi_gpu_aware();
88 m_instance->setup_fft(use_gpu_aware);
89 }
90 }
91
92public:
93 void do_construct(VariantMap const &args) override {
94 m_gpu = get_value_or<bool>(args, "gpu", false);
95 m_single_precision = get_value_or<bool>(args, "single_precision", m_gpu);
99 make_instance(args), m_resources_lock = std::make_unique<ResourceManager>();
100 // MPI communicator is needed to destroy the FFT plans
101 m_resources_lock->acquire_lock(::communication_environment->get_mpi_env());
102 for (auto &vtk : m_vtk_writers) {
103 vtk->attach_to_lattice(m_instance, get_lattice_to_md_units_conversion());
104 }
105 }
106
109 {"tau", AutoParameter::read_only, [this]() { return m_tau; }},
110 {"permittivity",
111 [this](Variant const &v) {
112 m_instance->set_permittivity(get_value<double>(v) *
114 },
115 [this]() {
116 return m_instance->get_permittivity() / m_conv_permittivity;
117 }},
118 {"single_precision", AutoParameter::read_only,
119 [this]() { return m_single_precision; }},
120 {"gpu", AutoParameter::read_only, [this]() { return m_gpu; }},
121 {"lattice", AutoParameter::read_only, [this]() { return m_lattice; }},
122 {"shape", AutoParameter::read_only,
123 [this]() { return m_instance->get_lattice().get_grid_dimensions(); }},
124 {"vtk_writers", AutoParameter::read_only,
125 [this]() { return serialize_vtk_writers(); }},
126 });
127 }
128
129 ~EKFFT() override {
130 m_lattice.reset();
131 m_instance.reset();
132 m_resources_lock.reset();
133 }
134
135 [[nodiscard]] std::shared_ptr<::walberla::PoissonSolver>
137 return m_instance;
138 }
139};
140
141} // namespace ScriptInterface::walberla
142
143#endif // ESPRESSO_WALBERLA_FFT
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
void add_parameters(std::vector< AutoParameter > &&params)
std::unique_ptr< ResourceManager > m_resources_lock
Definition EKFFT.hpp:52
void do_construct(VariantMap const &args) override
Definition EKFFT.hpp:93
void make_instance(VariantMap const &args) override
Definition EKFFT.hpp:58
std::shared_ptr<::walberla::PoissonSolver > get_instance() const noexcept override
Definition EKFFT.hpp:136
void set_potential_conversion(double agrid, double tau)
::LatticeModel::units_map get_lattice_to_md_units_conversion() const override
static std::shared_ptr< scoped_pause > make_shared_pause_scoped()
Generate a shared handle to temporarily disable any currently active exception trap for the lifetime ...
Definition fe_trap.cpp:144
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
std::unique_ptr< CommunicationEnvironment > communication_environment
This file contains the asynchronous MPI communication.
void check_features(std::vector< std::string > const &features)
Definition CodeInfo.cpp:74
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
std::shared_ptr< walberla::PoissonSolver > new_ek_poisson_fft(std::shared_ptr< LatticeWalberla > const &lattice, double permittivity, bool single_precision)
std::shared_ptr< walberla::PoissonSolver > new_ek_poisson_fft_cuda(std::shared_ptr< LatticeWalberla > const &lattice, double permittivity, bool single_precision)
static constexpr const ReadOnly read_only
Recursive variant implementation.
Definition Variant.hpp:84