Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | b596f6c | 2012-07-15 10:33:40 -0400 | [diff] [blame] | 2 | // for linear algebra. |
Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 3 | // |
| 4 | // Copyright (C) 2009 Mark Borgerding mark a borgerding net |
| 5 | // |
Benoit Jacob | 69124cf | 2012-07-13 14:42:47 -0400 | [diff] [blame] | 6 | // 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 Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 9 | |
Thomas Capricelli | 551cb9b | 2010-07-09 03:59:36 +0200 | [diff] [blame] | 10 | #include <iostream> |
| 11 | |
Mark Borgerding | 0fa68b9 | 2009-10-30 19:46:45 -0400 | [diff] [blame] | 12 | #include <bench/BenchUtil.h> |
Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 13 | #include <complex> |
| 14 | #include <vector> |
| 15 | #include <Eigen/Core> |
Mark Borgerding | 1c54340 | 2009-05-31 15:44:57 -0400 | [diff] [blame] | 16 | |
Mark Borgerding | 210092d | 2009-05-25 20:35:24 -0400 | [diff] [blame] | 17 | #include <unsupported/Eigen/FFT> |
Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 18 | |
| 19 | using namespace Eigen; |
| 20 | using namespace std; |
| 21 | |
Mark Borgerding | 326ea77 | 2009-05-23 22:50:07 -0400 | [diff] [blame] | 22 | template <typename T> |
| 23 | string nameof(); |
| 24 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 25 | template <> |
| 26 | string nameof<float>() { |
| 27 | return "float"; |
| 28 | } |
| 29 | template <> |
| 30 | string nameof<double>() { |
| 31 | return "double"; |
| 32 | } |
| 33 | template <> |
| 34 | string nameof<long double>() { |
| 35 | return "long double"; |
| 36 | } |
Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 37 | |
| 38 | #ifndef TYPE |
| 39 | #define TYPE float |
| 40 | #endif |
| 41 | |
Mark Borgerding | 326ea77 | 2009-05-23 22:50:07 -0400 | [diff] [blame] | 42 | #ifndef NFFT |
| 43 | #define NFFT 1024 |
| 44 | #endif |
| 45 | #ifndef NDATA |
| 46 | #define NDATA 1000000 |
Mark Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 47 | #endif |
| 48 | |
Mark Borgerding | 326ea77 | 2009-05-23 22:50:07 -0400 | [diff] [blame] | 49 | using namespace Eigen; |
| 50 | |
| 51 | template <typename T> |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 52 | void 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 Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 59 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 60 | if (unscaled) { |
| 61 | fft.SetFlag(fft.Unscaled); |
| 62 | cout << "unscaled "; |
| 63 | } |
| 64 | if (halfspec) { |
| 65 | fft.SetFlag(fft.HalfSpectrum); |
| 66 | cout << "halfspec "; |
| 67 | } |
Mark Borgerding | 7a6cb2a | 2010-01-21 21:09:26 -0500 | [diff] [blame] | 68 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 69 | std::fill(inbuf.begin(), inbuf.end(), 0); |
| 70 | fft.fwd(outbuf, inbuf); |
Mark Borgerding | 7a6cb2a | 2010-01-21 21:09:26 -0500 | [diff] [blame] | 71 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 72 | BenchTimer timer; |
| 73 | timer.reset(); |
| 74 | for (int k = 0; k < 8; ++k) { |
| 75 | timer.start(); |
Mark Borgerding | 09b4733 | 2009-05-25 23:52:21 -0400 | [diff] [blame] | 76 | if (fwd) |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 77 | for (int i = 0; i < nits; i++) fft.fwd(outbuf, inbuf); |
Mark Borgerding | 09b4733 | 2009-05-25 23:52:21 -0400 | [diff] [blame] | 78 | else |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 79 | for (int i = 0; i < nits; i++) fft.inv(inbuf, outbuf); |
| 80 | timer.stop(); |
| 81 | } |
Mark Borgerding | 09b4733 | 2009-05-25 23:52:21 -0400 | [diff] [blame] | 82 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 83 | 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 Borgerding | 326ea77 | 2009-05-23 22:50:07 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 100 | int 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 Borgerding | 7a6cb2a | 2010-01-21 21:09:26 -0500 | [diff] [blame] | 107 | |
Antonio Sánchez | 46e9cdb | 2023-12-05 21:22:55 +0000 | [diff] [blame] | 108 | 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 Borgerding | 9c0fcd0 | 2009-05-23 10:09:48 -0400 | [diff] [blame] | 117 | } |