-
Notifications
You must be signed in to change notification settings - Fork 103
test: add threadsafe execute test and sanitizer CI modes #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DiamonDinoia
merged 1 commit into
flatironinstitute:master
from
DiamonDinoia:feat/threadsafe-execute-tests
Mar 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #include <finufft.h> | ||
| #include <finufft_common/constants.h> | ||
| #include <finufft_opts.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <complex> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include "utils/dirft1d.hpp" | ||
| #include "utils/norms.hpp" | ||
|
|
||
| int main() { | ||
| constexpr int nthreads = 4; | ||
| constexpr int nreps = 16; | ||
| constexpr int M = 400; | ||
| constexpr int64_t N1 = 2048; | ||
| constexpr double tol = 1e-12; | ||
|
|
||
| finufft_opts opts; | ||
| finufft_default_opts(&opts); | ||
| opts.nthreads = 1; // crucial: parallelism is across concurrent plan executes | ||
| opts.debug = 0; | ||
|
|
||
| std::vector<double> x(M); | ||
| std::vector<std::complex<double>> c(M), ref(N1); | ||
| for (int j = 0; j < M; ++j) { | ||
| double t = static_cast<double>(j) / M; | ||
| x[j] = -finufft::common::PI + 2.0 * finufft::common::PI * t; | ||
| c[j] = std::complex<double>(0.5 * std::cos(13.0 * t) + 0.25 * std::sin(7.0 * t), | ||
| 0.75 * std::sin(11.0 * t) - 0.2 * std::cos(5.0 * t)); | ||
| } | ||
|
|
||
| int64_t Ns[3] = {N1, 1, 1}; | ||
| finufft_plan plan; | ||
| int ier = finufft_makeplan(1, 1, Ns, +1, 1, tol, &plan, &opts); | ||
| if (ier != 0) { | ||
| std::fprintf(stderr, "finufft_makeplan failed: ier=%d\n", ier); | ||
| return ier; | ||
| } | ||
| ier = finufft_setpts(plan, M, x.data(), nullptr, nullptr, 0, nullptr, nullptr, nullptr); | ||
| if (ier != 0) { | ||
| std::fprintf(stderr, "finufft_setpts failed: ier=%d\n", ier); | ||
| finufft_destroy(plan); | ||
| return ier; | ||
| } | ||
|
|
||
| dirft1d1<int64_t>(M, x, c, +1, N1, ref); | ||
|
|
||
| std::vector<int> failures(nthreads, 0); | ||
|
|
||
| std::vector<std::thread> workers; | ||
| workers.reserve(nthreads); | ||
| for (int tid = 0; tid < nthreads; ++tid) { | ||
| workers.emplace_back([&, tid]() { | ||
| std::vector<std::complex<double>> out(N1); | ||
| for (int rep = 0; rep < nreps; ++rep) { | ||
| int local_ier = finufft_execute(plan, c.data(), out.data()); | ||
| double relerr = relerrtwonorm(N1, ref.data(), out.data()); | ||
| if (local_ier != 0 || relerr > 10.0 * tol) { | ||
| failures[tid] = 1; | ||
| std::fprintf(stderr, "thread %d rep %d failed: ier=%d relerr=%.3g\n", tid, rep, | ||
| local_ier, relerr); | ||
| return; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| for (auto &worker : workers) worker.join(); | ||
|
|
||
| finufft_destroy(plan); | ||
| return *std::max_element(failures.begin(), failures.end()); | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never tried this. Is this emplace_back non-block, so workers.reserve(nthreads) for nthreads==4 will execute 4 finufft_execute simultaneously, and that's how you test the thread safety of executing parallel execution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A brief explanation here may be useful. workers.emplace_back(...) constructs each std::thread directly in the vector. The new thread begins running the lambda immediately, and the thread constructor returns without waiting for completion, so the loop launches all threads without blocking.
After that, I call join() on each thread to wait for them all to finish. So yes, for nthreads == 4, this is intended to execute 4 finufft_execute calls concurrently and test thread safety.