Xmipp  v3.23.11-Nereus
fft_settings.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef FFTSETTINGS_H_
27 #define FFTSETTINGS_H_
28 
29 #include "dimensions.h"
30 #include <cassert>
31 #include <complex>
32 
36 
37 // Forward declare both templates (otherwise you'll get: warning: friend declaration declares a non-template function)
38 template <typename T> class FFTSettings;
39 template <typename T> std::ostream& operator<<( std::ostream&, const FFTSettings<T>& );
40 
41 template<typename T>
42 class FFTSettings {
43 public:
44  explicit FFTSettings(size_t x, size_t y = 1, size_t z = 1, size_t n = 1,
45  size_t batch = 1, // meaning no batch processing
46  bool isInPlace = false,
47  bool isForward = true) :
48  m_spatial(x, y, z, n),
49  m_freq(x / 2 + 1, y, z, n),
50  m_batch(batch),
51  m_isInPlace(isInPlace),
52  m_isForward(isForward) {
53  if (isInPlace) {
54  size_t padding = (m_freq.x() * 2) - x;
55  m_spatial = Dimensions(x, y, z, n, padding);
56  }
57  assert(batch <= n);
58  assert(batch > 0);
59  };
60 
61  explicit FFTSettings(const Dimensions &spatial,
62  size_t batch = 1, // meaning no batch processing
63  bool isInPlace = false,
64  bool isForward = true) :
65  m_spatial(spatial),
66  m_freq(spatial.x() / 2 + 1, spatial.y(), spatial.z(), spatial.n()),
67  m_batch(batch),
68  m_isInPlace(isInPlace),
69  m_isForward(isForward) {
70  if (isInPlace) {
71  size_t padding = (m_freq.x() * 2) - spatial.x();
72  m_spatial = Dimensions(spatial.x(), spatial.y(), spatial.z(), spatial.n(), padding);
73  }
74  assert(batch <= spatial.n());
75  assert(batch > 0);
76  };
77 
78  constexpr Dimensions sDim() const {
79  return m_spatial;
80  }
81 
82  constexpr Dimensions fDim() const {
83  return m_freq;
84  }
85 
86  constexpr size_t batch() const {
87  return m_batch;
88  }
89 
90  constexpr size_t fBytesSingle() const {
91  return m_freq.xyzPadded() * sizeof(std::complex<T>);
92  }
93 
94  constexpr size_t fBytes() const {
95  return m_freq.sizePadded() * sizeof(std::complex<T>);
96  }
97 
98  constexpr size_t fBytesBatch() const {
99  return m_freq.xyzPadded() * m_batch * sizeof(std::complex<T>);
100  }
101 
102  constexpr size_t fElemsBatch() const {
103  return m_freq.xyzPadded() * m_batch;
104  }
105 
106  constexpr size_t sBytesSingle() const {
107  return m_spatial.xyzPadded() * sizeof(T);
108  }
109 
110  constexpr size_t sBytes() const {
111  return m_spatial.sizePadded() * sizeof(T);
112  }
113 
114  constexpr size_t sBytesBatch() const {
115  return m_spatial.xyzPadded() * m_batch * sizeof(T);
116  }
117 
118  constexpr size_t sElemsBatch() const {
119  return m_spatial.xyzPadded() * m_batch;
120  }
121 
122  constexpr bool isForward() const {
123  return m_isForward;
124  }
125 
126  constexpr bool isInPlace() const {
127  return m_isInPlace;
128  }
129 
130  constexpr size_t maxBytesBatch() const {
131  return sBytesBatch() + (m_isInPlace ? 0 :fBytesBatch());
132  }
133 
134  inline FFTSettings<T> createInverse() const {
135  auto copy = FFTSettings<T>(*this);
136  copy.m_isForward = ! this->m_isForward;
137  return copy;
138  }
139 
140  inline FFTSettings<T> createSingle() const {
141  auto copy = FFTSettings<T>(m_spatial.x(), m_spatial.y(), m_spatial.z(), 1, 1,
142  this->isInPlace(), this->isForward());
143  return copy;
144  }
145 
146  inline FFTSettings<T> createBatch() const {
147  auto copy = FFTSettings<T>(m_spatial.x(), m_spatial.y(), m_spatial.z(), m_batch, m_batch,
148  this->isInPlace(), this->isForward());
149  return copy;
150  }
151 
152  inline FFTSettings<T> createSubset(size_t n) const {
153  assert(n <= m_spatial.n());
154  auto copy = FFTSettings<T>(m_spatial.x(), m_spatial.y(), m_spatial.z(), n, n,
155  this->isInPlace(), this->isForward());
156  return copy;
157  }
158 
159  inline FFTSettings<T> copyForBatch(size_t b) const {
160  assert(b <= m_spatial.n());
161  auto copy = FFTSettings<T>(m_spatial.x(), m_spatial.y(), m_spatial.z(), m_spatial.n(), b,
162  this->isInPlace(), this->isForward());
163  return copy;
164  }
165 
166  friend std::ostream &operator<< <T>(std::ostream &os,
167  const FFTSettings<T> &s);
168 
169 private:
170  Dimensions m_spatial;
171  Dimensions m_freq;
172  size_t m_batch;
173  bool m_isInPlace;
174  bool m_isForward;
175 };
176 
177 
179 #endif /* FFTSETTINGS_H_ */
constexpr size_t xyzPadded() const
Definition: dimensions.h:95
constexpr size_t fBytes() const
Definition: fft_settings.h:94
FFTSettings< T > createSubset(size_t n) const
Definition: fft_settings.h:152
constexpr bool isInPlace() const
Definition: fft_settings.h:126
CUDA_HD constexpr size_t z() const
Definition: dimensions.h:69
constexpr size_t fBytesBatch() const
Definition: fft_settings.h:98
static double * y
constexpr size_t sBytes() const
Definition: fft_settings.h:110
constexpr Dimensions fDim() const
Definition: fft_settings.h:82
constexpr size_t sBytesSingle() const
Definition: fft_settings.h:106
constexpr size_t fElemsBatch() const
Definition: fft_settings.h:102
FFTSettings(const Dimensions &spatial, size_t batch=1, bool isInPlace=false, bool isForward=true)
Definition: fft_settings.h:61
doublereal * x
FFTSettings< T > copyForBatch(size_t b) const
Definition: fft_settings.h:159
constexpr size_t batch() const
Definition: fft_settings.h:86
CUDA_HD constexpr size_t x() const
Definition: dimensions.h:51
doublereal * b
FFTSettings(size_t x, size_t y=1, size_t z=1, size_t n=1, size_t batch=1, bool isInPlace=false, bool isForward=true)
Definition: fft_settings.h:44
CUDA_HD constexpr size_t y() const
Definition: dimensions.h:60
double z
FFTSettings< T > createInverse() const
Definition: fft_settings.h:134
constexpr size_t sizePadded() const
Definition: dimensions.h:108
constexpr size_t maxBytesBatch() const
Definition: fft_settings.h:130
CUDA_HD constexpr size_t n() const
Definition: dimensions.h:78
constexpr Dimensions sDim() const
Definition: fft_settings.h:78
constexpr size_t sElemsBatch() const
Definition: fft_settings.h:118
FFTSettings< T > createBatch() const
Definition: fft_settings.h:146
constexpr bool isForward() const
Definition: fft_settings.h:122
int * n
constexpr size_t fBytesSingle() const
Definition: fft_settings.h:90
constexpr size_t sBytesBatch() const
Definition: fft_settings.h:114
FFTSettings< T > createSingle() const
Definition: fft_settings.h:140