Xmipp  v3.23.11-Nereus
Public Member Functions | List of all members
GeoShiftTransformer< T > Class Template Reference

#include <cuda_gpu_geo_shift_transformer.h>

Public Member Functions

 GeoShiftTransformer ()
 
 ~GeoShiftTransformer ()
 
void init (const GPU &gpu, size_t x, size_t y, size_t n, int device, myStreamHandle *stream)
 
void initLazy (const GPU &gpu, size_t x, size_t y, size_t n, int device, myStreamHandle *stream=NULL)
 
void release ()
 
template<typename T_IN >
void applyShift (MultidimArray< T > &output, const MultidimArray< T_IN > &input, T shiftX, T shiftY)
 
void test ()
 

Detailed Description

template<typename T>
class GeoShiftTransformer< T >

Definition at line 51 of file cuda_gpu_geo_shift_transformer.h.

Constructor & Destructor Documentation

◆ GeoShiftTransformer()

template<typename T>
GeoShiftTransformer< T >::GeoShiftTransformer ( )
inline

Constructor

Definition at line 55 of file cuda_gpu_geo_shift_transformer.h.

55  :
56  device(-1), imgs(NULL), ffts(NULL), stream(NULL), isReady(false) {
57  };

◆ ~GeoShiftTransformer()

template<typename T>
GeoShiftTransformer< T >::~GeoShiftTransformer ( )
inline

Definition at line 59 of file cuda_gpu_geo_shift_transformer.h.

59  {
60  release();
61  }

Member Function Documentation

◆ applyShift()

template<typename T >
template<typename T_IN >
void GeoShiftTransformer< T >::applyShift ( MultidimArray< T > &  output,
const MultidimArray< T_IN > &  input,
shiftX,
shiftY 
)

Apply 2D shift. Image will be repeated at the border.

Parameters
outputwhere resulting images will be stored. Does not have to be initialized
inputto process
shiftXto apply
shiftYto apply

Definition at line 87 of file cuda_gpu_geo_shift_transformer.cpp.

