blob: 3c33e77ae6d00ac97c0d1e76c0d0572009a84c8b [file] [log] [blame]
Mark Borgerding9c0fcd02009-05-23 10:09:48 -04001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacobb596f6c2012-07-15 10:33:40 -04002// for linear algebra.
Mark Borgerding9c0fcd02009-05-23 10:09:48 -04003//
4// Copyright (C) 2009 Mark Borgerding mark a borgerding net
5//
Benoit Jacob69124cf2012-07-13 14:42:47 -04006// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Mark Borgerding9c0fcd02009-05-23 10:09:48 -04009
Thomas Capricelli551cb9b2010-07-09 03:59:36 +020010#include <iostream>
11
Mark Borgerding0fa68b92009-10-30 19:46:45 -040012#include <bench/BenchUtil.h>
Mark Borgerding9c0fcd02009-05-23 10:09:48 -040013#include <complex>
14#include <vector>
15#include <Eigen/Core>
Mark Borgerding1c543402009-05-31 15:44:57 -040016
Mark Borgerding210092d2009-05-25 20:35:24 -040017#include <unsupported/Eigen/FFT>
Mark Borgerding9c0fcd02009-05-23 10:09:48 -040018
19using namespace Eigen;
20using namespace std;
21
Mark Borgerding326ea772009-05-23 22:50:07 -040022template <typename T>
23string nameof();
24
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000025template <>
26string nameof<float>() {
27 return "float";
28}
29template <>
30string nameof<double>() {
31 return "double";
32}
33template <>
34string nameof<long double>() {
35 return "long double";
36}
Mark Borgerding9c0fcd02009-05-23 10:09:48 -040037
38#ifndef TYPE
39#define TYPE float
40#endif
41
Mark Borgerding326ea772009-05-23 22:50:07 -040042#ifndef NFFT
43#define NFFT 1024
44#endif
45#ifndef NDATA
46#define NDATA 1000000
Mark Borgerding9c0fcd02009-05-23 10:09:48 -040047#endif
48
Mark Borgerding326ea772009-05-23 22:50:07 -040049using namespace Eigen;
50
51template <typename T>
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000052void bench(int nfft, bool fwd, bool unscaled = false, bool halfspec = false) {
53 typedef typename NumTraits<T>::Real Scalar;
54 typedef typename std::complex<Scalar> Complex;
55 int nits = NDATA / nfft;
56 vector<T> inbuf(nfft);
57 vector<Complex> outbuf(nfft);
58 FFT<Scalar> fft;
Mark Borgerding9c0fcd02009-05-23 10:09:48 -040059
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000060 if (unscaled) {
61 fft.SetFlag(fft.Unscaled);
62 cout << "unscaled ";
63 }
64 if (halfspec) {
65 fft.SetFlag(fft.HalfSpectrum);
66 cout << "halfspec ";
67 }
Mark Borgerding7a6cb2a2010-01-21 21:09:26 -050068
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000069 std::fill(inbuf.begin(), inbuf.end(), 0);
70 fft.fwd(outbuf, inbuf);
Mark Borgerding7a6cb2a2010-01-21 21:09:26 -050071
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000072 BenchTimer timer;
73 timer.reset();
74 for (int k = 0; k < 8; ++k) {
75 timer.start();
Mark Borgerding09b47332009-05-25 23:52:21 -040076 if (fwd)
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000077 for (int i = 0; i < nits; i++) fft.fwd(outbuf, inbuf);
Mark Borgerding09b47332009-05-25 23:52:21 -040078 else
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000079 for (int i = 0; i < nits; i++) fft.inv(inbuf, outbuf);
80 timer.stop();
81 }
Mark Borgerding09b47332009-05-25 23:52:21 -040082
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +000083 cout << nameof<Scalar>() << " ";
84 double mflops = 5. * nfft * log2((double)nfft) / (1e6 * timer.value() / (double)nits);
85 if (NumTraits<T>::IsComplex) {
86 cout << "complex";
87 } else {
88 cout << "real ";
89 mflops /= 2;
90 }
91
92 if (fwd)
93 cout << " fwd";
94 else
95 cout << " inv";
96
97 cout << " NFFT=" << nfft << " " << (double(1e-6 * nfft * nits) / timer.value()) << " MS/s " << mflops << "MFLOPS\n";
Mark Borgerding326ea772009-05-23 22:50:07 -040098}
99
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000100int main(int argc, char** argv) {
101 bench<complex<float> >(NFFT, true);
102 bench<complex<float> >(NFFT, false);
103 bench<float>(NFFT, true);
104 bench<float>(NFFT, false);
105 bench<float>(NFFT, false, true);
106 bench<float>(NFFT, false, true, true);
Mark Borgerding7a6cb2a2010-01-21 21:09:26 -0500107
Antonio Sánchez46e9cdb2023-12-05 21:22:55 +0000108 bench<complex<double> >(NFFT, true);
109 bench<complex<double> >(NFFT, false);
110 bench<double>(NFFT, true);
111 bench<double>(NFFT, false);
112 bench<complex<long double> >(NFFT, true);
113 bench<complex<long double> >(NFFT, false);
114 bench<long double>(NFFT, true);
115 bench<long double>(NFFT, false);
116 return 0;
Mark Borgerding9c0fcd02009-05-23 10:09:48 -0400117}