Skip to content

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

Merged
Andrey1994 merged 4 commits into
masterfrom
perf/dsp-context-601
Nov 7, 2026
Merged

Andrey1994 merged 4 commits into
masterfrom
perf/dsp-context-601

Conversation

@Andrey1994

@Andrey1994 Andrey1994 commented Nov 3, 2026 •

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 and per-worker scratch buffers. Python and C# select source rows directly and can reuse caller-owned output arrays. Detrending uses the retained scratch buffer while sharing the existing numerical implementation.

The C API validates capacities and row strides. Calls serialize per handle; independent contexts run concurrently. Shared ownership lets native operations that have acquired a context finish after release; calls racing before acquisition may receive an invalid-handle error. Python close atomically detaches the handle so concurrent callers release it exactly once.

Batches below eight channels run serially. Contexts with max_channels below eight allocate one workspace and no background threads; larger contexts use up to four workers by default, with workers=1 selecting serial processing. Existing APIs and windowed zero-phase processing semantics remain available. No OpenMP runtime dependency is added.

Validation: fresh MSVC Release /MT native build; 25 band-power, 18 signal-processing, 11 signal-metrics and 11 decomposition Python regressions passed, plus the C# context/regression executable. Coverage includes legacy equivalence, changing window lengths, all detrending modes, capacities, failure recovery, concurrent calls, concurrent close and close during argument validation. The concurrent-close regression reproduces the original implementation's double-release failure. A four-channel context created zero background threads after the change, versus four before it.

Also repair the shared CI failures: stage native dylibs beside the macOS C# regression executable, report C++ test exceptions through a nonzero exit status, and guard allocation failure/overflow in the macOS Bluetooth pipe resize.

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);
@Andrey1994
Andrey1994 merged commit 695122e into master Nov 7, 2026
40 checks passed
@Andrey1994
Andrey1994 deleted the perf/dsp-context-601 branch November 7, 2026 18:54
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