12 #if defined(__PW_FPGA)
18 #define _USE_MATH_DEFINES
25 #include "CL/opencl.h"
28 static void tolowercase(
char *
p,
char *
q);
29 static int getFolderPath(
char **path,
int N[3]);
30 static size_t loadBinary(
char *binary_path,
char **buf);
41 cl_platform_id findPlatform(
char *platform_name) {
46 cl_uint num_platforms;
47 status = clGetPlatformIDs(0, NULL, &num_platforms);
48 if (status != CL_SUCCESS) {
49 printf(
"Query for number of platforms failed\n");
54 cl_platform_id *pids =
55 (cl_platform_id *)malloc(
sizeof(cl_platform_id) * num_platforms);
56 status = clGetPlatformIDs(num_platforms, pids, NULL);
57 if (status != CL_SUCCESS) {
58 printf(
"Query for platform ids failed\n");
64 size_t pl_len = strlen(platform_name);
65 char name_search[pl_len + 1];
66 tolowercase(platform_name, name_search);
70 for (
i = 0;
i < num_platforms;
i++) {
72 status = clGetPlatformInfo(pids[
i], CL_PLATFORM_NAME, 0, NULL, &sz);
73 if (status != CL_SUCCESS) {
74 printf(
"Query for platform info failed\n");
83 status = clGetPlatformInfo(pids[
i], CL_PLATFORM_NAME, sz, pl_name, NULL);
84 if (status != CL_SUCCESS) {
85 printf(
"Query for platform info failed\n");
90 tolowercase(pl_name, plat_name);
91 if (strstr(plat_name, name_search)) {
92 cl_platform_id pid = pids[
i];
108 cl_device_id *getDevices(cl_platform_id pid, cl_device_type device_type,
109 cl_uint *num_devices) {
113 status = clGetDeviceIDs(pid, device_type, 0, NULL, num_devices);
114 if (status != CL_SUCCESS) {
115 printf(
"Query for number of devices failed\n");
120 cl_device_id *dev_ids =
121 (cl_device_id *)malloc(
sizeof(cl_device_id) * (*num_devices));
122 status = clGetDeviceIDs(pid, device_type, *num_devices, dev_ids, NULL);
123 if (status != CL_SUCCESS) {
124 printf(
"Query for device ids failed\n");
131 static int fileExists(
char *filename) {
132 if (access(filename, R_OK) != -1) {
147 cl_program getProgramWithBinary(cl_context context,
const cl_device_id *devices,
148 unsigned num_device,
int N[3],
150 const int len_bin_path = 1000;
151 char bin_path[len_bin_path];
153 char *binaries[num_device];
156 cl_int bin_status, status;
159 const char *subpath =
"/../fpgabitstream/fft3d/synthesis_sp/";
161 const char *subpath =
"/../fpgabitstream/fft3d/synthesis_dp/";
164 const char *filename =
"fft3d.aocx";
166 if (!getFolderPath(&foldername, N)) {
167 printf(
"Path not found for the size (%d,%d,%d)", N[0], N[1], N[2]);
171 int num_bytes = snprintf(bin_path, len_bin_path,
"%s%s%s%s", data_path,
172 subpath, foldername, filename);
173 if (num_bytes > len_bin_path) {
174 printf(
"Insufficient buffer size to store path to binary\n");
179 if (!fileExists(bin_path)) {
180 printf(
"File not found in path %s\n", bin_path);
186 bin_size = loadBinary(bin_path, &binary);
188 printf(
"Could not load binary\n");
193 binaries[0] = binary;
196 cl_program program = clCreateProgramWithBinary(
197 context, 1, devices, &bin_size, (
const unsigned char **)binaries,
198 &bin_status, &status);
199 if (status != CL_SUCCESS) {
200 printf(
"Query to create program with binary failed\n");
210 int getFolderPath(
char **folderPath,
int N[3]) {
211 if (N[0] == 16 && N[1] == 16 && N[2] == 16) {
212 char folder[] =
"syn16/";
213 *folderPath = malloc(strlen(folder) + 1);
214 strncpy(*folderPath, folder, strlen(folder));
215 }
else if (N[0] == 32 && N[1] == 32 && N[2] == 32) {
216 char folder[] =
"syn32/";
217 *folderPath = malloc(strlen(folder) + 1);
218 strncpy(*folderPath, folder, strlen(folder));
219 }
else if (N[0] == 64 && N[1] == 64 && N[2] == 64) {
220 char folder[] =
"syn64/";
221 *folderPath = malloc(strlen(folder) + 1);
222 strncpy(*folderPath, folder, strlen(folder));
229 static size_t loadBinary(
char *binary_path,
char **buf) {
234 fp = fopen(binary_path,
"rb");
240 fseek(fp, 0L, SEEK_END);
241 size_t bin_size = ftell(fp);
243 *buf = (
char *)malloc(bin_size);
246 if (fread((
void *)*buf, bin_size, 1, fp) == 0) {
262 const unsigned OPENCL_ALIGNMENT = 64;
263 void *alignedMalloc(
size_t size) {
265 int ret = posix_memalign(&memptr, OPENCL_ALIGNMENT, size);
272 void openCLContextCallBackFxn(
const char *errinfo,
const void *private_info,
273 size_t cb,
void *user_data) {
274 printf(
"Context Callback - %s\n", errinfo);
277 void printError(cl_int error) {
280 case CL_INVALID_PLATFORM:
281 printf(
"CL_PLATFORM NOT FOUND OR INVALID ");
283 case CL_INVALID_DEVICE:
284 printf(
"CL_DEVICE NOT FOUND OR INVALID OR DOESN'T MATCH THE PLATFORM ");
286 case CL_INVALID_CONTEXT:
287 printf(
"CL_CONTEXT INVALID ");
289 case CL_OUT_OF_HOST_MEMORY:
290 printf(
"FAILURE TO ALLOCATE RESOURCES BY OPENCL");
292 case CL_DEVICE_NOT_AVAILABLE:
293 printf(
"CL_DEVICE NOT AVAILABLE ALTHOUGH FOUND");
295 case CL_INVALID_QUEUE_PROPERTIES:
296 printf(
"CL_QUEUE PROPERTIES INVALID");
298 case CL_INVALID_PROGRAM:
299 printf(
"CL_PROGRAM INVALID");
301 case CL_INVALID_BINARY:
302 printf(
"CL_BINARY INVALID");
304 case CL_INVALID_KERNEL_NAME:
305 printf(
"CL_KERNEL_NAME INVALID");
307 case CL_INVALID_KERNEL_DEFINITION:
308 printf(
"CL_KERNEL_DEFN INVALID");
310 case CL_INVALID_VALUE:
311 printf(
"CL_VALUE INVALID");
313 case CL_INVALID_BUFFER_SIZE:
314 printf(
"CL_BUFFER_SIZE INVALID");
316 case CL_INVALID_HOST_PTR:
317 printf(
"CL_HOST_PTR INVALID");
319 case CL_INVALID_COMMAND_QUEUE:
320 printf(
"CL_COMMAND_QUEUE INVALID");
322 case CL_INVALID_MEM_OBJECT:
323 printf(
"CL_MEM_OBJECT INVALID");
325 case CL_MEM_OBJECT_ALLOCATION_FAILURE:
326 printf(
"CL_MEM_OBJECT_ALLOCATION INVALID");
328 case CL_INVALID_ARG_INDEX:
329 printf(
"CL_ARG_INDEX INVALID");
331 case CL_INVALID_ARG_VALUE:
332 printf(
"CL_ARG_VALUE INVALID");
334 case CL_INVALID_ARG_SIZE:
335 printf(
"CL_ARG_SIZE INVALID");
337 case CL_INVALID_PROGRAM_EXECUTABLE:
338 printf(
"CL_PROGRAM_EXEC INVALID");
340 case CL_INVALID_KERNEL:
341 printf(
"CL_KERNEL INVALID");
343 case CL_INVALID_KERNEL_ARGS:
344 printf(
"CL_KERNEL_ARG INVALID");
346 case CL_INVALID_WORK_GROUP_SIZE:
347 printf(
"CL_WORK_GROUP_SIZE INVALID");
351 printf(
"UNKNOWN ERROR %d\n", error);
355 void _checkError(
const char *file,
int line,
const char *func, cl_int err,
356 const char *msg, ...) {
358 if (err != CL_SUCCESS) {
361 printf(
"\nError Location: %s:%d:%s\n", file, line, func);
381 static void tolowercase(
char *
p,
char *
q) {
384 for (
i = 0;
i < strlen(
p);
i++) {
static void const int const int i