Blame view

math/realfield.cuh 9.06 KB
821513c8   David Mayerich   ERROR plane wave ...
1
2
3
  #ifndef	RTS_REALFIELD_H

  #define RTS_REALFIELD_H

  

d609550e   David Mayerich   fixed bug in plan...
4
5
6
7
8
  #include "../visualization/colormap.h"

  #include "../envi/envi.h"

  #include "../math/rect.h"

  #include "../cuda/devices.h"

  #include "cublas_v2.h"

821513c8   David Mayerich   ERROR plane wave ...
9
10
  #include <cuda_runtime.h>

  

d609550e   David Mayerich   fixed bug in plan...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  ///Compute a Gaussian function in 3D (mostly for testing)

  /*template<typename T>

  __global__ void gpu_gaussian(T* dest, unsigned int r0, unsigned int r1, T mean, T std, rts::rect<T> shape)

  {

  	int iu = blockIdx.x * blockDim.x + threadIdx.x;

  	int iv = blockIdx.y * blockDim.y + threadIdx.y;

  

  	//make sure that the thread indices are in-bounds

  	if(iu >= r0 || iv >= r1) return;

  

  	//compute the index into the field

  	int i = iv*r0 + iu;

  

  	T u = (T)iu / (T)r0;

  	T v = (T)iv / (T)r1;

  

  	rts::vec<T> p = shape(u, v);

  

  	T fx = (T)1.0 / (std * (T)sqrt(2 * 3.14159f) ) * exp( - pow(p[0] - mean, 2) / (2 * std*std) );

  	T fy = (T)1.0 / (std * (T)sqrt(2 * 3.14159f) ) * exp( - pow(p[1] - mean, 2) / (2 * std*std) );

  	T fz = (T)1.0 / (std * (T)sqrt(2 * 3.14159f) ) * exp( - pow(p[2] - mean, 2) / (2 * std*std) );

  

  	dest[i] = fx * fy * fz;

821513c8   David Mayerich   ERROR plane wave ...
34
35
36
37
  }*/

  

  namespace rts{

  

ecfd14df   David Mayerich   implemented D fie...
38
39
  //multiply R = X * Y

  template<typename T>

7a2d0012   David Mayerich   mirst1d updates
40
  __global__ void gpu_realfield_multiply(T* R, T* X, T* Y, unsigned int r0, unsigned int r1){

ecfd14df   David Mayerich   implemented D fie...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  

  	int iu = blockIdx.x * blockDim.x + threadIdx.x;

      int iv = blockIdx.y * blockDim.y + threadIdx.y;

  

      //make sure that the thread indices are in-bounds

      if(iu >= r0 || iv >= r1) return;

  

      //compute the index into the field

      int i = iv*r0 + iu;

  

      //calculate and store the result

      R[i] = X[i] * Y[i];

  

  }

  

821513c8   David Mayerich   ERROR plane wave ...
56
57
58
  template<typename P, unsigned int N = 1, bool positive = false>

  class realfield{

  

d609550e   David Mayerich   fixed bug in plan...
59
60
61
  	P* X[N];		//an array of N gpu pointers for each field component

  	int R[2];		//resolution of the slice

  	rect<P> shape;

821513c8   David Mayerich   ERROR plane wave ...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  

  	void process_filename(std::string name, std::string &prefix, std::string &postfix, 

                            std::string &ext, unsigned int &digits)

      {

          std::stringstream ss(name);

          std::string item;

          std::vector<std::string> elems;

          while(std::getline(ss, item, '.'))      //split the string at the '.' character (filename and extension)

          {

              elems.push_back(item);

          }

          

          prefix = elems[0];                      //prefix contains the filename (with wildcard '?' characters)

          ext = elems[1];                         //file extension (ex. .bmp, .png)

          ext = std::string(".") + ext;           //add a period back into the extension

  

          size_t i0 = prefix.find_first_of("?");  //find the positions of the first and last wildcard ('?'')

          size_t i1 = prefix.find_last_of("?");

  

          postfix = prefix.substr(i1+1);

          prefix = prefix.substr(0, i0);

  

          digits = i1 - i0 + 1;                   //compute the number of wildcards

      }

  

  	void init()

  	{

d609550e   David Mayerich   fixed bug in plan...
89
  		for(unsigned int n=0; n<N; n++)

821513c8   David Mayerich   ERROR plane wave ...
90
91
92
93
  			X[n] = NULL;

  	}

  	void destroy()

  	{

d609550e   David Mayerich   fixed bug in plan...
94
95
  		for(unsigned int n=0; n<N; n++)

  			if(X[n] != NULL)

821513c8   David Mayerich   ERROR plane wave ...
96
97
98
99
100
101
102
103
104
  				HANDLE_ERROR(cudaFree(X[n]));

  	}

  

  public:

  	//field constructor

  	realfield()

  	{

  		R[0] = R[1] = 0;

  		init();

559d0fcb   David Mayerich   wave interactions...
105
  		//std::cout<<"realfield CONSTRUCTOR"<<std::endl;

821513c8   David Mayerich   ERROR plane wave ...
106
  	}

d609550e   David Mayerich   fixed bug in plan...
107
108
109
110
111
112
113
114
  	realfield(unsigned int x, unsigned int y)

  	{

          //set the resolution

          R[0] = x;

          R[1] = y;

  		//allocate memory on the GPU

  		for(unsigned int n=0; n<N; n++)

  		{

821513c8   David Mayerich   ERROR plane wave ...
115
  			HANDLE_ERROR(cudaMalloc( (void**)&X[n], sizeof(P) * R[0] * R[1] ));

d609550e   David Mayerich   fixed bug in plan...
116
117
118
  		}

  		//shape = rect<P>(vec<P>(-1, -1, 0), vec<P>(-1, 1, 0), vec<P>(1, 1, 0));	//default geometry

  		clear();		//zero the field

559d0fcb   David Mayerich   wave interactions...
119
  		//std::cout<<"realfield CONSTRUCTOR"<<std::endl;

821513c8   David Mayerich   ERROR plane wave ...
120
121
      }

  

d609550e   David Mayerich   fixed bug in plan...
122
123
124
  	~realfield()

      {

  		destroy();

559d0fcb   David Mayerich   wave interactions...
125
  		//std::cout<<"realfield DESTRUCTOR"<<std::endl;

821513c8   David Mayerich   ERROR plane wave ...
126
127
      }

  

ecfd14df   David Mayerich   implemented D fie...
128
  	P* ptr(unsigned int n = 0)

821513c8   David Mayerich   ERROR plane wave ...
129
130
131
132
133
134
135
  	{

  		if(n < N)

  			return X[n];

  		else return NULL;

  	}

  

  	//set all components of the field to zero

d609550e   David Mayerich   fixed bug in plan...
136
137
138
139
140
  	void clear()

      {

  		for(unsigned int n=0; n<N; n++)

  			if(X[n] != NULL)

  				HANDLE_ERROR(cudaMemset(X[n], 0, sizeof(P) * R[0] * R[1]));

821513c8   David Mayerich   ERROR plane wave ...
141
142
143
144
145
146
147
      }

  

  	void toImage(std::string filename, unsigned int n, P vmin, P vmax, rts::colormapType cmap = rts::cmBrewer)

      {

  		rts::gpu2image<P>(X[n], filename, R[0], R[1], vmin, vmax, cmap);

      }

  

d609550e   David Mayerich   fixed bug in plan...
148
  	void toImages(std::string filename, bool global_max = true, rts::colormapType cmap = rts::cmBrewer)

821513c8   David Mayerich   ERROR plane wave ...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  	{

          std::string prefix, postfix, extension;

          unsigned int digits;

          process_filename(filename, prefix, postfix, extension, digits);      //process the filename for wild cards

  

          cublasStatus_t stat;

          cublasHandle_t handle;

  

          //create a CUBLAS handle

          stat = cublasCreate(&handle);

          if(stat != CUBLAS_STATUS_SUCCESS)

          {

              std::cout<<"CUBLAS Error: initialization failed"<<std::endl;

              exit(1);

          }

  

          int L = R[0] * R[1];    //compute the number of discrete points in a slice

          int result;             //result of the max operation

  

          P maxVal[N];            //array stores minimum and maximum values

          P maxAll = 0;           //largest value in the data set

  

          //compute the maximum value for each vector component

          for(int n=0; n<N; n++)

          {

              if(sizeof(P) == 4)

                  stat = cublasIsamax(handle, L, (const float*)X[n], 1, &result);

              else

                  stat = cublasIdamax(handle, L, (const double*)X[n], 1, &result);

  

              result -= 1;        //adjust for 1-based indexing

  

              if(stat != CUBLAS_STATUS_SUCCESS)   //if there was a GPU error, terminate

              {

                  std::cout<<"CUBLAS Error: failure finding maximum value."<<std::endl;

                  exit(1);

              }

  

              //retrieve the maximum value for this slice and store it in the maxVal array

              HANDLE_ERROR(cudaMemcpy(&maxVal[n], X[n] + result, sizeof(P), cudaMemcpyDeviceToHost));

              if(abs(maxVal[n]) > maxAll)          //if maxVal is larger, update the maxAll variable

                  maxAll = maxVal[n];

  

          }

          

          cublasDestroy(handle);  //destroy the CUBLAS handle

  

d609550e   David Mayerich   fixed bug in plan...
196
          P outputMax = abs(maxAll);			//maximum value used for each output image

821513c8   David Mayerich   ERROR plane wave ...
197
198
  		for(int n=0; n<N; n++)          //for each image

  		{

d609550e   David Mayerich   fixed bug in plan...
199
200
  			if(!global_max) outputMax = maxVal[n];	//calculate the maximum value for this image

  

821513c8   David Mayerich   ERROR plane wave ...
201
202
203
204
  			stringstream ss;            //assemble the file name

  			ss<<prefix<<std::setfill('0')<<std::setw(digits)<<n<<postfix<<extension;

  			std::cout<<ss.str()<<std::endl;

  			if(positive)                //if the image is positive

d609550e   David Mayerich   fixed bug in plan...
205
  				toImage(ss.str(), n, 0, abs(outputMax), cmap);         //save the image using the global maximum

821513c8   David Mayerich   ERROR plane wave ...
206
  			else

d609550e   David Mayerich   fixed bug in plan...
207
  				toImage(ss.str(), n, -abs(outputMax), abs(outputMax), cmap);   //save the image using the global maximum

821513c8   David Mayerich   ERROR plane wave ...
208
209
210
  		}

  	}

  

d609550e   David Mayerich   fixed bug in plan...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  	//assignment operator

  	realfield & operator= (const realfield & rhs)

      {

          //de-allocate any existing GPU memory

          destroy();

  

          //copy the slice resolution

          R[0] = rhs.R[0];

          R[1] = rhs.R[1];

  

  		for(unsigned int n=0; n<N; n++)

  		{

  			//allocate the necessary memory

  			HANDLE_ERROR(cudaMalloc(&X[n], sizeof(P) * R[0] * R[1]));

  			//copy the slice

  			HANDLE_ERROR(cudaMemcpy(X[n], rhs.X[n], sizeof(P) * R[0] * R[1], cudaMemcpyDeviceToDevice));

  		}

559d0fcb   David Mayerich   wave interactions...
228
          //std::cout<<"Assignment operator."<<std::endl;

d609550e   David Mayerich   fixed bug in plan...
229
230
231
232
  

          return *this;

      }

  

ecfd14df   David Mayerich   implemented D fie...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      //multiply two fields (element-wise multiplication)

      realfield<P, N, positive> operator* (const realfield & rhs){

  

      	int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size

          int SQRT_BLOCK = (int)std::sqrt((float)maxThreads);

  

          //create one thread for each detector pixel

          dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK);

          dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK);

  

          //create a scalar field to store the result

          realfield<P, N, positive> result(R[0], R[1]);

  

          for(int n=0; n<N; n++)

7a2d0012   David Mayerich   mirst1d updates
247
          	rts::gpu_realfield_multiply <<<dimGrid, dimBlock>>> (result.X[n], X[n], rhs.X[n], R[0], R[1]);

ecfd14df   David Mayerich   implemented D fie...
248
249
  

          return result;

ecfd14df   David Mayerich   implemented D fie...
250
251
      }

  

