Quantum 3xplorer (Q3)
An AI-native quantum simulation engine
Loading...
Searching...
No Matches
factory_mpi.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * create_scf_iterator_mpi — MPI SCF iterator factory.
5 *
6 * Two overloads:
7 *
8 * 1. create_scf_iterator_mpi(input)
9 * Constructs a PlanewaveMPIExecutionPolicy internally (convenience form).
10 * MPI is initialized inside the factory, AFTER parse_input in main().
11 * Preserved for backward compatibility.
12 *
13 * 2. create_scf_iterator_mpi(input, policy)
14 * Accepts an already-constructed, already-configured policy.
15 * Use this overload from main() to ensure MPI_Init fires before any
16 * other code (including parse_input), so that:
17 * - log::init_mpi() runs immediately and silences non-root ranks
18 * - MPI_Finalize is guaranteed by policy's destructor even if
19 * parse_input throws
20 *
21 * This is the entry point for multi-process MPI runs. The only io/ files
22 * that touch <mpi.h> are this one (transitively via PlanewaveMPIExecutionPolicy)
23 * and io/logger_mpi.hpp (directly, for the rank query).
24 */
25
26#include <memory>
27
28#include "io/input/data.hpp"
29#include "io/logger_mpi.hpp"
33
34namespace q3 {
35
36/**
37 * Create an MPI-capable Kohn-Sham SCF iterator from an already-initialized policy.
38 *
39 * This overload is preferred from main(): construct PlanewaveMPIExecutionPolicy
40 * before parse_input so that MPI_Init fires first and log::init_mpi() silences
41 * non-root ranks before any output is produced.
42 *
43 * @param input Parsed input parameters.
44 * @param policy Fully constructed, topology-configured execution policy.
45 * @return A fully initialized, execution-policy-owning SCF iterator.
46 */
47inline std::unique_ptr<PlanewaveKohnShamSCFIterator>
49 std::unique_ptr<execution::PlanewaveMPIExecutionPolicy> policy) {
50 const execution::PlanewaveMPIExecutionPolicy& policy_ref = *policy;
51 std::unique_ptr<PlanewaveKohnShamSCFIterator> iterator =
52 create_kohn_sham_iterator(input, policy_ref);
53 iterator->set_owned_execution_policy(std::move(policy));
54 return iterator;
55}
56
57/**
58 * Create an MPI-capable Kohn-Sham SCF iterator from parsed InputData.
59 *
60 * Convenience overload: constructs PlanewaveMPIExecutionPolicy internally,
61 * calls MPI_Init and log::init_mpi(), then delegates to the shared factory.
62 *
63 * @param input Parsed input parameters.
64 * @return A fully initialized, execution-policy-owning SCF iterator.
65 */
66inline std::unique_ptr<PlanewaveKohnShamSCFIterator>
68 auto policy = std::make_unique<execution::PlanewaveMPIExecutionPolicy>();
69 policy->configure_topology(input.number_of_kpoint_pools);
71 return create_scf_iterator_mpi(input, std::move(policy));
72}
73
74} // namespace q3
void init_mpi(const std::string &log_file="")
std::unique_ptr< PlanewaveKohnShamSCFIterator > create_scf_iterator_mpi(const InputData &input, std::unique_ptr< execution::PlanewaveMPIExecutionPolicy > policy)
std::unique_ptr< PlanewaveKohnShamSCFIterator > create_kohn_sham_iterator(const InputData &input, const execution::PlanewaveExecutionPolicy &execution_policy)
Definition factory.hpp:64
Parsed input data — holds raw values read from input files.
Definition data.hpp:27
int number_of_kpoint_pools
Definition data.hpp:280