-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfftw3l-benchmark.cc
65 lines (49 loc) · 1.88 KB
/
fftw3l-benchmark.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <fftw3.h>
#include <benchmark/benchmark.h>
#include <math.h>
#include <chrono>
#include <string.h>
#define REAL 0
#define IMAG 1
void generate_signal(fftwl_complex* signal, const int N) {
int i;
for (i = 0; i < N; ++i) {
double theta = (double) i / (double) N * M_PI;
signal[i][REAL] = 1.0 * cos(10.0 * theta) + 0.5 * cos(25.0 * theta);
signal[i][IMAG] = 1.0 * sin(10.0 * theta) + 0.5 * sin(25.0 * theta);
}
}
static void fftwl(benchmark::State& state) {
int N = state.range(0);
// Allocate memory for the signal and result
fftwl_complex* signal = (fftwl_complex*) fftwl_malloc(sizeof(fftwl_complex) * N);
fftwl_complex * in = (fftwl_complex*) fftwl_malloc(sizeof(fftwl_complex) * N);
fftwl_complex * out = (fftwl_complex*) fftwl_malloc(sizeof(fftwl_complex) * N);
// Init fftw plan
fftwl_plan plan = fftwl_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
// Generate signal
generate_signal(signal, N);
for (auto _ : state) {
// Start iteration timer
auto start = std::chrono::high_resolution_clock::now();
// Copy signal into input of fft
memcpy(in, signal, sizeof(fftwl_complex) * N);
// Transform signal to fft (in => out)
fftwl_execute(plan);
// Calculate elapsed time
auto elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start);
// Set Iteration Time
state.SetIterationTime(elapsed_seconds.count());
}
// Destroy fft plan
fftwl_destroy_plan(plan);
// Cleanup memory
fftwl_free(in);
fftwl_free(out);
// Save statistics
state.SetItemsProcessed(static_cast<int64_t>(state.iterations()) * N);
state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * N * sizeof(fftwl_complex));
state.SetComplexityN(N);
}
BENCHMARK(fftwl)->RangeMultiplier(2)->Range(1<<10, 1<<20)->Complexity()->UseManualTime();
BENCHMARK_MAIN();