Repository navigation
Add reusable band-power contexts with bounded native workers - #873
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.