d609550e   David Mayerich   fixed bug in plan...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  	///copy constructor

  	realfield(const realfield &rhs)

  	{

  		//first make a shallow copy

  		R[0] = rhs.R[0];

  		R[1] = rhs.R[1];

  

  		for(unsigned int n=0; n<N; n++)

  		{

  			//do we have to make a deep copy?

  			if(rhs.X[n] == NULL)

  				X[n] = NULL;		//no

  			else

  			{

  				//allocate the necessary memory

  				HANDLE_ERROR(cudaMalloc(&X[n], sizeof(P) * R[0] * R[1]));

  

  				//copy the slice

  				HANDLE_ERROR(cudaMemcpy(X[n], rhs.X[n], sizeof(P) * R[0] * R[1], cudaMemcpyDeviceToDevice));

  			}

  		}

  

559d0fcb   David Mayerich   wave interactions...
274
  		//std::cout<<"realfield COPY CONSTRUCTOR"<<std::endl;

821513c8   David Mayerich   ERROR plane wave ...
275
276
  	}

  

d609550e   David Mayerich   fixed bug in plan...
277
278
279
280
281
282
283
284
285
  	/*void gaussian(P mean, P std, unsigned int n=0)	//creates a 3D gaussian using component n

  	{

  		int maxThreads = rts::maxThreadsPerBlock(); //compute the optimal block size

          int SQRT_BLOCK = (int)sqrt((float)maxThreads);

  		//create one thread for each detector pixel

  		dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK);

  		dim3 dimGrid((R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK);

  

  		gpu_gaussian<float> <<<dimGrid, dimBlock>>> (X[n], R[0], R[1], mean, std, shape);

821513c8   David Mayerich   ERROR plane wave ...
286
287
288
289
290
291
292
293
294
295
296
  	}*/

  

  

  

  };

  

  

  }	//end namespace rts

  

  

  #endif