#include <string.h>
#include <assert.h>
-#define FATAL(...) \
-do { \
- fprintf(stderr, "error: "); \
- fprintf(stderr, __VA_ARGS__); \
- assert(0); \
- exit(-1); \
+#define FATAL(...) \
+do { \
+ fprintf(stderr, "error: "); \
+ fprintf(stderr, __VA_ARGS__); \
+ assert(0); \
+ exit(-1); \
} while (0)
-#define FATAL_IF(COND, ...) \
-do { \
- if (COND) FATAL(__VA_ARGS__); \
+#define FATAL_IF(COND, ...) \
+do { \
+ if (COND) FATAL(__VA_ARGS__); \
} while (0)
-cl_platform_id platform;
-cl_device_id device;
-cl_context ctx;
-cl_program program;
-cl_kernel kernel;
-cl_command_queue queue;
+cl_platform_id platform = NULL;
+cl_device_id device = NULL;
+cl_context ctx = NULL;
+cl_program program = NULL;
+cl_kernel kernel = NULL;
+cl_command_queue queue = NULL;
+cl_mem buf[MAX_BUFFER_N] = {};
+void *buf_data[MAX_BUFFER_N] = {};
+size_t globals[3] = {};
+size_t locals[3] = {};
static const char*
cl_test_channel_order_string(cl_channel_order order)
}
int
-cl_test_init(const char *file_name, const char *kernel_name, int format)
+cl_kernel_init(const char *file_name, const char *kernel_name, int format)
{
cl_file_map_t *fm = NULL;
- cl_int status = CL_SUCCESS;
char *ker_path = NULL;
+ cl_int status = CL_SUCCESS;
+
+ /* Load the program and build it */
+ ker_path = do_kiss_path(file_name, device);
+ if (format == LLVM)
+ program = clCreateProgramWithLLVM(ctx, 1, &device, ker_path, &status);
+ else if (format == SOURCE) {
+ cl_file_map_t *fm = cl_file_map_new();
+ FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
+ "Failed to open file");
+ const char *src = cl_file_map_begin(fm);
+ const size_t sz = cl_file_map_size(fm);
+ program = clCreateProgramWithSource(ctx, 1, &src, &sz, &status);
+ } else
+ FATAL("Not able to create program from binary");
+
+ if (status != CL_SUCCESS) {
+ fprintf(stderr, "error calling clCreateProgramWithBinary\n");
+ goto error;
+ }
+
+ /* OCL requires to build the program even if it is created from a binary */
+ CALL (clBuildProgram, program, 1, &device, NULL, NULL, NULL);
+
+ /* Create a kernel from the program */
+ kernel = clCreateKernel(program, kernel_name, &status);
+ if (status != CL_SUCCESS) {
+ fprintf(stderr, "error calling clCreateKernel\n");
+ goto error;
+ }
+
+exit:
+ free(ker_path);
+ cl_file_map_delete(fm);
+ return status;
+error:
+ goto exit;
+}
+
+int
+cl_ocl_init(void)
+{
+ cl_int status = CL_SUCCESS;
char name[128];
cl_uint platform_n;
size_t i;
cl_test_channel_order_string(fmt[i].image_channel_order),
cl_test_channel_type_string(fmt[i].image_channel_data_type));
- /* Load the program and build it */
- ker_path = do_kiss_path(file_name, device);
- if (format == LLVM)
- program = clCreateProgramWithLLVM(ctx, 1, &device, ker_path, &status);
- else if (format == SOURCE) {
- cl_file_map_t *fm = cl_file_map_new();
- FATAL_IF (cl_file_map_open(fm, ker_path) != CL_FILE_MAP_SUCCESS,
- "Failed to open file");
- const char *src = cl_file_map_begin(fm);
- const size_t sz = cl_file_map_size(fm);
- program = clCreateProgramWithSource(ctx, 1, &src, &sz, &status);
- } else
- FATAL("Not able to create program from binary");
-
+ /* We are going to push NDRange kernels here */
+ queue = clCreateCommandQueue(ctx, device, 0, &status);
if (status != CL_SUCCESS) {
- fprintf(stderr, "error calling clCreateProgramWithBinary\n");
+ fprintf(stderr, "error calling clCreateCommandQueue\n");
goto error;
}
- /* OCL requires to build the program even if it is created from a binary */
- CALL (clBuildProgram, program, 1, &device, NULL, NULL, NULL);
+error:
+ return status;
+}
- /* Create a kernel from the program */
- kernel = clCreateKernel(program, kernel_name, &status);
- if (status != CL_SUCCESS) {
- fprintf(stderr, "error calling clCreateKernel\n");
+int
+cl_test_init(const char *file_name, const char *kernel_name, int format)
+{
+ cl_int status = CL_SUCCESS;
+
+ /* Initialize OCL */
+ if ((status = cl_ocl_init()) != CL_SUCCESS)
goto error;
- }
- /* We are going to push NDRange kernels here */
- queue = clCreateCommandQueue(ctx, device, 0, &status);
- if (status != CL_SUCCESS) {
- fprintf(stderr, "error calling clCreateCommandQueue\n");
+ /* Load the kernel */
+ if ((status = cl_kernel_init(file_name, kernel_name, format)) != CL_SUCCESS)
goto error;
- }
-exit:
- free(ker_path);
- cl_file_map_delete(fm);
- return status;
error:
- goto exit;
+ return status;
}
void
-cl_test_destroy(void)
+cl_kernel_destroy(void)
{
- clReleaseCommandQueue(queue);
clReleaseKernel(kernel);
clReleaseProgram(program);
+ kernel = NULL;
+ program = NULL;
+}
+
+void
+cl_ocl_destroy(void)
+{
+ clReleaseCommandQueue(queue);
clReleaseContext(ctx);
}
+void
+cl_test_destroy(void)
+{
+ cl_kernel_destroy();
+ cl_ocl_destroy();
+ printf("%i memory leaks\n", clIntelReportUnfreed());
+ assert(clIntelReportUnfreed() == 0);
+}
+
+void
+cl_release_buffers(void)
+{
+ int i;
+ for (i = 0; i < MAX_BUFFER_N; ++i)
+ if (buf[i] != NULL) {
+ clReleaseMemObject(buf[i]);
+ buf[i] = NULL;
+ }
+}
+
static const char *err_msg[] = {
[-CL_SUCCESS] = "CL_SUCCESS",
[-CL_DEVICE_NOT_FOUND] = "CL_DEVICE_NOT_FOUND",
#include "CL/cl.h"
#include "common.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
-#define TEST_SIMD8 0
-#define CALL(FN, ...) \
- do { \
- status = FN(__VA_ARGS__); \
- if (status != CL_SUCCESS) { \
- fprintf(stderr, "error calling %s\n", #FN); \
- goto error; \
- } \
+#define CALL(FN, ...) \
+ do { \
+ status = FN(__VA_ARGS__); \
+ if (status != CL_SUCCESS) { \
+ fprintf(stderr, "error calling %s \n", #FN); \
+ goto error; \
+ } \
+ } while (0)
+
+#define OCL_CREATE_BUFFER(BUFFER, FLAGS, SIZE, DATA) \
+ do { \
+ cl_int status; \
+ BUFFER = clCreateBuffer(ctx, FLAGS, SIZE, DATA, &status); \
+ if (status != CL_SUCCESS) goto error; \
+ } while (0)
+
+#define OCL_MAP_BUFFER(ID) \
+ do { \
+ cl_int status; \
+ buf_data[ID] = (int *) clIntelMapBuffer(buf[ID], &status); \
+ if (status != CL_SUCCESS) goto error; \
+ } while (0)
+
+#define OCL_UNMAP_BUFFER(ID) \
+ do { \
+ CALL (clIntelUnmapBuffer, buf[ID]); \
+ buf_data[ID] = NULL; \
+ } while (0)
+
+#define OCL_NDRANGE(DIM_N) \
+ do { \
+ CALL (clEnqueueNDRangeKernel, queue, kernel, DIM_N, NULL, globals, locals, 0, NULL, NULL); \
+ } while (0)
+
+#define OCL_SET_ARG(ID, SIZE, ARG) \
+ do { \
+ CALL (clSetKernelArg, kernel, ID, SIZE, ARG); \
} while (0)
#ifdef __cplusplus
extern cl_program program;
extern cl_kernel kernel;
extern cl_command_queue queue;
+enum { MAX_BUFFER_N = 16 };
+extern cl_mem buf[MAX_BUFFER_N]; // initialized at NULL
+extern void* buf_data[MAX_BUFFER_N]; // initialized at NULL
+extern size_t globals[3]; // initialized at zero
+extern size_t locals[3]; // initialized at zero
-enum {
+enum {
SOURCE = 0,
LLVM = 1,
BIN = 2
};
-/* Init the bunch of global varaibles here */
+/* Init OpenCL */
+extern int cl_ocl_init(void);
+
+/* Init program and kernel for the test */
+extern int cl_kernel_init(const char *file_name, const char *kernel_name, int format);
+
+/* init the bunch of global varaibles here */
extern int cl_test_init(const char *file_name, const char *kernel_name, int format);
+/* Release all the created buffers */
+extern void cl_release_buffers(void);
+
+/* Release OCL queue, context and device */
+extern void cl_ocl_destroy(void);
+
+/* Release kernel and program */
+extern void cl_kernel_destroy(void);
+
/* Release everything allocated in cl_test_init */
extern void cl_test_destroy(void);
#include "cl_test.h"
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-
int
main (int argc, char *argv[])
{
+++ /dev/null
-/*
- * Copyright © 2012 Intel Corporation
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- * Author: Benjamin Segovia <benjamin.segovia@intel.com>
- */
-
-#include "cl_test.h"
-
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-int
-main (int argc, char *argv[])
-{
- cl_mem dst;
- int *dst_buffer = NULL;
- //const size_t n = 1024 * 1024;
- const size_t n = 2048;
- const size_t global_work_size = n;
- const size_t local_work_size = 16;
- int status = 0, i;
-
- if ((status = cl_test_init("test_write_only.cl", "test_write_only", SOURCE)) != 0)
- goto error;
-
- /* Allocate the two buffers */
- dst = clCreateBuffer(ctx, 0, n * sizeof(uint32_t), NULL, &status);
- if (status != CL_SUCCESS) goto error;
-
- /* Set source and destination */
- CALL (clSetKernelArg, kernel, 0, sizeof(cl_mem), &dst);
-
- /* Run the kernel */
- CALL (clEnqueueNDRangeKernel, queue,
- kernel,
- 1,
- NULL,
- &global_work_size,
- &local_work_size,
- 0,
- NULL,
- NULL);
-
- /* Be sure that everything run fine */
- dst_buffer = (int *) clIntelMapBuffer(dst, &status);
- if (status != CL_SUCCESS)
- goto error;
- for (i = 0; i < n; ++i) assert(dst_buffer[i] == i);
- CALL (clIntelUnmapBuffer, dst);
- CALL (clReleaseMemObject, dst);
- cl_test_destroy();
- printf("%i memory leaks\n", clIntelReportUnfreed());
- assert(clIntelReportUnfreed() == 0);
-
-error:
- cl_report_error(status);
- return status;
-}
-
--- /dev/null
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+#include "cl_test.h"
+
+int
+main (int argc, char *argv[])
+{
+ const size_t n = 2048;
+ int status = 0, i;
+
+ CALL (cl_test_init, "test_write_only.cl", "test_write_only", SOURCE);
+ OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(uint32_t), NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+ globals[0] = n;
+ locals[0] = 16;
+ OCL_NDRANGE(1);
+ OCL_MAP_BUFFER(0);
+ for (i = 0; i < n; ++i) assert(((int*)buf_data[0])[i] == i);
+ OCL_UNMAP_BUFFER(0);
+
+error:
+ cl_release_buffers();
+ cl_report_error(status);
+ cl_test_destroy();
+ return status;
+}
+