-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a user level locking mechanism for FFTW (#548)
* Provide a user level locking mechanism for FFTW This commit provides a basic interface for the user to provide a lock around `fftw` calls. This is because any plan manipulation functions in `fftw` (make, destroy) are not thread-safe. See example code here: ```c++ #include <vector> #include <mutex> #include <fftw3.h> #include <finufft.h> #include <omp.h> using namespace std; #define N 65384 void locker(void *lck) { reinterpret_cast<recursive_mutex *>(lck)->lock(); } void unlocker(void *lck) { reinterpret_cast<recursive_mutex *>(lck)->unlock(); } int main() { int64_t Ns[3]; // guru describes mode array by vector [N1,N2..] Ns[0] = N; recursive_mutex lck; finufft_opts opts; finufft_default_opts(&opts); opts.nthreads = 1; opts.debug = 0; opts.fftw_lock_fun = locker; opts.fftw_unlock_fun = unlocker; opts.fftw_lock_data = reinterpret_cast<void *>(&lck); // random nonuniform points (x) and complex strengths (c)... vector<complex<double>> c(N); // init FFTW threads fftw_init_threads(); // FFTW and FINUFFT execution using OpenMP parallelization #pragma omp parallel for for (int j = 0; j < 100; ++j) { // allocate output array for FFTW... vector<complex<double>> F1(N); // FFTW plan lck.lock(); fftw_plan_with_nthreads(1); fftw_plan plan = fftw_plan_dft_1d(N, reinterpret_cast<fftw_complex*>(c.data()), reinterpret_cast<fftw_complex*>(F1.data()), FFTW_FORWARD, FFTW_ESTIMATE); fftw_destroy_plan(plan); lck.unlock(); // FINUFFT plan finufft_plan nufftplan; finufft_makeplan(1, 1, Ns, 1, 1, 1e-6, &nufftplan, &opts); finufft_destroy(nufftplan); } return 0; } ``` * fftw_lock: fix issue when null opts passed * fftw_lock: add new opts to python bindings * threads: fix lock guard issue in osx arm64 clang * fftw_lock: add info to changelog and user documentation * fftw_lock: add test for new fftw_lock * fftw_lock: share lock and init between float/double * Revert "fftw_lock: share lock and init between float/double" This reverts commit 8594b65. * fftw_lock_test: add comments to source describing why --------- Co-authored-by: Robert Blackwell <[email protected]>
- Loading branch information
Showing
8 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,63 @@ | ||
#include <complex> | ||
#include <mutex> | ||
#include <vector> | ||
|
||
#include <fftw3.h> | ||
#include <finufft.h> | ||
#include <omp.h> | ||
|
||
// This file tests the user locking mechanism for multi-threaded FFTW. This | ||
// demonstrates a user lock to prevent FFTW plan calls from interfering with | ||
// finufft plan calls (make/destroy). | ||
// Robert Blackwell. Based on bug identified by Jonas Krimmer (9/17/24) | ||
// See discussion at https://github.com/ludvigak/FINUFFT.jl/issues/62 | ||
|
||
constexpr int N = 65384; | ||
|
||
// Example user lock functions | ||
void locker(void *lck) { reinterpret_cast<std::mutex *>(lck)->lock(); } | ||
void unlocker(void *lck) { reinterpret_cast<std::mutex *>(lck)->unlock(); } | ||
|
||
int main() { | ||
int64_t Ns[3]; // guru describes mode array by vector [N1,N2..] | ||
Ns[0] = N; | ||
std::mutex lck; | ||
|
||
finufft_opts opts; | ||
finufft_default_opts(&opts); | ||
opts.nthreads = 1; | ||
opts.debug = 0; | ||
opts.fftw_lock_fun = locker; | ||
opts.fftw_unlock_fun = unlocker; | ||
opts.fftw_lock_data = reinterpret_cast<void *>(&lck); | ||
|
||
// random nonuniform points (x) and complex strengths (c)... | ||
std::vector<std::complex<double>> c(N); | ||
|
||
omp_set_num_threads(8); | ||
|
||
// init FFTW threads | ||
fftw_init_threads(); | ||
|
||
// FFTW and FINUFFT execution using OpenMP parallelization | ||
#pragma omp parallel for | ||
for (int j = 0; j < 100; ++j) { | ||
// allocate output array for FFTW... | ||
std::vector<std::complex<double>> F1(N); | ||
|
||
// FFTW plan | ||
lck.lock(); | ||
fftw_plan_with_nthreads(1); | ||
fftw_plan plan = fftw_plan_dft_1d(N, reinterpret_cast<fftw_complex *>(c.data()), | ||
reinterpret_cast<fftw_complex *>(F1.data()), | ||
FFTW_FORWARD, FFTW_ESTIMATE); | ||
fftw_destroy_plan(plan); | ||
lck.unlock(); | ||
|
||
// FINUFFT plan | ||
finufft_plan nufftplan; | ||
finufft_makeplan(1, 1, Ns, 1, 1, 1e-6, &nufftplan, &opts); | ||
finufft_destroy(nufftplan); | ||
} | ||
return 0; | ||
} |