Mercurial > hg > Members > yuuhi > OpenCL
annotate fft_fixstart/main.cc @ 9:ed3d4a769bf3
add divice type all
author | Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 05 Feb 2013 17:21:18 +0900 |
parents | 1b8da19bb31c |
children | e38bef2012bc |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <math.h> | |
4 #include <sys/stat.h> | |
5 #include <fcntl.h> | |
4
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
6 #include <sys/time.h> |
3 | 7 |
8 #ifdef __APPLE__ | |
9 #include <OpenCL/opencl.h> | |
10 #else | |
11 #include <CL/cl.h> | |
12 #endif | |
13 | |
14 #include "pgm.h" | |
15 | |
16 #define PI 3.14159265358979 | |
17 | |
18 #define MAX_SOURCE_SIZE (0x100000) | |
19 | |
20 #define AMP(a, b) (sqrt((a)*(a)+(b)*(b))) | |
21 | |
4
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
22 static double st_time; |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
23 static double ed_time; |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
24 |
3 | 25 cl_device_id device_id = NULL; |
26 cl_context context = NULL; | |
27 cl_command_queue queue = NULL; | |
28 cl_program program = NULL; | |
29 cl_device_type device_type = CL_DEVICE_TYPE_GPU; | |
30 | |
31 enum Mode { | |
32 forward = 0, | |
33 inverse = 1 | |
34 }; | |
35 | |
4
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
36 static double |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
37 getTime() |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
38 { |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
39 struct timeval tv; |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
40 gettimeofday(&tv, NULL); |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
41 return tv.tv_sec + (double)tv.tv_usec*1e-6; |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
42 } |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
43 |
3 | 44 int setWorkSize(size_t* gws, size_t* lws, cl_int x, cl_int y) |
45 { | |
46 switch(y) { | |
47 case 1: | |
48 gws[0] = x; | |
49 gws[1] = 1; | |
50 lws[0] = 1; | |
51 lws[1] = 1; | |
52 break; | |
53 default: | |
54 gws[0] = x; | |
55 gws[1] = y; | |
56 lws[0] = 1; | |
57 lws[1] = 1; | |
58 break; | |
59 } | |
60 | |
61 return 0; | |
62 } | |
63 | |
64 int fftCore(cl_mem dst, cl_mem src, cl_mem spin, cl_int m, enum Mode direction) | |
65 { | |
66 cl_int ret; | |
67 | |
68 cl_int iter; | |
69 cl_uint flag; | |
70 | |
71 cl_int n = 1<<m; | |
72 | |
73 cl_event kernelDone; | |
74 | |
75 cl_kernel brev = NULL; | |
76 cl_kernel bfly = NULL; | |
77 cl_kernel norm = NULL; | |
78 | |
79 brev = clCreateKernel(program, "bitReverse", &ret); | |
80 bfly = clCreateKernel(program, "butterfly", &ret); | |
81 norm = clCreateKernel(program, "norm", &ret); | |
82 | |
83 size_t gws[2]; | |
84 size_t lws[2]; | |
85 | |
86 switch (direction) { | |
87 case forward:flag = 0x00000000; break; | |
88 case inverse:flag = 0x80000000; break; | |
89 } | |
90 | |
91 ret = clSetKernelArg(brev, 0, sizeof(cl_mem), (void *)&dst); | |
92 ret = clSetKernelArg(brev, 1, sizeof(cl_mem), (void *)&src); | |
93 ret = clSetKernelArg(brev, 2, sizeof(cl_int), (void *)&m); | |
94 ret = clSetKernelArg(brev, 3, sizeof(cl_int), (void *)&n); | |
95 | |
96 ret = clSetKernelArg(bfly, 0, sizeof(cl_mem), (void *)&dst); | |
97 ret = clSetKernelArg(bfly, 1, sizeof(cl_mem), (void *)&spin); | |
98 ret = clSetKernelArg(bfly, 2, sizeof(cl_int), (void *)&m); | |
99 ret = clSetKernelArg(bfly, 3, sizeof(cl_int), (void *)&n); | |
100 ret = clSetKernelArg(bfly, 5, sizeof(cl_uint), (void *)&flag); | |
101 | |
102 ret = clSetKernelArg(norm, 0, sizeof(cl_mem), (void *)&dst); | |
103 ret = clSetKernelArg(norm, 1, sizeof(cl_int), (void *)&n); | |
104 | |
105 /* Reverse bit ordering */ | |
106 setWorkSize(gws, lws, n, n); | |
5 | 107 ret = clEnqueueTask(queue, brev, 0, NULL, NULL); |
3 | 108 |
109 /* Perform Butterfly Operations*/ | |
110 setWorkSize(gws, lws, n/2, n); | |
111 for (iter=1; iter <= m; iter++) { | |
112 ret = clSetKernelArg(bfly, 4, sizeof(cl_int), (void *)&iter); | |
5 | 113 ret = clEnqueueTask(queue, bfly, 0, NULL, NULL); |
3 | 114 ret = clWaitForEvents(1, &kernelDone); |
115 } | |
116 | |
117 if (direction == inverse) { | |
118 setWorkSize(gws, lws, n, n); | |
5 | 119 ret = clEnqueueTask(queue, norm, 0, NULL, NULL); |
3 | 120 ret = clWaitForEvents(1, &kernelDone); |
121 } | |
122 | |
123 ret = clReleaseKernel(bfly); | |
124 ret = clReleaseKernel(brev); | |
125 ret = clReleaseKernel(norm); | |
126 | |
127 return 0; | |
128 } | |
129 | |
130 char * | |
131 init(int argc, char**argv){ | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
132 |
3 | 133 char *filename = 0; |
134 | |
135 for (int i = 1; argv[i]; ++i) { | |
136 if (strcmp(argv[i], "-file") == 0) { | |
137 filename = argv[i+1]; | |
138 } else if (strcmp(argv[i], "-cpu") == 0) { | |
139 device_type = CL_DEVICE_TYPE_CPU; | |
140 } else if (strcmp(argv[i], "-gpu") == 0) { | |
141 device_type = CL_DEVICE_TYPE_GPU; | |
9 | 142 } else if (strcmp(argv[i], "-all") == 0) { |
143 device_type = CL_DEVICE_TYPE_ALL; | |
3 | 144 } |
145 } | |
146 if ( (argc == 1)||(filename==0)) { | |
6 | 147 printf("Usage: ./fft -file [image filename] -cpu or -gpu \n"); |
3 | 148 exit(-1); |
149 } | |
150 | |
151 return filename; | |
152 } | |
153 | |
154 int main(int argc, char** argv) { | |
155 cl_mem xmobj = NULL; | |
156 cl_mem rmobj = NULL; | |
157 cl_mem wmobj = NULL; | |
158 cl_kernel sfac = NULL; | |
159 cl_kernel trns = NULL; | |
160 cl_kernel hpfl = NULL; | |
161 | |
162 cl_platform_id platform_id = NULL; | |
163 | |
164 cl_uint ret_num_devices; | |
165 cl_uint ret_num_platforms; | |
166 | |
167 cl_int ret; | |
168 | |
169 cl_float2 *xm; | |
170 cl_float2 *rm; | |
171 cl_float2 *wm; | |
172 | |
173 pgm_t ipgm; | |
174 pgm_t opgm; | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
175 |
3 | 176 const char fileName[] = "./fft.cl"; |
177 size_t source_size; | |
178 char *source_str; | |
179 cl_int i, j; | |
180 cl_int n; | |
181 cl_int m; | |
182 | |
183 size_t gws[2]; | |
184 size_t lws[2]; | |
185 | |
186 /* Load kernel source code */ | |
187 int fd = open(fileName, O_RDONLY); | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
188 |
3 | 189 if (fd<0) { |
190 fprintf(stderr, "Failed to load kernel %s.\n",fileName); | |
191 exit(1); | |
192 } | |
193 struct stat stats; | |
194 fstat(fd, &stats); | |
195 off_t size = stats.st_size; | |
196 if (size<=0) { | |
197 fprintf(stderr, "Failed to load kernel.\n"); | |
198 exit(1); | |
199 } | |
200 source_str = (char*)alloca(size); | |
201 source_size = read(fd, source_str, size); | |
202 close( fd ); | |
203 | |
204 char * pgm_file = init(argc,argv); | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
205 |
3 | 206 /* Read image */ |
207 int err = readPGM(&ipgm, pgm_file); | |
208 if (err<0) { | |
209 fprintf(stderr, "Failed to read image file.\n"); | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
210 exit(1); |
3 | 211 } |
212 | |
213 n = ipgm.width; | |
214 m = (cl_int)(log((double)n)/log(2.0)); | |
215 | |
216 xm = (cl_float2 *)malloc(n * n * sizeof(cl_float2)); | |
217 rm = (cl_float2 *)malloc(n * n * sizeof(cl_float2)); | |
218 wm = (cl_float2 *)malloc(n / 2 * sizeof(cl_float2)); | |
219 | |
220 for (i=0; i < n; i++) { | |
221 for (j=0; j < n; j++) { | |
222 ((float*)xm)[(2*n*j)+2*i+0] = (float)ipgm.buf[n*j+i]; | |
223 ((float*)xm)[(2*n*j)+2*i+1] = (float)0; | |
224 } | |
225 } | |
226 | |
227 /* Get platform/device */ | |
228 ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms); | |
5 | 229 |
230 ret = clGetDeviceIDs( platform_id, device_type, 1, &device_id, &ret_num_devices); | |
3 | 231 |
232 /* Create OpenCL context */ | |
233 context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret); | |
234 | |
235 /* Create Command queue */ | |
236 queue = clCreateCommandQueue(context, device_id, 0, &ret); | |
237 | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
238 st_time = getTime(); |
3 | 239 /* Create Buffer Objects */ |
240 xmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, n*n*sizeof(cl_float2), NULL, &ret); | |
241 rmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, n*n*sizeof(cl_float2), NULL, &ret); | |
242 wmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, (n/2)*sizeof(cl_float2), NULL, &ret); | |
243 | |
244 /* Transfer data to memory buffer */ | |
245 ret = clEnqueueWriteBuffer(queue, xmobj, CL_TRUE, 0, n*n*sizeof(cl_float2), xm, 0, NULL, NULL); | |
246 | |
247 /* Create kernel program from source */ | |
248 program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret); | |
249 | |
250 /* Build kernel program */ | |
251 ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL); | |
252 | |
253 if (ret<0) { | |
254 size_t size; | |
255 clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &size); | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
256 |
3 | 257 char *log = new char[size]; |
258 clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, size, log, NULL); | |
259 printf("%s ",log); | |
260 exit (ret); | |
261 } | |
262 | |
263 /* Create OpenCL Kernel */ | |
264 sfac = clCreateKernel(program, "spinFact", &ret); | |
265 trns = clCreateKernel(program, "transpose", &ret); | |
266 hpfl = clCreateKernel(program, "highPassFilter", &ret); | |
267 | |
268 /* Create spin factor */ | |
269 ret = clSetKernelArg(sfac, 0, sizeof(cl_mem), (void *)&wmobj); | |
270 ret = clSetKernelArg(sfac, 1, sizeof(cl_int), (void *)&n); | |
271 setWorkSize(gws, lws, n/2, 1); | |
5 | 272 ret = clEnqueueTask(queue, sfac, 0, NULL, NULL); |
3 | 273 |
274 /* Butterfly Operation */ | |
275 fftCore(rmobj, xmobj, wmobj, m, forward); | |
276 | |
277 /* Transpose matrix */ | |
278 ret = clSetKernelArg(trns, 0, sizeof(cl_mem), (void *)&xmobj); | |
279 ret = clSetKernelArg(trns, 1, sizeof(cl_mem), (void *)&rmobj); | |
280 ret = clSetKernelArg(trns, 2, sizeof(cl_int), (void *)&n); | |
281 setWorkSize(gws, lws, n, n); | |
5 | 282 ret = clEnqueueTask(queue, trns, 0, NULL, NULL); |
3 | 283 |
284 /* Butterfly Operation */ | |
285 fftCore(rmobj, xmobj, wmobj, m, forward); | |
286 | |
287 /* Apply high-pass filter */ | |
288 cl_int radius = n/8; | |
289 ret = clSetKernelArg(hpfl, 0, sizeof(cl_mem), (void *)&rmobj); | |
290 ret = clSetKernelArg(hpfl, 1, sizeof(cl_int), (void *)&n); | |
291 ret = clSetKernelArg(hpfl, 2, sizeof(cl_int), (void *)&radius); | |
292 setWorkSize(gws, lws, n, n); | |
5 | 293 ret = clEnqueueTask(queue, hpfl, 0, NULL, NULL); |
3 | 294 |
295 /* Inverse FFT */ | |
296 | |
297 /* Butterfly Operation */ | |
298 fftCore(xmobj, rmobj, wmobj, m, inverse); | |
299 | |
300 /* Transpose matrix */ | |
301 ret = clSetKernelArg(trns, 0, sizeof(cl_mem), (void *)&rmobj); | |
302 ret = clSetKernelArg(trns, 1, sizeof(cl_mem), (void *)&xmobj); | |
303 setWorkSize(gws, lws, n, n); | |
5 | 304 ret = clEnqueueTask(queue, trns, 0, NULL, NULL); |
3 | 305 |
306 /* Butterfly Operation */ | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
307 |
3 | 308 fftCore(xmobj, rmobj, wmobj, m, inverse); |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
309 |
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
310 /* Read data from memory buffer */ |
3 | 311 ret = clEnqueueReadBuffer(queue, xmobj, CL_TRUE, 0, n*n*sizeof(cl_float2), xm, 0, NULL, NULL); |
312 | |
313 /* */ | |
314 float* ampd; | |
315 ampd = (float*)malloc(n*n*sizeof(float)); | |
316 for (i=0; i < n; i++) { | |
317 for (j=0; j < n; j++) { | |
318 ampd[n*((i))+((j))] = (AMP(((float*)xm)[(2*n*i)+2*j], ((float*)xm)[(2*n*i)+2*j+1])); | |
319 } | |
320 } | |
321 opgm.width = n; | |
322 opgm.height = n; | |
323 normalizeF2PGM(&opgm, ampd); | |
324 free(ampd); | |
8
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
325 ret = clFlush(queue); |
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
326 ret = clFinish(queue); |
1b8da19bb31c
change time measure point
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
327 ed_time = getTime(); |
3 | 328 |
329 /* Write out image */ | |
330 writePGM(&opgm, "output.pgm"); | |
331 | |
332 /* Finalizations*/ | |
333 ret = clFlush(queue); | |
334 ret = clFinish(queue); | |
335 ret = clReleaseKernel(hpfl); | |
336 ret = clReleaseKernel(trns); | |
337 ret = clReleaseKernel(sfac); | |
338 ret = clReleaseProgram(program); | |
339 ret = clReleaseMemObject(xmobj); | |
340 ret = clReleaseMemObject(rmobj); | |
341 ret = clReleaseMemObject(wmobj); | |
342 ret = clReleaseCommandQueue(queue); | |
343 ret = clReleaseContext(context); | |
344 | |
345 destroyPGM(&ipgm); | |
346 destroyPGM(&opgm); | |
347 | |
348 free(wm); | |
349 free(rm); | |
350 free(xm); | |
351 | |
4
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
352 fprintf(stdout, "image out put succeeded.\n"); |
8df0d3128672
add time measurement function
Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
353 printf("Time: %0.6f\n",ed_time-st_time); |
3 | 354 return 0; |
355 } |