Loading...
Searching...
No Matches
init.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * PlanewaveGPUExecutionPolicy — GPU implementation of PlanewaveExecutionPolicy.
5 *
6 * Extends PlanewaveExecutionPolicy to provide GPU-accelerated band-group
7 * vector operations by delegating BLAS calls to GPUBLAS. Pool-level
8 * operations (reduce_charge_density, reduce_scalar, etc.) remain no-ops
9 * in this single-process GPU policy.
10 *
11 * For GPU+MPI, see PlanewaveGPUMPIExecutionPolicy (future).
12 *
13 * No CUDA headers. No Metal headers. Vendor-neutral.
14 */
15
22
23#include <cstring>
24#include <memory>
25#include <string>
26
27namespace q3 {
28namespace execution {
29
32 public:
33 /**
34 * Construct a planewave GPU execution policy.
35 *
36 * @param device_backend Shared pointer to the GPU device backend.
37 * @param gpu_blas Reference to the GPU BLAS wrapper for vector operations.
38 * Must outlive this policy.
39 */
40 PlanewaveGPUExecutionPolicy(std::shared_ptr<gpu::device::GPUDeviceBackend> device_backend,
42 : GPUExecutionPolicy(std::move(device_backend)), gpu_blas_(gpu_blas) {}
43
44 // ── Band-group vector operations (delegated to GPUBLAS) ──────────────
45
46 /**
47 * Compute the inner product x† · y via GPUBLAS.
48 */
49 ComplexValue dot_product(int number_of_elements,
50 const ComplexValue* vector_x,
51 const ComplexValue* vector_y) const override {
52 return gpu_blas_.dot_product(number_of_elements, vector_x, vector_y);
53 }
54
55 /**
56 * Scaled vector addition via GPUBLAS: vector_y += scaling_factor * vector_x.
57 */
58 void add_scaled_vector(int number_of_elements,
59 ComplexValue scaling_factor,
60 const ComplexValue* vector_x,
61 ComplexValue* vector_y) const override {
62 gpu_blas_.axpy(number_of_elements, scaling_factor, vector_x, vector_y);
63 }
64
65 /**
66 * Euclidean norm via GPUBLAS.
67 */
68 Real norm(int number_of_elements, const ComplexValue* vector_x) const override {
69 return gpu_blas_.norm(number_of_elements, vector_x);
70 }
71
72 // ── Planewave pool-level operations (no-ops in single-process GPU) ───
73
74 int number_of_pools() const override {
75 return 1;
76 }
77 bool one_process_owns_complete_state() const override {
78 return true;
79 }
80 /// The single GPU process holds every k-point whole.
81 bool each_process_owns_whole_kpoints() const override {
82 return true;
83 }
84 /// Identity: the single GPU process holds the global k-point grid in order.
85 int global_kpoint_index(int local_kpoint_index) const override {
86 return local_kpoint_index;
87 }
88
89 void reduce_charge_density(std::vector<std::vector<Real>>& /*charge_density*/) const override {}
90
91 void reduce_scalar(Real& /*value*/) const override {}
92 void reduce_vector(std::vector<Real>& /*values*/) const override {}
93 void reduce_scalar_max(int& /*value*/) const override {}
94 void reduce_scalar_max(Real& /*value*/) const override {}
95 void reduce_scalar_min(Real& /*value*/) const override {}
96 void reduce_band_group_scalar_min(int& /*value*/) const override {}
97
98 void reduce_within_pool_scalar(Real& /*value*/) const override {}
99 void reduce_within_pool_vector(std::vector<Real>& /*values*/) const override {}
100
101 /**
102 * Identity: return the global grid unchanged.
103 */
104 KPointsGrid local_kpoint_grid(const KPointsGrid& global_kpoint_grid) const override {
105 return global_kpoint_grid;
106 }
107
108 std::vector<Real>
109 assemble_global_kpoint_state_data(const std::vector<Real>& local_data,
110 int /*local_number_of_physical_kpoints*/,
111 int /*number_of_spin_components*/,
112 int /*number_of_values_per_state*/) const override {
113 return local_data;
114 }
115
116 /**
117 * Create a GPU FFT grid instance.
118 *
119 * Intentionally returns nullptr: vendor GPU FFT grids (`GPUFFTGrid`,
120 * cuFFT/hipFFT/SYCL-backed) exist but are constructed by the GPU entry
121 * points (main_gpu*.cpp) and handed to the basis directly, not created
122 * through this policy hook. The caller must check for null and fall
123 * back to a serial FFT grid.
124 */
125 std::unique_ptr<FFTGrid> create_fft_grid() const override {
126 return nullptr;
127 }
128
129 /**
130 * Access the GPU BLAS wrapper.
131 */
133 return gpu_blas_;
134 }
135
136 /**
137 * Access the GPU BLAS wrapper (const).
138 */
140 return gpu_blas_;
141 }
142
143 private:
144 gpu::blas::GPUBLAS& gpu_blas_;
145};
146
147} // namespace execution
148} // namespace q3
void reduce_within_pool_scalar(Real &) const override
Definition init.hpp:98
PlanewaveGPUExecutionPolicy(std::shared_ptr< gpu::device::GPUDeviceBackend > device_backend, gpu::blas::GPUBLAS &gpu_blas)
Definition init.hpp:40
bool one_process_owns_complete_state() const override
Definition init.hpp:77
Real norm(int number_of_elements, const ComplexValue *vector_x) const override
Definition init.hpp:68
void reduce_vector(std::vector< Real > &) const override
Definition init.hpp:92
int global_kpoint_index(int local_kpoint_index) const override
Identity: the single GPU process holds the global k-point grid in order.
Definition init.hpp:85
void reduce_within_pool_vector(std::vector< Real > &) const override
Definition init.hpp:99
void reduce_scalar_max(Real &) const override
Definition init.hpp:94
std::vector< Real > assemble_global_kpoint_state_data(const std::vector< Real > &local_data, int, int, int) const override
Definition init.hpp:109
std::unique_ptr< FFTGrid > create_fft_grid() const override
Definition init.hpp:125
void reduce_scalar(Real &) const override
Definition init.hpp:91
const gpu::blas::GPUBLAS & gpu_blas() const
Definition init.hpp:139
void reduce_charge_density(std::vector< std::vector< Real > > &) const override
Definition init.hpp:89
void reduce_band_group_scalar_min(int &) const override
Definition init.hpp:96
bool each_process_owns_whole_kpoints() const override
The single GPU process holds every k-point whole.
Definition init.hpp:81
void add_scaled_vector(int number_of_elements, ComplexValue scaling_factor, const ComplexValue *vector_x, ComplexValue *vector_y) const override
Definition init.hpp:58
ComplexValue dot_product(int number_of_elements, const ComplexValue *vector_x, const ComplexValue *vector_y) const override
Definition init.hpp:49
void reduce_scalar_min(Real &) const override
Definition init.hpp:95
KPointsGrid local_kpoint_grid(const KPointsGrid &global_kpoint_grid) const override
Definition init.hpp:104
void reduce_scalar_max(int &) const override
Definition init.hpp:93
int number_of_pools() const override
Definition init.hpp:74
const device::GPUDeviceBackend & device_backend() const
Definition init.hpp:137
GPUExecutionPolicy(std::shared_ptr< device::GPUDeviceBackend > device_backend)
Definition init.hpp:43
virtual void axpy(int number_of_elements, Complex scaling_factor, const Complex *vector_x, Complex *vector_y) const =0
virtual Complex dot_product(int number_of_elements, const Complex *vector_x, const Complex *vector_y) const =0
virtual double norm(int number_of_elements, const Complex *vector_x) const =0
double Real