/
main.cu
248 lines (182 loc) · 8.47 KB
/
main.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*******************************************************************
c* Multimodal Deformable Image Registration *
c* via Mutual Information or Bhattacharyya Distantce *
c* Version: 1.0 *
c* Language: C, CUDA *
c* *
c* Developer: Yifei Lou *
c* Email: yifei.lou@ece.gatech.edu *
c* *
c* School of Electrical and Computer Engineering *
c* Georgia Institute of Technology *
c* Atlanta, GA, 30318 *
c* Website: http://groups.bme.gatech.edu/groups/bil/ *
c* *
c* Copyright (c) 2011 *
c* All rights reserved. *
c* *
c* Permission to use, copy, or modify this code and its *
c* documentation for scientific purpose is hereby granted *
c* without fee, provided that this copyright notice appear in *
c* all copies and that both that copyright notice and this *
c* permission notice appear in supporting documentation. The use *
c* for commercial purposes is prohibited without permission. *
c* *
c* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
c* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
c* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
c* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
c* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR *
c* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
c* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT *
c* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF*
c* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
c* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
c* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING *
c* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF *
c* THE POSSIBILITY OF SUCH DAMAGE. *
c* *
c******************************************************************/
/*******************************************************************
c* Short discription *
c* main code of the multi-modal deformable registration *
c* it calls all the other components *
c******************************************************************/
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
// includes, gloable variables
#include "global.h"
#include "convolution.cu"
// includes, project
#include <cutil_inline.h>
#include <cublas.h>
#include <thrust/device_ptr.h>
#include <thrust/reduce.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/binary_search.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/random.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <cuda.h> // for float2
using namespace std;
using namespace thrust;
// include files
#include "initialize.cu"
#include "funcHistogram.cu"
#include "funcImageDomain.cu"
#include "compute.cu"
#include "finalize.cu"
/****************************************************
main program
****************************************************/
int main( int argc, char** argv)
{
cout << endl << "****************************************" << endl;
cout << "Computation parameters..." << endl;
cout << "****************************************" << endl ;
int device = DEVICENUMBER;
// device number
cudaSetDevice(device);
cout << "Using device # " << device << endl;
// choose which device to use
cudaGetDeviceCount(&deviceCount);
cudaGetDeviceProperties(&dP,device);
cout<<"Max threads per block: "<<dP.maxThreadsPerBlock<<endl;
cout<<"Max Threads DIM: "<<dP.maxThreadsDim[0]<<" x "<<dP.maxThreadsDim[1]<<" x "<<dP.maxThreadsDim[2]<<endl;
cout<<"Max Grid Size: "<<dP.maxGridSize[0]<<" x "<<dP.maxGridSize[1]<<" x "<<dP.maxGridSize[2]<<endl;
printf("Device %d: \"%s\" with Compute %d.%d capability\n",
device, dP.name, dP.major, dP.minor);
// obtain computing resource
nblocks_hist.x = NBLOCKX;
nblocks_hist.y = ((1 + (nBin*nBin - 1)/NTHREAD_PER_BLOCK) - 1) / NBLOCKX + 1;
cout << endl << "****************************************" << endl;
cout << "Computing starts..." << endl;
cout << "****************************************" << endl << endl;
// mark the start total time timer
unsigned int totalTimer = 0;
cutilCheckError( cutCreateTimer( &totalTimer));
cutilCheckError( cutStartTimer( totalTimer));
/******************************************************
initialize
******************************************************/
cout << "\n\n";
cout << "Initializing MI 3Dreg program...\n\n";
////// CBLAS initialization ////////////////////////////
cout << "Initializing CUBLAS..." << endl;
cublasStatus status = cublasInit();
if (status != CUBLAS_STATUS_SUCCESS)
{
fprintf (stderr, "!!!! CUBLAS initialization error\n");
getchar();
exit(0);
}
// initialize CUBLAS
initData();
initGaussKernel();
/******************************************************
start iterations
******************************************************/
unsigned int timer = 0;
cutilCheckError( cutCreateTimer( &timer));
cutilCheckError( cutStartTimer( timer));
// mark the start time
cout << "\n\n";
cout << "Performing registration...\n\n";
for(int scale = NSCALE-1; scale >=0; scale--)
{
NX = NX0/pow(2, scale);
NY = NY0/pow(2, scale);
NZ = (NZ0-1)/pow(2, scale) +1;
sDATA_SIZE = (NX*NY*NZ)* sizeof(float);
nblocks.x = NBLOCKX;
nblocks.y = ((1 + (NX*NY*NZ - 1)/NTHREAD_PER_BLOCK) - 1) / NBLOCKX + 1;
printf("current scale = %d, size of image = %d x %d x %d ... \n", scale, NX, NY, NZ);
if(scale<NSCALE-1)
{
upSample<<<nblocks, NTHREAD_PER_BLOCK>>>(d_mv_x[scale+1], d_mv_x[scale], NX, NY, NZ);
upSample<<<nblocks, NTHREAD_PER_BLOCK>>>(d_mv_y[scale+1], d_mv_y[scale], NX, NY, NZ);
upSample<<<nblocks, NTHREAD_PER_BLOCK>>>(d_mv_z[scale+1], d_mv_z[scale], NX, NY, NZ);
}
compute(d_im_move[scale], d_im_static[scale], d_mv_x[scale], d_mv_y[scale], d_mv_z[scale], MAX_ITER);
printf("\n\n");
}
cudaThreadSynchronize();
cutilCheckError( cutStopTimer( timer));
printf("\n\n****************************************\n");
printf( "Computing time: %f (ms)\n", cutGetTimerValue( timer));
printf("****************************************\n\n\n");
cutilCheckError( cutDeleteTimer( timer));
// mark the end timer and print
/******************************************************
finalize
******************************************************/
printf("Finalizing program...\n\n");
fina();
/**** shut down CBLAS ********/
status = cublasShutdown();
if (status != CUBLAS_STATUS_SUCCESS)
{
fprintf (stderr, "!!!! shutdown error (A)\n");
getchar();
exit(0);
}
// Shut down CUBLAS
cudaThreadSynchronize();
// mark the end total timer
cutilCheckError( cutStopTimer( totalTimer));
printf("\n\n****************************************\n");
printf( "Entire program time: %f (ms)\n", cutGetTimerValue( totalTimer));
printf("****************************************\n\n\n");
cutilCheckError( cutDeleteTimer( totalTimer));
printf("Have a nice day!\n");
cudaThreadExit();
cutilExit(argc, argv);
return 0;
}