Xmipp  v3.23.11-Nereus
cuda_gpu_geo_transformer.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 
37 #ifndef CUDA_GEO_TRANSFORMER
38 #define CUDA_GEO_TRANSFORMER
39 
40 #include <assert.h>
41 #include <stdexcept>
42 #include <memory>
43 #include "core/multidim_array.h"
44 #include "core/transformations.h"
45 #include "core/xmipp_image.h" // for tests only
46 #include "core/bilib/iirconvolve.h" // for tests only
48 #include "cuda_xmipp_utils.h"
50 
54 template<typename T>
56 
57 public:
59  GeoTransformer() { setDefaultValues(); };
60 
62  release();
63  }
64 
73  void initForMatrix(size_t x, size_t y, size_t z);
74 
81  void initLazyForMatrix(size_t x, size_t y = 1, size_t z = 1);
82 
90  void initForBSpline(size_t inX, size_t inY, size_t inN,
91  size_t splineX, size_t splineY, size_t splineN, const GPU &gpu);
92 
99  void initLazyForBSpline(size_t inX, size_t inY, size_t inN,
100  size_t splineX, size_t splineY, size_t splineN, const GPU &gpu);
101 
105  void release();
106 
121  template<typename T_MAT>
122  void applyGeometry(int splineDegree, MultidimArray<T> &output,
123  const MultidimArray<T> &input, const Matrix2D<T_MAT> &transform,
124  bool isInv, bool wrap, T outside = 0,
125  const MultidimArray<T> *bCoeffsPtr = NULL);
126 
137  void applyBSplineTransform(int splineDegree,
138  MultidimArray<T> &output, const MultidimArray<T> &input,
139  const std::pair<Matrix1D<T>, Matrix1D<T>> &coeffs, size_t imageIdx, T outside = 0);
140 
141  void test();
142 
143 private:
151  template<typename T_MAT>
152  void checkRestrictions(int splineDegree, MultidimArray<T> &output,
153  const MultidimArray<T> &input, const Matrix2D<T_MAT> &transform);
154 
163  void checkRestrictions(int splineDegree,
164  MultidimArray<T> &output, const MultidimArray<T> &input,
165  const std::pair<Matrix1D<T>, Matrix1D<T>> &coeffs, size_t frameIdx);
166 
167 
173  void checkRestrictions(const MultidimArray<T> &output,
174  const MultidimArray<T> &input);
175 
181  void loadOutput(MultidimArray<T> &output, T outside);
182 
187  void loadInput(const MultidimArray<T> &input);
188 
193  void applyGeometry_2D_wrap(int SplineDegree);
194 
195 
202  template<typename T_MAT>
203  void loadTransform(const Matrix2D<T_MAT> &transform, bool isInv);
204 
208  void setDefaultValues();
209 
210 
211  void test(const Matrix2D<T> &transform);
212 
216  void loadCoefficients(const Matrix1D<T> &X,
217  const Matrix1D<T> &Y);
218 
222  void setOutputSize(MultidimArray<T> &output);
223 
224 
225 protected:
226  /*
227  * Reference computation used for the testing of a faster kernel
228  */
229  void applyBSplineTransformRef(int splineDegree,
230  MultidimArray<T> &output, const MultidimArray<T> &input,
231  const std::pair<Matrix1D<T>, Matrix1D<T>> &coeffs, size_t imageIdx, T outside = 0);
232 
238  void produceAndLoadCoeffs(const MultidimArray<T> &input);
239 
240  /*
241  * Creates a copy of device input memory
242  * Used in tests
243  */
244  std::unique_ptr<T[]> copy_out_d_in( size_t size ) const;
245 
246 private:
247  bool isReadyForMatrix;
248  bool isReadyForBspline;
249 
250  T *d_trInv; // memory on GPU with inverse transformation (dest->src)
251  T *d_in; // memory on GPU with input data
252  T *d_out; // memory in GPU with output data
253 
254  T *d_coeffsX; // coefficients of the BSpline transformation, X direction
255  T *d_coeffsY; // coefficients of the BSpline transformation, Y direction
256 
257  const GPU *gpu;
258 
259  // dimensions of the input/output data
260  size_t inX;
261  size_t inY;
262  size_t inZ;
263  size_t inN;
264 
265  // dimension of the coefficients control points
266  size_t splineX;
267  size_t splineY;
268  size_t splineN;
269 
270  constexpr static const T transposeTileDim = (T)32;
271  constexpr static const T transposeBlockRow = (T)8;
272  constexpr static const int pixelsPerThread = 2;
273 };
275 #endif // CUDA_GEO_TRANSFORMER
void produceAndLoadCoeffs(const MultidimArray< T > &input)
static double * y
void initForMatrix(size_t x, size_t y, size_t z)
doublereal * x
Definition: mask.h:36
void initLazyForMatrix(size_t x, size_t y=1, size_t z=1)
void applyBSplineTransformRef(int splineDegree, MultidimArray< T > &output, const MultidimArray< T > &input, const std::pair< Matrix1D< T >, Matrix1D< T >> &coeffs, size_t imageIdx, T outside=0)
void initLazyForBSpline(size_t inX, size_t inY, size_t inN, size_t splineX, size_t splineY, size_t splineN, const GPU &gpu)
double z
std::unique_ptr< T[]> copy_out_d_in(size_t size) const
Definition: ctf.h:38
void initForBSpline(size_t inX, size_t inY, size_t inN, size_t splineX, size_t splineY, size_t splineN, const GPU &gpu)
Definition: gpu.h:36
void applyBSplineTransform(int splineDegree, MultidimArray< T > &output, const MultidimArray< T > &input, const std::pair< Matrix1D< T >, Matrix1D< T >> &coeffs, size_t imageIdx, T outside=0)
void applyGeometry(int splineDegree, MultidimArray< T > &output, const MultidimArray< T > &input, const Matrix2D< T_MAT > &transform, bool isInv, bool wrap, T outside=0, const MultidimArray< T > *bCoeffsPtr=NULL)