#include <cf4ocl2.h>
#include <assert.h>
#define KERNEL_SRC \
@canon_KERNEL_SRC@
#define KERNEL_NAME "sum"
#define DEF_BUF_N 16;
#define ERROR_MSG_AND_EXIT(msg) \
do { fprintf(stderr, "\n%s\n", msg); exit(EXIT_FAILURE); } while(0)
#define HANDLE_ERROR(err) \
if (err != NULL) { ERROR_MSG_AND_EXIT(err->message); }
int main(
int argc,
char** argv) {
size_t buf_n = DEF_BUF_N;
int dev_idx = -1;
int ret_val;
if (argc >= 2) {
dev_idx = atoi(argv[1]);
}
if (argc >= 3) {
buf_n = atoi(argv[2]);
}
size_t gws = 0;
size_t lws = 0;
cl_uint* a_host = NULL;
cl_uint* b_host = NULL;
cl_uint* c_host = NULL;
cl_uint d_host;
cl_bool check_result;
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
queue =
ccl_queue_new(ctx, dev, CL_QUEUE_PROFILING_ENABLE, &err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
printf("\n");
printf(" * Global worksize: %d\n", (int) gws);
printf(" * Local worksize : %d\n", (int) lws);
a_host = (cl_uint*) malloc(sizeof(cl_uint) * buf_n);
b_host = (cl_uint*) malloc(sizeof(cl_uint) * buf_n);
c_host = (cl_uint*) malloc(sizeof(cl_uint) * buf_n);
for (cl_uint i = 0; i < buf_n; ++i) {
a_host[i] = i;
b_host[i] = buf_n - i;
}
d_host = buf_n / 4;
buf_n * sizeof(cl_uint), NULL, &err);
HANDLE_ERROR(err);
buf_n * sizeof(cl_uint), NULL, &err);
HANDLE_ERROR(err);
buf_n * sizeof(cl_uint), NULL, &err);
HANDLE_ERROR(err);
buf_n * sizeof(cl_uint), a_host, NULL, &err);
HANDLE_ERROR(err);
buf_n * sizeof(cl_uint), b_host, NULL, &err);
HANDLE_ERROR(err);
NULL, &gws, &lws, &ewl, &err,
a_dev, b_dev, c_dev,
NULL);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
buf_n * sizeof(cl_uint), c_host, NULL, &err);
HANDLE_ERROR(err);
check_result = CL_TRUE;
for (cl_uint i = 0; i < buf_n; ++i) {
if(c_host[i] != a_host[i] + b_host[i] + d_host) {
check_result = CL_FALSE;
break;
}
}
if (check_result) {
fprintf(stdout, " * Kernel execution produced the expected results.\n");
ret_val = EXIT_SUCCESS;
} else {
fprintf(stderr,
" * Kernel execution failed to produce the expected results.\n");
ret_val = EXIT_FAILURE;
}
HANDLE_ERROR(err);
HANDLE_ERROR(err);
free(a_host);
free(b_host);
free(c_host);
return ret_val;
}