1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::common_simd::assets::validate_inputs;
use crate::indicators::mama::{
min_data, output_length, validate_options, IndicatorState, State, INPUTS_WIDTH, OPTIONS_WIDTH,
};
use crate::indicators::simd_indicators::mama_simd::SimdState;
use crate::indicators::simd_indicators::road_train::{Asset, Driver, PrimeMover};
use crate::types::IndicatorError;
use std::simd::Simd;
/// SIMD driver that advances MAMA / FAMA across `N` asset lanes per scheduling epoch.
struct MamaDriver {
fast_limit: f64,
slow_limit: f64,
want_optional_outputs: (bool, bool, bool),
}
impl Driver<State> for MamaDriver {
/// Processes one epoch of bars for `N` assets simultaneously using SIMD.
///
/// Gathers per-asset states into a [`SimdState`], runs the full HD + MAMA pipeline
/// for every bar in the epoch, writes `mama` and `fama` (and optionally
/// `dc_period` / `alpha`) for each asset, then scatters the updated state back.
fn next_run<const N: usize>(
&mut self,
inputs: Vec<Vec<&[f64]>>,
mut outputs: Vec<Vec<&mut [f64]>>,
mut states: Vec<&mut State>,
_options: Vec<Option<&()>>,
) {
let len = inputs[0][0].len();
let mut simd_state = SimdState::new(&mut states);
let fast_limits = Simd::splat(self.fast_limit);
let slow_limits = Simd::splat(self.slow_limit);
let (has_optional, want_dc, want_alpha) = self.want_optional_outputs;
let real_ptrs = crate::extract_input_ptrs!(inputs, N, real_ptrs);
let (mama_line_ptr, fama_line_ptr, dc_period_line_ptr, alpha_line_ptr) = crate::extract_output_ptrs!(
outputs,
N,
mama_line_ptr,
fama_line_ptr,
dc_period_line_ptr,
alpha_line_ptr
);
for i in 0..len {
let real = crate::extract_simd_inputs_at_index!(i, N, real @ real_ptrs);
// Safety: all ring buffers are full — guaranteed by State::init_state
// called during indicator_by_assets setup, before PrimeMover dispatches
// to this driver for the first time.
let (mama, fama) =
unsafe { simd_state.calc_simd_unchecked(real, fast_limits, slow_limits) };
crate::write_simd_at_indices!(N, i,
mama_line_ptr => mama,
fama_line_ptr => fama
);
if has_optional {
crate::store_simd_optional_outputs!(i, N,
want_dc, dc_period_line_ptr => simd_state.hd.smooth_period,
want_alpha, alpha_line_ptr => simd_state.alpha
);
}
}
simd_state.write_states(&mut states);
}
}
/// Calculates MAMA and FAMA for `N` assets simultaneously using SIMD parallelism.
///
/// Each asset's state is independently warmed up via [`State::init_state`] (consuming
/// the first 23 bars, writing bar 22's output to index 0), then all assets are batched
/// by the [`PrimeMover`] scheduler and advanced together through the SIMD pipeline
/// starting at bar 23.
///
/// # Arguments
/// * `inputs` — `N` asset input sets; `inputs[i]` is `[&[f64]; 1]` containing `[real]`
/// for asset `i`.
/// * `options` — Shared options: `[fast_limit, slow_limit]`.
/// * `optional_outputs` — Optional flags: index `0` = `dc_period`, index `1` = `alpha`.
///
/// # Returns
/// `Ok((outputs, states))` where `outputs[i][0]` is `mama`, `outputs[i][1]` is `fama`,
/// `outputs[i][2]` is `dc_period` (empty unless requested), `outputs[i][3]` is `alpha`
/// (empty unless requested), and `states[i]` is the final [`IndicatorState`] for asset `i`.
/// Returns `Err(IndicatorError::NotEnoughData)` if any input is shorter than
/// [`min_data`] (23 bars), or `Err(IndicatorError::InvalidOptions)` if options are invalid.
pub fn indicator_by_assets<const N: usize>(
inputs: &[&[&[f64]; INPUTS_WIDTH]; N],
options: &[f64; OPTIONS_WIDTH],
optional_outputs: Option<&[bool]>,
) -> Result<(Vec<Vec<Vec<f64>>>, Vec<IndicatorState>), IndicatorError> {
validate_inputs::<INPUTS_WIDTH>(inputs, min_data(options))?;
validate_options(options)?;
let fast_limit = options[0];
let slow_limit = options[1];
let mut output_buffers = Vec::with_capacity(N);
let mut road_train = PrimeMover::<N, State>::new();
let mut want_optional_outputs = (false, false, false);
for i in 0..N {
let len = inputs[i][0].len();
let capacity = output_length(len, options);
let mut mama_line = crate::uninit_vec!(f64, capacity);
let mut fama_line = crate::uninit_vec!(f64, capacity);
let (mut dc_period_line, mut alpha_line) = crate::init_optional_outputs!(
optional_outputs, &[false, false],
dc_period_line: capacity,
alpha_line: capacity
);
// init_state warms up the HD pipeline, seeds MAMA/FAMA from the first valid bar's price,
// processes bar min_data−1 = 22 (0-indexed), and writes outputs[0] for all series.
let state = State::init_state(
inputs[i][0],
fast_limit,
slow_limit,
&mut mama_line,
&mut fama_line,
&mut dc_period_line,
&mut alpha_line,
);
if i == 0 {
want_optional_outputs = crate::calc_want_flags!(dc_period_line, alpha_line);
}
// init_state wrote index 0; the driver writes indices 1..capacity.
// Pass slices starting at index 1 to the road_train.
let mut output_buffer = vec![mama_line, fama_line, dc_period_line, alpha_line];
let mut asset_outputs = Vec::with_capacity(output_buffer.len());
for j in 0..output_buffer.len() {
unsafe {
let buf = &mut output_buffer[j];
let len = buf.len();
// Start from index 1: init_state already wrote index 0.
let start = if len > 0 { 1 } else { 0 };
asset_outputs.push(std::slice::from_raw_parts_mut(
buf.as_mut_ptr().add(start),
len.saturating_sub(start),
));
}
}
road_train.add_asset(Asset::new(
vec![inputs[i][0]],
asset_outputs,
i,
// init_state consumed bars 0..22 (inclusive), so the driver starts at bar 23 = min_data.
min_data(options),
0,
state,
None,
));
output_buffers.push(output_buffer);
}
let mut driver = MamaDriver {
fast_limit,
slow_limit,
want_optional_outputs,
};
let final_states = road_train.drive(&mut driver);
let states = final_states
.into_iter()
.map(|s| IndicatorState::new(s, fast_limit, slow_limit))
.collect();
Ok((output_buffers, states))
}