25#ifndef __SIMULATION_CONTEXT_HPP__
26#define __SIMULATION_CONTEXT_HPP__
30#include <spla/spla.hpp>
45extern "C" void generate_phase_factors_gpu(
int num_gvec_loc__,
int num_atoms__,
int const* gvec__,
46 double const* atom_pos__, std::complex<double>* phase_factors__);
51template <
typename OUT>
53print_memory_usage(OUT&& out__, std::string file_and_line__ =
"")
55 if (!env::print_memory_usage()) {
59 auto res = get_proc_status();
62 s <<
"rank" << std::setfill(
'0') << std::setw(4) << mpi::Communicator::world().rank();
63 out__ <<
"[" << s.str() <<
" at " << file_and_line__ <<
"] "
64 <<
"VmHWM: " << (res.VmHWM >> 20) <<
" Mb, "
65 <<
"VmRSS: " << (res.VmRSS >> 20) <<
" Mb";
68 size_t gpu_mem = acc::get_free_mem();
69 out__ <<
", GPU: " << (gpu_mem >> 20) <<
" Mb";
73 std::vector<std::string> labels = {
"host"};
74 std::vector<sddk::memory_pool*> mp = {&get_memory_pool(sddk::memory_t::host)};
78 labels.push_back(
"host pinned");
79 labels.push_back(
"device");
80 mp.push_back(&get_memory_pool(sddk::memory_t::host_pinned));
81 mp.push_back(&get_memory_pool(sddk::memory_t::device));
85 for (
int i = 0; i < np; i++) {
86 out__ <<
"[mem.pool] " << labels[i] <<
": total capacity: " << (mp[i]->total_size() >> 20) <<
" Mb, "
87 <<
"free: " << (mp[i]->free_size() >> 20) <<
" Mb, "
88 <<
"num.blocks: " << mp[i]->
num_blocks() << std::endl;
96 std::function<void(
int,
double,
double*,
int)>
beta_ri_{
nullptr};
103 std::function<void(
int,
double,
double*,
int,
int)>
aug_ri_{
nullptr};
107 std::function<void(
int,
double,
double*,
int,
int)>
aug_ri_djl_{
nullptr};
110 std::function<void(
int,
int,
double*,
double*)>
rhoc_ri_{
nullptr};
117 std::function<void(
int,
int,
double*,
double*)>
ps_rho_ri_{
nullptr};
127 std::function<void(
int,
int,
double*,
double*)>
vloc_ri_{
nullptr};
136 std::function<void(
void)>
veff_{
nullptr};
143 std::unique_ptr<Radial_integrals_beta<false>>
beta_;
149 std::unique_ptr<Radial_integrals_aug<false>>
aug_;
152 std::unique_ptr<Radial_integrals_aug<true>>
aug_djl_;
161 std::unique_ptr<Radial_integrals_rho_core_pseudo<false>>
ps_core_;
167 std::unique_ptr<Radial_integrals_rho_pseudo>
ps_rho_;
170 std::unique_ptr<Radial_integrals_vloc<false>>
vloc_;
210 std::unique_ptr<spfft::Grid> spfft_grid_;
211#if defined(SIRIUS_USE_FP32)
212 std::unique_ptr<spfft::TransformFloat> spfft_transform_float_;
213 std::unique_ptr<spfft::GridFloat> spfft_grid_float_;
221 std::unique_ptr<spfft::Grid> spfft_grid_coarse_;
222#if defined(SIRIUS_USE_FP32)
223 std::unique_ptr<spfft::TransformFloat> spfft_transform_coarse_float_;
224 std::unique_ptr<spfft::GridFloat> spfft_grid_coarse_float_;
230 std::shared_ptr<fft::Gvec_fft> gvec_fft_;
235 std::shared_ptr<fft::Gvec_fft> gvec_coarse_fft_;
237 std::shared_ptr<fft::Gvec_shells> remap_gvec_;
284 std::shared_ptr<::spla::Context>
spla_ctx_{new ::spla::Context{SPLA_PU_HOST}};
286 std::ostream* output_stream_{
nullptr};
287 std::ofstream output_file_stream_;
290 std::map<std::string, periodic_function_ptr_t<double>>
pf_ext_ptr;
298 mutable double evp_work_count_{0};
299 mutable int num_loc_op_applied_{0};
323 this->
import(env::config_file());
340 , comm_band_(comm_band__)
347 :
comm_(mpi::Communicator::world())
355 :
comm_(mpi::Communicator::world())
363 Simulation_context(std::string
const& str__, mpi::Communicator
const& comm__)
375 print_memory_usage(this->
out(), FILE_LINE);
382 void print_info(std::ostream& out__)
const;
387 auto const& atoms_to_grid_idx_map(
int ia__)
const
427 auto const& gvec_coarse()
const
432 auto const& gvec_coarse_sptr()
const
437 auto const& gvec_coarse_fft_sptr()
const
439 return gvec_coarse_fft_;
442 auto const& remap_gvec()
const
447 auto const& blacs_grid()
const
481 auto const& comm_ortho_fft()
const
483 return mpi::Communicator::self();
490 if (cfg().control().fft_mode() ==
"serial") {
491 return mpi::Communicator::self();
502 if (cfg().control().fft_mode() ==
"serial") {
509 auto const& comm_ortho_fft_coarse()
const
514 void create_storage_file(std::string name__)
const;
516 inline std::string
const& start_time_tag()
const
521 inline auto& std_evp_solver()
526 inline auto const& std_evp_solver()
const
531 inline auto& gen_evp_solver()
536 inline auto const& gen_evp_solver()
const
541 inline auto phase_factors_t(
int igloc__,
int iat__)
const
546 inline auto const& phase_factors_t()
const
563 inline auto const& gvec_coord()
const
579 auto const& sym_phase_factors()
const
584 inline bool initialized()
const
624 return (this->processing_unit() == sddk::device_t::CPU) ? sddk::memory_t::host : sddk::memory_t::device;
630 cfg().settings().fft_grid_size(fft_grid_size__);
633 template <
typename T>
634 fft::spfft_grid_type<T>& spfft_grid_coarse();
636 template <
typename T>
637 fft::spfft_transform_type<T>& spfft();
639 template <
typename T>
640 fft::spfft_transform_type<T>
const& spfft()
const;
642 template <
typename T>
643 fft::spfft_transform_type<T>& spfft_coarse();
645 template <
typename T>
646 fft::spfft_transform_type<T>
const& spfft_coarse()
const;
648 auto const& fft_grid()
const
653 auto const& fft_coarse_grid()
const
658 auto const& spla_context()
const
668 inline double evp_work_count(
double w__ = 0)
const
670 evp_work_count_ += w__;
671 return evp_work_count_;
677 num_loc_op_applied_ += n;
678 return num_loc_op_applied_;
681 inline int num_itsol_steps(
int n = 0)
const
692 inline auto const& cb()
const
702 inline auto const& ri()
const
707 inline std::function<void(
void)> band_occ_callback()
const
712 inline std::function<void(
void)> veff_callback()
const
721 dict[
"config"] = cfg().dict();
722 bool const cart_pos{
false};
723 dict[
"config"][
"unit_cell"] = unit_cell().serialize(cart_pos);
726 dict[
"fft_coarse_grid"] = fftgrid;
728 dict[
"omega"] = unit_cell().omega();
729 dict[
"chemical_formula"] = unit_cell().chemical_formula();
730 dict[
"num_atoms"] = unit_cell().num_atoms();
735 inline std::ostream&
out()
const
737 RTE_ASSERT(output_stream_ !=
nullptr);
738 return *output_stream_;
742 inline std::ostream&
out(
int level__)
const
744 if (this->verbosity() >= level__) {
747 return null_stream();
753 if (this->verbosity() >= level__) {
756 return rte::ostream(null_stream(), label__);
761 inline void message(
int level__,
char const* label__, std::stringstream
const& s)
const
763 if (this->verbosity() >= level__) {
764 auto strings =
split(s.str(),
'\n');
765 for (
auto& e : strings) {
766 this->
out() <<
"[" << label__ <<
"] " << e << std::endl;
776 inline auto periodic_function_ptr(std::string label__)
const
778 periodic_function_ptr_t<double>
const* ptr{
nullptr};
Interface to accelerators API.
Contains implementation of sirius::Augmentation_operator class.
Simulation context is a set of parameters and objects describing a single simulation.
Simulation_context(std::string const &str__)
Create a simulation context with world communicator and load parameters from JSON string or JSON file...
std::unique_ptr< spfft::Transform > spfft_transform_
Fine-grained FFT for density and potential.
auto gvec_phase_factor(r3::vector< int > G__, int ia__) const
Phase factors .
std::vector< std::vector< std::pair< int, double > > > atoms_to_grid_idx_
List of real-space point indices for each of the atoms.
sddk::mdarray< int, 2 > gvec_coord_
Lattice coordinats of G-vectors in a GPU-friendly ordering.
mpi::Communicator const & comm_
Communicator for this simulation.
nlohmann::json serialize()
Export parameters of simulation context as a JSON dictionary.
void init_atoms_to_grid_idx(double R__)
Find a list of real-space grid points around each atom.
mpi::Communicator comm_ortho_fft_coarse_
Auxiliary communicator for the coarse-grid FFT transformation.
timeval start_time_
Creation time of the parameters.
std::string start_time_tag_
A tag string based on the the starting time.
auto const & gvec() const
Return const reference to Gvec object.
~Simulation_context()
Destructor.
auto const & comm_band() const
Band parallelization communicator.
void init_common()
Common init function called by all constructors.
double omega0_
Volume of the initial unit cell.
std::unique_ptr< Unit_cell > unit_cell_
Unit cell of the simulation.
int num_itsol_steps_
Total number of iterative solver steps.
auto const & augmentation_op(int iat__) const
Returns a constant pointer to the augmentation operator of a given atom type.
auto processing_unit_memory_t() const
Return the memory type for processing unit.
void fft_grid_size(std::array< int, 3 > fft_grid_size__)
Set the size of the fine-grained FFT grid.
auto const & comm_fft_coarse() const
Communicator of the coarse FFT grid.
sddk::mdarray< std::complex< double >, 3 > sym_phase_factors_
1D phase factors of the symmetry operations.
void update()
Update context after setting new lattice vectors or atomic coordinates.
std::shared_ptr< fft::Gvec > gvec_coarse_
G-vectors within the 2 * |Gmax^{WF}| cutoff.
void init_comm()
Initialize communicators.
std::shared_ptr< fft::Gvec > gvec_
G-vectors within the Gmax cutoff.
std::vector< std::unique_ptr< Augmentation_operator > > augmentation_op_
Augmentation operator for each atom type.
void init_fft_grid()
Initialize FFT coarse and fine grids.
callback_functions_t cb_
Stores all callback functions.
auto gvec_phase_factor(int ig__, int ia__) const
Phase factors .
void initialize()
Initialize the similation (can only be called once).
sddk::mdarray< std::complex< double >, 3 > phase_factors_
1D phase factors for each atom coordinate and G-vector index.
std::unique_ptr< la::BLACS_grid > blacs_grid_
2D BLACS grid for distributed linear algebra operations.
auto const & unit_cell() const
Return const reference to unit cell object.
std::map< std::string, periodic_function_ptr_t< double > > pf_ext_ptr
External pointers to periodic functions.
auto const & comm_fft() const
Communicator of the dense FFT grid.
auto host_memory_t() const
Type of the host memory for arrays used in linear algebra operations.
std::ostream & out(int level__) const
Return output stream based on the verbosity level.
auto const & comm_k() const
Communicator between k-points.
std::shared_ptr<::spla::Context > spla_ctx_
SPLA library context.
void generate_phase_factors(int iat__, sddk::mdarray< std::complex< double >, 2 > &phase_factors__) const
Generate phase factors for all atoms of a given type.
std::unique_ptr< la::Eigensolver > std_evp_solver_
Standard eigen-value problem solver.
sddk::memory_t host_memory_t_
Type of host memory (pagable or page-locked) for the arrays that participate in host-to-device memory...
auto const & gvec_fft() const
Return const reference to Gvec_fft object.
Simulation_context(mpi::Communicator const &comm__=mpi::Communicator::world())
Create an empty simulation context with an explicit communicator.
auto gvec_fft_sptr() const
Return shared pointer to Gvec_fft object.
std::unique_ptr< spfft::Transform > spfft_transform_coarse_
Coarse-grained FFT for application of local potential and density summation.
r3::matrix< double > lattice_vectors0_
Initial lattice vectors.
std::ostream & out() const
Return output stream.
sddk::mdarray< std::complex< double >, 2 > phase_factors_t_
Phase factors for atom types.
double theta(int ir__) const
Return the value of the step function for the grid point ir.
double ewald_lambda() const
Find the lambda parameter used in the Ewald summation.
auto const & theta_pw(int ig__) const
Return plane-wave coefficient of the step function.
std::unique_ptr< la::Eigensolver > gen_evp_solver_
Generalized eigen-value problem solver.
mpi::Communicator const & comm() const
Total communicator of the simulation.
fft::Grid fft_grid_
Grid descriptor for the fine-grained FFT transform.
auto gvec_sptr() const
Return shared pointer to Gvec object.
int num_loc_op_applied(int n=0) const
Keep track of the total number of wave-functions to which the local operator was applied.
bool initialized_
True if the context is already initialized.
std::unique_ptr< mpi::Grid > mpi_grid_
MPI grid for this simulation.
step_function_t theta_
Step function in real-space and reciprocal domains.
auto const & comm_band_ortho_fft_coarse() const
Communicator, which is orthogonal to comm_fft_coarse within a band communicator.
radial_integrals_t ri_
Stores all radial integrals.
fft::Grid fft_coarse_grid_
Grid descriptor for the coarse-grained FFT transform.
void message(int level__, char const *label__, std::stringstream const &s) const
Print message from the stringstream.
Set of basic parameters of a simulation.
std::vector< int > mpi_grid_dims(std::vector< int > mpi_grid_dims__)
Set dimensions of MPI grid for band diagonalization problem.
Helper class to create FFT grids of given sizes and compute indices in space- and frequency domains.
MPI communicator wrapper.
Simple implementation of 3d vector.
Multidimensional array with the column-major (Fortran) order.
Get the environment variables.
Contains helper functions for the interface with SpFFT library.
memory_t
Memory types where the code can store data.
Contains declaration and implementation of sddk::MPI_grid class.
int num_devices()
Get the number of devices.
Namespace of the SIRIUS library.
auto timestamp(std::string fmt)
Return the timestamp string in a specified format.
int num_blocks(int length__, int block_size__)
Return the maximum number of blocks (with size 'block_size') needed to split the 'length' elements.
auto split(std::string const str__, char delim__)
Split multi-line string into a list of strings.
Representation of various radial integrals.
Generate rotation matrices and related entities.
Contains definition and implementation of sirius::Simulation_parameters class.
Generate unit step function for LAPW method.
Store all callback functions in one place.
std::function< void(int, int, double *, double *)> ps_rho_ri_
Callback function provided by the host code to compute radial integrals of pseudo charge density.
std::function< void(int, int, double *, double *)> rhoc_ri_djl_
std::function< void(int, double, double *, int)> ps_atomic_wf_ri_
Callback function provided by the host code to compute radial integrals of pseudo atomic wave-functio...
std::function< void(int, double, double *, int)> beta_ri_djl_
std::function< void(void)> veff_
Callback function to compute effective potential.
std::function< void(int, double, double *, int)> beta_ri_
Callback function provided by the host code to compute radial integrals of beta projectors.
std::function< void(int, double, double *, int, int)> aug_ri_
Callback function provided by the host code to compute radial integrals of augmentation operator.
std::function< void(void)> band_occ_
Callback function to compute band occupancies.
std::function< void(int, double, double *, int)> ps_atomic_wf_ri_djl_
std::function< void(int, int, double *, double *)> vloc_ri_djl_
Callback function to compute radial integrals of local potential with derivatives of spherical Bessel...
std::function< void(int, int, double *, double *)> rhoc_ri_
Callback function provided by the host code to compute radial integrals of pseudo core charge density...
std::function< void(int, int, double *, double *)> vloc_ri_
Callback function to compute radial integrals of local potential.
std::function< void(int, double, double *, int, int)> aug_ri_djl_
Describe external pointers to periodic function.
Store all radial integrals in one place.
std::unique_ptr< Radial_integrals_beta< true > > beta_djl_
Radial integrals of beta-projectors with derivatives of spherical Bessel functions.
std::unique_ptr< Radial_integrals_rho_pseudo > ps_rho_
Radial integrals of total pseudo-charge density.
std::unique_ptr< Radial_integrals_rho_core_pseudo< true > > ps_core_djl_
Radial integrals of pseudo-core charge density with derivatives of spherical Bessel functions.
std::unique_ptr< Radial_integrals_rho_core_pseudo< false > > ps_core_
Radial integrals of pseudo-core charge density.
std::unique_ptr< Radial_integrals_aug< true > > aug_djl_
Radial integrals of augmentation operator with derivatives of spherical Bessel functions.
std::unique_ptr< Radial_integrals_vloc< true > > vloc_djl_
Radial integrals of the local part of pseudopotential with derivatives of spherical Bessel functions.
std::unique_ptr< Radial_integrals_aug< false > > aug_
Radial integrals of augmentation operator.
std::unique_ptr< Radial_integrals_atomic_wf< true > > ps_atomic_wf_djl_
Radial integrals of atomic wave-functions with derivatives of spherical Bessel functions.
std::unique_ptr< Radial_integrals_atomic_wf< false > > ps_atomic_wf_
Radial integrals of atomic wave-functions.
std::unique_ptr< Radial_integrals_beta< false > > beta_
Radial integrals of beta-projectors.
std::unique_ptr< Radial_integrals_vloc< false > > vloc_
Radial integrals of the local part of pseudopotential.
Representation of the unit step function.
sddk::mdarray< std::complex< double >, 1 > pw
Plane wave expansion coefficients of the step function (global array).
sddk::mdarray< double, 1 > rg
Step function on the real-space grid.