Loading...
Searching...
No Matches
factory.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * SubspaceDiagonalizerFactory — chooses the best subspace solver.
5 *
6 * Automatically selects SerialSubspaceDiagonalizer for serial execution
7 * and MPISubspaceDiagonalizer (ScaLAPACK) for parallel execution.
8 *
9 * Dispatch is based on the runtime group_size() of the execution policy.
10 * The MPI subspace header self-guards with Q3_WITH_MPI, so this factory
11 * includes it unconditionally. In non-MPI builds, MPISubspaceDiagonalizer
12 * is not defined and the MPI branch is unreachable (group_size() is always 1).
13 */
14
19
20#include <memory>
21#include <stdexcept>
22
23namespace q3 {
24
26 public:
27 static std::unique_ptr<SubspaceDiagonalizer>
28 create(const execution::ExecutionPolicy& execution_policy) {
29 if (execution_policy.group_size() > 1) {
30 return create_mpi_subspace_diagonalizer(execution_policy);
31 }
32 return std::make_unique<SerialSubspaceDiagonalizer>();
33 }
34
35 private:
36 /**
37 * Create an MPI subspace diagonalizer from the execution policy.
38 *
39 * This is a separate function so that the MPISubspaceDiagonalizer type
40 * reference is isolated behind the Q3_WITH_MPI self-guard in mpi.hpp.
41 * In non-MPI builds, group_size() is always 1 so this is never called,
42 * and the linker never needs to resolve the MPI symbols.
43 */
44 static std::unique_ptr<SubspaceDiagonalizer> create_mpi_subspace_diagonalizer(
45 [[maybe_unused]] const execution::ExecutionPolicy& execution_policy) {
46#ifdef Q3_WITH_MPI
47 const auto& mpi_policy =
48 dynamic_cast<const execution::MPIExecutionPolicy&>(execution_policy);
49 return std::make_unique<MPISubspaceDiagonalizer>(mpi_policy);
50#else
51 throw std::runtime_error(
52 "group_size() > 1 requires MPI support, but Q3 was built without Q3_WITH_MPI.");
53#endif
54 }
55};
56
57} // namespace q3
static std::unique_ptr< SubspaceDiagonalizer > create(const execution::ExecutionPolicy &execution_policy)
Definition factory.hpp:28
virtual int group_size() const =0