Xmipp  v3.23.11-Nereus
xmipp_gpu_utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Amaya Jimenez ajimenez@cnb.csic.es (2017)
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 #ifndef _PROG_GPU_UTILS
26 #define _PROG_GPU_UTILS
27 
28 #include <core/multidim_array.h>
31 
35 template<typename T>
37 {
38 public:
40  T* data;
41 
43  {
44  Xdim=Ydim=Zdim=Ndim=yxdim=zyxdim=nzyxdim=0;
45  data=NULL;
46  }
47 
48  GpuMultidimArrayAtCpu(int _Xdim, int _Ydim=1, int _Zdim=1, int _Ndim=1)
49  {
50  Xdim=Ydim=Zdim=Ndim=yxdim=zyxdim=nzyxdim=0;
51  data=NULL;
52  resize(_Xdim, _Ydim, _Zdim, _Ndim);
53  }
54 
55  void resize(int _Xdim, int _Ydim=1, int _Zdim=1, int _Ndim=1)
56  {
57  //FIXME what happens if x and y swaps?
58  if (_Xdim*_Ydim*_Zdim*_Ndim==nzyxdim){
59 
60  return;
61  }
62 
63  clear();
64 
65  Xdim=_Xdim;
66  Ydim=_Ydim;
67  Zdim=_Zdim;
68  Ndim=_Ndim;
69  yxdim=(size_t)_Ydim*_Xdim;
70  zyxdim=yxdim*_Zdim;
71  nzyxdim=zyxdim*_Ndim;
72  if (nzyxdim>0){
73  //data=new T[nzyxdim];
74  cpuMalloc((void**)&data, nzyxdim*sizeof(T));
75  }else
76  data=NULL;
77  }
78 
79  void fillImage(int n, const MultidimArray<T> &from)
80  {
81  //TODO: preguntar esto, en el host puedo usar un memcpy??
82  memcpy(data+n*zyxdim, MULTIDIM_ARRAY(from), MULTIDIM_SIZE(from)*sizeof(T));
83  }
84 
85  bool isEmpty()
86  {
87  return data==NULL;
88  }
89 
91  {
92  if (gpuArray.isEmpty()){
93  gpuArray.resize(Xdim,Ydim,Zdim,Ndim);
94  }
95 
96  gpuCopyFromCPUToGPUStream(data, gpuArray.d_data, nzyxdim*sizeof(T), myStream);
97  }
98 
100  {
101  gpuCopyFromGPUToCPUStream(gpuArray.d_data, data, nzyxdim*sizeof(T), myStream);
102 
103  }
104 
105  void copyToGpuMultiple(GpuMultidimArrayAtGpu<T> &gpuArray, int numCopy, myStreamHandle myStream)
106  {
107  if (gpuArray.isEmpty())
108  gpuArray.resize(Xdim,Ydim,Zdim,numCopy);
109 
110  int index = 0;
111  for(int i=0; i<numCopy; i++){
112  gpuCopyFromCPUToGPUStream(data, &gpuArray.d_data[index], nzyxdim*sizeof(T), myStream);
113  index=index+nzyxdim;
114  }
115  }
116 
118  if (out.isEmpty())
119  out.resize(Xdim,Ydim,Zdim,1);
120  memcpy(out.data, data[idx*zyxdim], zyxdim*sizeof(T));
121  }
122 
124  memcpy(data[idx*zyxdim], in.data, zyxdim*sizeof(T));
125  }
126 
127  void clear();
128 
130  {
131  clear();
132  }
133 };
134 
135 template<class T>
136 void fillImage(GpuMultidimArrayAtGpu<T> &to, const MultidimArray<T> &from, myStreamHandle myStream, int n=0)
137 {
138  gpuCopyFromCPUToGPUStream(MULTIDIM_ARRAY(from), to.d_data+n*MULTIDIM_SIZE(from), MULTIDIM_SIZE(from)*sizeof(T), myStream);
139 }
140 
142 #endif
143 
144 
145 
146 
147 
148 
void resize(const GpuMultidimArrayAtGpu< T1 > &array)
#define MULTIDIM_SIZE(v)
void copyToGpu(GpuMultidimArrayAtGpu< T > &gpuArray, myStreamHandle &myStream)
void copyToGpuMultiple(GpuMultidimArrayAtGpu< T > &gpuArray, int numCopy, myStreamHandle myStream)
#define MULTIDIM_ARRAY(v)
void fillImage(int n, const MultidimArray< T > &from)
void giveOneImage(GpuMultidimArrayAtCpu< T > out, int idx)
#define i
void gpuCopyFromGPUToCPUStream(void *d_data, void *data, size_t Nbytes, myStreamHandle &myStream)
viol index
int in
void copyFromGpu(GpuMultidimArrayAtGpu< T > &gpuArray, myStreamHandle myStream)
void resize(int _Xdim, int _Ydim=1, int _Zdim=1, int _Ndim=1)
GpuMultidimArrayAtCpu(int _Xdim, int _Ydim=1, int _Zdim=1, int _Ndim=1)
void gpuCopyFromCPUToGPUStream(void *data, void *d_data, size_t Nbytes, myStreamHandle &myStream)
void cpuMalloc(void **h_data, size_t Nbytes)
void setOneImage(GpuMultidimArrayAtCpu< T > in, int idx)
int * n