bzr branch
http://darksoft.org/webbzr/opencl/oclfft
1
by Matthias Vogelgesang
Initial commit |
1 |
//
|
2 |
// File: fft_setup.cpp
|
|
3 |
//
|
|
4 |
// Version: <1.0>
|
|
5 |
//
|
|
6 |
// Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple")
|
|
7 |
// in consideration of your agreement to the following terms, and your use,
|
|
8 |
// installation, modification or redistribution of this Apple software
|
|
9 |
// constitutes acceptance of these terms. If you do not agree with these
|
|
10 |
// terms, please do not use, install, modify or redistribute this Apple
|
|
11 |
// software.
|
|
12 |
//
|
|
13 |
// In consideration of your agreement to abide by the following terms, and
|
|
14 |
// subject to these terms, Apple grants you a personal, non - exclusive
|
|
15 |
// license, under Apple's copyrights in this original Apple software ( the
|
|
16 |
// "Apple Software" ), to use, reproduce, modify and redistribute the Apple
|
|
17 |
// Software, with or without modifications, in source and / or binary forms;
|
|
18 |
// provided that if you redistribute the Apple Software in its entirety and
|
|
19 |
// without modifications, you must retain this notice and the following text
|
|
20 |
// and disclaimers in all such redistributions of the Apple Software. Neither
|
|
21 |
// the name, trademarks, service marks or logos of Apple Inc. may be used to
|
|
22 |
// endorse or promote products derived from the Apple Software without specific
|
|
23 |
// prior written permission from Apple. Except as expressly stated in this
|
|
24 |
// notice, no other rights or licenses, express or implied, are granted by
|
|
25 |
// Apple herein, including but not limited to any patent rights that may be
|
|
26 |
// infringed by your derivative works or by other works in which the Apple
|
|
27 |
// Software may be incorporated.
|
|
28 |
//
|
|
29 |
// The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
|
|
30 |
// WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
|
|
31 |
// WARRANTIES OF NON - INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
|
|
32 |
// PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION
|
|
33 |
// ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
|
|
34 |
//
|
|
35 |
// IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
|
|
36 |
// CONSEQUENTIAL DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
37 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
38 |
// INTERRUPTION ) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION
|
|
39 |
// AND / OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER
|
|
40 |
// UNDER THEORY OF CONTRACT, TORT ( INCLUDING NEGLIGENCE ), STRICT LIABILITY OR
|
|
41 |
// OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
42 |
//
|
|
43 |
// Copyright ( C ) 2008 Apple Inc. All Rights Reserved.
|
|
44 |
//
|
|
45 |
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
46 |
||
47 |
||
48 |
#include "fft_internal.h" |
|
49 |
#include "fft_base_kernels.h" |
|
50 |
#include <limits.h> |
|
51 |
#include <stdlib.h> |
|
52 |
#include <string.h> |
|
53 |
#include <sys/types.h> |
|
54 |
#include <sys/stat.h> |
|
55 |
#include <iostream> |
|
56 |
#include <string> |
|
57 |
#include <sstream> |
|
58 |
||
59 |
using namespace std; |
|
60 |
||
61 |
extern void getKernelWorkDimensions(cl_fft_plan *plan, cl_fft_kernel_info *kernelInfo, cl_int *batchSize, size_t *gWorkItems, size_t *lWorkItems); |
|
62 |
||
63 |
static void |
|
64 |
getBlockConfigAndKernelString(cl_fft_plan *plan) |
|
65 |
{
|
|
66 |
plan->temp_buffer_needed = 0; |
|
67 |
*plan->kernel_string += baseKernels; |
|
68 |
||
69 |
if(plan->format == clFFT_SplitComplexFormat) |
|
70 |
*plan->kernel_string += twistKernelPlannar; |
|
71 |
else
|
|
72 |
*plan->kernel_string += twistKernelInterleaved; |
|
73 |
||
74 |
switch(plan->dim) |
|
75 |
{
|
|
76 |
case clFFT_1D: |
|
77 |
FFT1D(plan, cl_fft_kernel_x); |
|
78 |
break; |
|
79 |
||
80 |
case clFFT_2D: |
|
81 |
FFT1D(plan, cl_fft_kernel_x); |
|
82 |
FFT1D(plan, cl_fft_kernel_y); |
|
83 |
break; |
|
84 |
||
85 |
case clFFT_3D: |
|
86 |
FFT1D(plan, cl_fft_kernel_x); |
|
87 |
FFT1D(plan, cl_fft_kernel_y); |
|
88 |
FFT1D(plan, cl_fft_kernel_z); |
|
89 |
break; |
|
90 |
||
91 |
default: |
|
92 |
return; |
|
93 |
}
|
|
94 |
||
95 |
plan->temp_buffer_needed = 0; |
|
96 |
cl_fft_kernel_info *kInfo = plan->kernel_info; |
|
97 |
while(kInfo) |
|
98 |
{
|
|
99 |
plan->temp_buffer_needed |= !kInfo->in_place_possible; |
|
100 |
kInfo = kInfo->next; |
|
101 |
}
|
|
102 |
}
|
|
103 |
||
104 |
||
105 |
static void |
|
106 |
deleteKernelInfo(cl_fft_kernel_info *kInfo) |
|
107 |
{
|
|
108 |
if(kInfo) |
|
109 |
{
|
|
110 |
if(kInfo->kernel_name) |
|
111 |
free(kInfo->kernel_name); |
|
112 |
if(kInfo->kernel) |
|
113 |
clReleaseKernel(kInfo->kernel); |
|
114 |
free(kInfo); |
|
115 |
}
|
|
116 |
}
|
|
117 |
||
118 |
static void |
|
119 |
destroy_plan(cl_fft_plan *Plan) |
|
120 |
{
|
|
121 |
cl_fft_kernel_info *kernel_info = Plan->kernel_info; |
|
122 |
||
123 |
while(kernel_info) |
|
124 |
{
|
|
125 |
cl_fft_kernel_info *tmp = kernel_info->next; |
|
126 |
deleteKernelInfo(kernel_info); |
|
127 |
kernel_info = tmp; |
|
128 |
}
|
|
129 |
||
130 |
Plan->kernel_info = NULL; |
|
131 |
||
132 |
if(Plan->kernel_string) |
|
133 |
{
|
|
134 |
delete Plan->kernel_string; |
|
135 |
Plan->kernel_string = NULL; |
|
136 |
}
|
|
137 |
if(Plan->twist_kernel) |
|
138 |
{
|
|
139 |
clReleaseKernel(Plan->twist_kernel); |
|
140 |
Plan->twist_kernel = NULL; |
|
141 |
}
|
|
142 |
if(Plan->program) |
|
143 |
{
|
|
144 |
clReleaseProgram(Plan->program); |
|
145 |
Plan->program = NULL; |
|
146 |
}
|
|
147 |
if(Plan->tempmemobj) |
|
148 |
{
|
|
149 |
clReleaseMemObject(Plan->tempmemobj); |
|
150 |
Plan->tempmemobj = NULL; |
|
151 |
}
|
|
152 |
if(Plan->tempmemobj_real) |
|
153 |
{
|
|
154 |
clReleaseMemObject(Plan->tempmemobj_real); |
|
155 |
Plan->tempmemobj_real = NULL; |
|
156 |
}
|
|
157 |
if(Plan->tempmemobj_imag) |
|
158 |
{
|
|
159 |
clReleaseMemObject(Plan->tempmemobj_imag); |
|
160 |
Plan->tempmemobj_imag = NULL; |
|
161 |
}
|
|
162 |
}
|
|
163 |
||
164 |
static int |
|
165 |
createKernelList(cl_fft_plan *plan) |
|
166 |
{
|
|
167 |
cl_program program = plan->program; |
|
168 |
cl_fft_kernel_info *kernel_info = plan->kernel_info; |
|
169 |
||
170 |
cl_int err; |
|
171 |
while(kernel_info) |
|
172 |
{
|
|
173 |
kernel_info->kernel = clCreateKernel(program, kernel_info->kernel_name, &err); |
|
174 |
if(!kernel_info->kernel || err != CL_SUCCESS) |
|
175 |
return err; |
|
176 |
kernel_info = kernel_info->next; |
|
177 |
}
|
|
178 |
||
179 |
if(plan->format == clFFT_SplitComplexFormat) |
|
180 |
plan->twist_kernel = clCreateKernel(program, "clFFT_1DTwistSplit", &err); |
|
181 |
else
|
|
182 |
plan->twist_kernel = clCreateKernel(program, "clFFT_1DTwistInterleaved", &err); |
|
183 |
||
184 |
if(!plan->twist_kernel || err) |
|
185 |
return err; |
|
186 |
||
187 |
return CL_SUCCESS; |
|
188 |
}
|
|
189 |
||
190 |
int getMaxKernelWorkGroupSize(cl_fft_plan *plan, unsigned int *max_wg_size, unsigned int num_devices, cl_device_id *devices) |
|
191 |
{
|
|
192 |
int reg_needed = 0; |
|
193 |
*max_wg_size = INT_MAX; |
|
194 |
int err; |
|
195 |
size_t wg_size; |
|
196 |
||
197 |
unsigned int i; |
|
198 |
for(i = 0; i < num_devices; i++) |
|
199 |
{
|
|
200 |
cl_fft_kernel_info *kInfo = plan->kernel_info; |
|
201 |
while(kInfo) |
|
202 |
{
|
|
203 |
err = clGetKernelWorkGroupInfo(kInfo->kernel, devices[i], CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &wg_size, NULL); |
|
204 |
if(err != CL_SUCCESS) |
|
205 |
return -1; |
|
206 |
||
207 |
if(wg_size < kInfo->num_workitems_per_workgroup) |
|
208 |
reg_needed |= 1; |
|
209 |
||
210 |
if(*max_wg_size > wg_size) |
|
211 |
*max_wg_size = wg_size; |
|
212 |
||
213 |
kInfo = kInfo->next; |
|
214 |
}
|
|
215 |
}
|
|
216 |
||
217 |
return reg_needed; |
|
218 |
}
|
|
219 |
||
220 |
#define ERR_MACRO(err) { \
|
|
221 |
if( err != CL_SUCCESS) \
|
|
222 |
{ \
|
|
223 |
if(error_code) \
|
|
224 |
*error_code = err; \
|
|
225 |
clFFT_DestroyPlan((clFFT_Plan) plan); \
|
|
226 |
return (clFFT_Plan) NULL; \
|
|
227 |
} \
|
|
228 |
}
|
|
229 |
||
230 |
clFFT_Plan
|
|
231 |
clFFT_CreatePlan(cl_context context, clFFT_Dim3 n, clFFT_Dimension dim, clFFT_DataFormat dataFormat, cl_int *error_code ) |
|
232 |
{
|
|
233 |
int i; |
|
234 |
cl_int err; |
|
235 |
int isPow2 = 1; |
|
236 |
cl_fft_plan *plan = NULL; |
|
237 |
ostringstream kString; |
|
238 |
int num_devices; |
|
239 |
int gpu_found = 0; |
|
240 |
cl_device_id devices[16]; |
|
241 |
size_t ret_size; |
|
242 |
cl_device_type device_type; |
|
243 |
||
244 |
if(!context) |
|
245 |
ERR_MACRO(CL_INVALID_VALUE); |
|
246 |
||
247 |
isPow2 |= n.x && !( (n.x - 1) & n.x ); |
|
248 |
isPow2 |= n.y && !( (n.y - 1) & n.y ); |
|
249 |
isPow2 |= n.z && !( (n.z - 1) & n.z ); |
|
250 |
||
251 |
if(!isPow2) |
|
252 |
ERR_MACRO(CL_INVALID_VALUE); |
|
253 |
||
254 |
if( (dim == clFFT_1D && (n.y != 1 || n.z != 1)) || (dim == clFFT_2D && n.z != 1) ) |
|
255 |
ERR_MACRO(CL_INVALID_VALUE); |
|
256 |
||
257 |
plan = (cl_fft_plan *) malloc(sizeof(cl_fft_plan)); |
|
258 |
if(!plan) |
|
259 |
ERR_MACRO(CL_OUT_OF_RESOURCES); |
|
260 |
||
261 |
plan->context = context; |
|
262 |
clRetainContext(context); |
|
263 |
plan->n = n; |
|
264 |
plan->dim = dim; |
|
265 |
plan->format = dataFormat; |
|
266 |
plan->kernel_info = 0; |
|
267 |
plan->num_kernels = 0; |
|
268 |
plan->twist_kernel = 0; |
|
269 |
plan->program = 0; |
|
270 |
plan->temp_buffer_needed = 0; |
|
271 |
plan->last_batch_size = 0; |
|
272 |
plan->tempmemobj = 0; |
|
273 |
plan->tempmemobj_real = 0; |
|
274 |
plan->tempmemobj_imag = 0; |
|
275 |
plan->max_localmem_fft_size = 2048; |
|
276 |
plan->max_work_item_per_workgroup = 256; |
|
277 |
plan->max_radix = 16; |
|
278 |
plan->min_mem_coalesce_width = 16; |
|
279 |
plan->num_local_mem_banks = 16; |
|
280 |
||
281 |
patch_kernel_source: |
|
282 |
||
283 |
plan->kernel_string = new string(""); |
|
284 |
if(!plan->kernel_string) |
|
285 |
ERR_MACRO(CL_OUT_OF_RESOURCES); |
|
286 |
||
287 |
getBlockConfigAndKernelString(plan); |
|
288 |
||
289 |
const char *source_str = plan->kernel_string->c_str(); |
|
290 |
plan->program = clCreateProgramWithSource(context, 1, (const char**) &source_str, NULL, &err); |
|
291 |
ERR_MACRO(err); |
|
292 |
||
293 |
err = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(devices), devices, &ret_size); |
|
294 |
ERR_MACRO(err); |
|
295 |
||
296 |
num_devices = ret_size / sizeof(cl_device_id); |
|
297 |
||
7
by Suren A. Chilingaryan
Merge mv fix for multigpu contexts on AMD platform |
298 |
err = clBuildProgram(plan->program, num_devices, devices, "-cl-mad-enable", NULL, NULL); |
299 |
if (err != CL_SUCCESS) |
|
300 |
{
|
|
301 |
char *build_log; |
|
302 |
char devicename[200]; |
|
303 |
size_t log_size; |
|
304 |
||
305 |
err = clGetProgramBuildInfo(plan->program, devices[i], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size); |
|
306 |
ERR_MACRO(err); |
|
307 |
||
308 |
build_log = (char *) malloc(log_size + 1); |
|
309 |
||
310 |
err = clGetProgramBuildInfo(plan->program, devices[i], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL); |
|
311 |
ERR_MACRO(err); |
|
312 |
||
313 |
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(devicename), devicename, NULL); |
|
314 |
ERR_MACRO(err); |
|
315 |
||
316 |
fprintf(stdout, "FFT program build log on device %s\n", devicename); |
|
317 |
fprintf(stdout, "%s\n", build_log); |
|
318 |
free(build_log); |
|
319 |
||
320 |
ERR_MACRO(err); |
|
321 |
}
|
|
322 |
||
1
by Matthias Vogelgesang
Initial commit |
323 |
err = createKernelList(plan); |
324 |
ERR_MACRO(err); |
|
325 |
||
326 |
// we created program and kernels based on "some max work group size (default 256)" ... this work group size
|
|
327 |
// may be larger than what kernel may execute with ... if thats the case we need to regenerate the kernel source
|
|
328 |
// setting this as limit i.e max group size and rebuild.
|
|
329 |
unsigned int max_kernel_wg_size; |
|
330 |
int patching_req = getMaxKernelWorkGroupSize(plan, &max_kernel_wg_size, num_devices, devices); |
|
331 |
if(patching_req == -1) |
|
332 |
{
|
|
333 |
ERR_MACRO(err); |
|
334 |
}
|
|
335 |
||
336 |
if(patching_req) |
|
337 |
{
|
|
338 |
destroy_plan(plan); |
|
339 |
plan->max_work_item_per_workgroup = max_kernel_wg_size; |
|
340 |
goto patch_kernel_source; |
|
341 |
}
|
|
342 |
||
343 |
cl_fft_kernel_info *kInfo = plan->kernel_info; |
|
344 |
while(kInfo) |
|
345 |
{
|
|
346 |
plan->num_kernels++; |
|
347 |
kInfo = kInfo->next; |
|
348 |
}
|
|
349 |
||
350 |
if(error_code) |
|
351 |
*error_code = CL_SUCCESS; |
|
352 |
||
353 |
return (clFFT_Plan) plan; |
|
354 |
}
|
|
355 |
||
356 |
void
|
|
357 |
clFFT_DestroyPlan(clFFT_Plan plan) |
|
358 |
{
|
|
359 |
cl_fft_plan *Plan = (cl_fft_plan *) plan; |
|
360 |
if(Plan) |
|
361 |
{
|
|
362 |
destroy_plan(Plan); |
|
363 |
clReleaseContext(Plan->context); |
|
364 |
free(Plan); |
|
365 |
}
|
|
366 |
}
|
|
367 |
||
368 |
void clFFT_DumpPlan( clFFT_Plan Plan, FILE *file) |
|
369 |
{
|
|
370 |
size_t gDim, lDim; |
|
371 |
FILE *out; |
|
372 |
if(!file) |
|
373 |
out = stdout; |
|
374 |
else
|
|
375 |
out = file; |
|
376 |
||
377 |
cl_fft_plan *plan = (cl_fft_plan *) Plan; |
|
378 |
cl_fft_kernel_info *kInfo = plan->kernel_info; |
|
379 |
||
380 |
while(kInfo) |
|
381 |
{
|
|
382 |
cl_int s = 1; |
|
383 |
getKernelWorkDimensions(plan, kInfo, &s, &gDim, &lDim); |
|
384 |
fprintf(out, "Run kernel %s with global dim = {%zd*BatchSize}, local dim={%zd}\n", kInfo->kernel_name, gDim, lDim); |
|
385 |
kInfo = kInfo->next; |
|
386 |
}
|
|
387 |
fprintf(out, "%s\n", plan->kernel_string->c_str()); |
|
388 |
}
|
|
389 |