Skip to content

Add reusable band-power contexts with bounded native workers - #873

Open
Andrey1994 wants to merge 1 commit into
masterfrom
perf/dsp-context-601
Open

Andrey1994 wants to merge 1 commit into
masterfrom
perf/dsp-context-601

Conversation

@Andrey1994

Copy link
Copy Markdown
Member

Repeated windowed band-power calls rebuild filter/FFT setup and allocate channel workspaces. Add an optional bounded context API that retains coefficients, normalized windows, FFT plans, per-worker scratch buffers, and native worker threads. Python and C# can select source rows directly and reuse caller-owned output arrays. Existing APIs remain available; the shared spectral kernel preserves their numerical definition.

The C API validates input/output capacities and row strides. Context calls serialize per handle; independent contexts run concurrently. Release is safe with in-flight operations through shared ownership. Batches below eight channels run serially; workers=0 chooses up to four workers, and workers=1 is serial. This uses std::thread and adds no OpenMP runtime dependency. It does not change windowed zero-phase processing into causal streaming.

Validation: MSVC Release /MT build; 18 signal-processing and 22 band-power Python regressions passed, including legacy equivalence, custom settings, repeated/reordered channels, failure recovery, capacities, and concurrent calls; C# context/regression executable passed against the new DLL. Tests extend the existing Windows/Unix CI suites. A local 64-channel x 2,000-sample filtered band-power probe improved from 28.7 ms to 9.25 ms with four workers (~3.1x); performance depends on workload and hardware.

Part of #601. Based directly on master, independent of #871 and #872.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T01:27:22.894675Z 7f80004 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

var batch = new double[8, 2048];
for (int row = 0; row < 8; row++)
for (int col = 0; col < 2048; col++)
batch[row, col] = Math.Sin((row + 1) * col * 0.1);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int read_file (double[] data, int[] num_rows, int[] num_cols, string file_name, int max_elements);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int create_band_power_context (int max_channels, int max_samples, double[] starts, double[] stops, int bands, int fs, int apply, int nfft, double low, double high, int mains, int detrend, int workers, int[] handle);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int create_band_power_context (int max_channels, int max_samples, double[] starts, double[] stops, int bands, int fs, int apply, int nfft, double low, double high, int mains, int detrend, int workers, int[] handle);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int process_band_power_context (int handle, double[,] data, int capacity, int rows, int samples, int stride, int[] channels, int count, double[] averages, double[] deviations, int output_capacity);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int process_band_power_context (int handle, double[,] data, int capacity, int rows, int samples, int stride, int[] channels, int count, double[] averages, double[] deviations, int output_capacity);
[DllImport ("DataHandler", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int release_band_power_context (int handle);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int read_file (double[] data, int[] num_rows, int[] num_cols, string file_name, int max_elements);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int create_band_power_context (int max_channels, int max_samples, double[] starts, double[] stops, int bands, int fs, int apply, int nfft, double low, double high, int mains, int detrend, int workers, int[] handle);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int create_band_power_context (int max_channels, int max_samples, double[] starts, double[] stops, int bands, int fs, int apply, int nfft, double low, double high, int mains, int detrend, int workers, int[] handle);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int process_band_power_context (int handle, double[,] data, int capacity, int rows, int samples, int stride, int[] channels, int count, double[] averages, double[] deviations, int output_capacity);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int process_band_power_context (int handle, double[,] data, int capacity, int rows, int samples, int stride, int[] channels, int count, double[] averages, double[] deviations, int output_capacity);
[DllImport ("DataHandler32", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int release_band_power_context (int handle);
switch (PlatformHelper.get_library_environment ())
{
case LibraryEnvironment.x64:
return DataHandlerLibrary64.create_band_power_context (max_channels, max_samples, starts, stops, bands, fs, apply, nfft, low, high, mains, detrend, workers, handle);
case LibraryEnvironment.x64:
return DataHandlerLibrary64.create_band_power_context (max_channels, max_samples, starts, stops, bands, fs, apply, nfft, low, high, mains, detrend, workers, handle);
case LibraryEnvironment.x86:
return DataHandlerLibrary32.create_band_power_context (max_channels, max_samples, starts, stops, bands, fs, apply, nfft, low, high, mains, detrend, workers, handle);
switch (PlatformHelper.get_library_environment ())
{
case LibraryEnvironment.x64:
return DataHandlerLibrary64.process_band_power_context (handle, data, capacity, rows, samples, stride, channels, count, averages, deviations, output_capacity);
def __del__(self):
try:
self.close()
except Exception:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant