Skip to content

Commit

Permalink
fftw_lock: add test for new fftw_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwer committed Sep 10, 2024
1 parent 024bfa8 commit 1e2c047
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ add_test(
COMMAND testutils
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

if(NOT FINUFFT_USE_DUCC0)
add_executable(fftw_lock_test fftw_lock_test.cpp)
target_compile_features(fftw_lock_test PRIVATE cxx_std_17)
finufft_link_test(fftw_lock_test)

add_test(
NAME run_fftw_lock_test
COMMAND fftw_lock_test
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# Add ctest definitions that run at both precisions...
function(add_tests_with_prec PREC REQ_TOL CHECK_TOL SUFFIX)
# All of the following should be run at OMP_NUM_THREADS=4 or something small,
Expand Down
56 changes: 56 additions & 0 deletions test/fftw_lock_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <complex>
#include <mutex>
#include <vector>

#include <fftw3.h>
#include <finufft.h>
#include <omp.h>

constexpr int N = 65384;

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;
}

0 comments on commit 1e2c047

Please sign in to comment.