88  {
89  checkRestrictions(output, input);
90  if (output.xdim == 0) {
91  output.resizeNoCopy(input.ndim, input.zdim, input.ydim, input.xdim);
92  }
93  if (shiftX == 0 && shiftY == 0) {
94  typeCast(input, output);
95  return;
96  }
97 
99 
100  MultidimArray<T> tmp;
101  typeCast(input, tmp);
102  imgs->copyToGpu(tmp.data);
103  if(stream) {
104  imgs->fftStream(*ffts, fftHandle, *stream, false, mask);
105  } else {
106  imgs->fft(*ffts, fftHandle);
107  }
108 
109  dim3 dimBlock(BLOCK_DIM_X, BLOCK_DIM_X);
110  dim3 dimGrid(ceil(ffts->Xdim / (T) dimBlock.x), ceil(ffts->Ydim / (T) dimBlock.y));
111 
112  if (stream) {
113  shiftFFT2D<true><<<dimGrid, dimBlock, 0, *(cudaStream_t*)stream->ptr>>>(
114  (float2*)ffts->d_data, ffts->Ndim, ffts->Xdim, imgs->Xdim,
115  imgs->Ydim, shiftX, shiftY);
116  } else {
117  shiftFFT2D<true><<<dimGrid, dimBlock>>>((float2*)ffts->d_data,
118  ffts->Ndim, ffts->Xdim, imgs->Xdim, imgs->Ydim, shiftX, shiftY);
119  }
120  gpuErrchk(cudaPeekAtLastError());
121 
122  if (stream) {
123  ffts->ifftStream(*imgs, ifftHandle, *stream, false, mask);
124  } else {
125  ffts->ifft(*imgs, ifftHandle);
126  }
127  imgs->copyToCpu(output.data);
128 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
void resizeNoCopy(const MultidimArray< T1 > &v)
void ifft(GpuMultidimArrayAtGpu< T1 > &realSpace, mycufftHandle &myhandle)
#define BLOCK_DIM_X
void ifftStream(GpuMultidimArrayAtGpu< T1 > &realSpace, mycufftHandle &myhandle, myStreamHandle &myStream, bool useCallback, GpuMultidimArrayAtGpu< std::complex< float > > &dataExp)
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227

◆ init()

template<typename T >
void GeoShiftTransformer< T >::init ( const GPU gpu,
size_t  x,
size_t  y,
size_t  n,
int  device,
myStreamHandle stream 
)

Release previously obtained resources and initialize the transformer for processing images of given size. It also allocates all resources on GPU.

Parameters
gputo use
xdim (inner-most) of the resulting image
ydim (outer-most) of the resulting image
nno. of images to process in a single batch
deviceto be used
streamto be used. NULL for default

Definition at line 53 of file cuda_gpu_geo_shift_transformer.cpp.

54  {
55  release();
56 
57  this->device = device;
58  this->stream = stream;
59 
60  m_gpu = &gpu; // FIXME DS implement set() check
61  this->imgs = new GpuMultidimArrayAtGpu<T>(x, y, 1, n);
62  this->ffts = new GpuMultidimArrayAtGpu<std::complex<T> >(x / 2 + 1, y, 1, n);
63 
64  fftHandle.ptr = new cufftHandle;
65  ifftHandle.ptr = new cufftHandle;
66  if (this->stream) {
67  createPlanFFTStream(x, y, n, 1, true, (cufftHandle*)fftHandle.ptr, *stream);
68  createPlanFFTStream(x, y, n, 1, false, (cufftHandle*)ifftHandle.ptr, *stream);
69  } else {
70  createPlanFFT(x, y, n, 1, true, (cufftHandle*)fftHandle.ptr);
71  createPlanFFT(x, y, n, 1, false, (cufftHandle*)ifftHandle.ptr);
72  }
73 
74  isReady = true;
75 }
static double * y
void createPlanFFTStream(int Xdim, int Ydim, int Ndim, int Zdim, bool forward, cufftHandle *plan, myStreamHandle &myStream)
doublereal * x
int cufftHandle
Definition: cuda_fft.h:41
void createPlanFFT(int Xdim, int Ydim, int Ndim, int Zdim, bool forward, cufftHandle *plan)
int * n

◆ initLazy()

template<typename T >
void GeoShiftTransformer< T >::initLazy ( const GPU gpu,
size_t  x,
size_t  y,
size_t  n,
int  device,
myStreamHandle stream = NULL 
)

Similar as init(), except this method has no effect should the instance be already initialized. It is useful for example in a for loop, where first call will initialize resources and following calls will be ignored Do NOT use it for reinitialization.

Definition at line 78 of file cuda_gpu_geo_shift_transformer.cpp.

79  {
80  if (!isReady) {
81  init(gpu, x, y, n, device, stream);
82  }
83 }
static double * y
doublereal * x
void init(const GPU &gpu, size_t x, size_t y, size_t n, int device, myStreamHandle *stream)
int * n

◆ release()

template<typename T >
void GeoShiftTransformer< T >::release ( )

Release all resources hold by this instance

Definition at line 34 of file cuda_gpu_geo_shift_transformer.cpp.

34  {
35  delete imgs;
36  imgs = nullptr;
37  delete ffts;
38  ffts = nullptr;
39 
40  fftHandle.clear();
41  ifftHandle.clear();
42 
43  // do not release stream, it has been passed by pointer, so we don't own it
44  stream = nullptr;
45  device = -1;
46 
47  m_gpu = nullptr;
48 
49  isReady = false;
50 }

◆ test()

template<typename T >
void GeoShiftTransformer< T >::test ( )

Definition at line 131 of file cuda_gpu_geo_shift_transformer.cpp.

131  {
132  int offsetX = 5;
133  int offsetY = -7;
134  size_t xSize = 2048;
135  size_t ySize = 3072;
136  MultidimArray<float> resGpu(ySize, xSize);
137  MultidimArray<float> expected(ySize, xSize);
138  MultidimArray<float> input(ySize, xSize);
139  for (int y = 10; y < 15; ++y) {
140  for (int x = 10; x < 15; ++x) {
141  int indexIn = (y * input.xdim) + x;
142  int indexExp = ((y + offsetY) * input.xdim) + (x + offsetX);
143  input.data[indexIn] = 10;
144  expected.data[indexExp] = 10;
145  }
146  }
147 
149  auto gpu = GPU();
150  tr.initLazy(gpu, input.xdim, input.ydim, 1, 0);
151  tr.applyShift(resGpu, input, offsetX, offsetY);
152 
153  bool error = false;
154  for (int y = 0; y < expected.ydim; ++y) {
155  for (int x = 0; x < expected.xdim; ++x) {
156  int index = (y * input.xdim) + x;
157  float gpu = resGpu.data[index];
158  float cpu = expected.data[index];
159  float threshold = std::max(gpu, cpu) / 1000.f;
160  float diff = std::abs(cpu - gpu);
161  if (diff > threshold && diff > 0.001) {
162  error = true;
163  printf("%d gpu %.4f cpu %.4f (%f > %f)\n", index, gpu, cpu, diff, threshold);
164  }
165  }
166  }
167 #ifdef DEBUG
168  Image<float> img(expected.xdim, expected.ydim);
169  img.data = expected;
170  img.write("expected.vol");
171  img.data = resGpu;
172  img.write("resGpu.vol");
173 #endif
174  printf("\n SHIFT %s\n", error ? "FAILED" : "OK");
175 }
void applyShift(MultidimArray< T > &output, const MultidimArray< T_IN > &input, T shiftX, T shiftY)
static double * y
void abs(Image< double > &op)
doublereal * x
void threshold(double *phi, unsigned long nvox, double limit)
Definition: lib_vwk.cpp:524
viol index
void max(Image< double > &op1, const Image< double > &op2)
void initLazy(const GPU &gpu, size_t x, size_t y, size_t n, int device, myStreamHandle *stream=NULL)

The documentation for this class was generated from the following files: