#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <cf4ocl2.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define CA_KERNEL \
@ca_KERNEL_SRC@
#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); }
#define IMAGE_FILE_PREFIX "out"
#define IMAGE_FILE_NUM_DIGITS 5
#define CA_WIDTH 128
#define CA_HEIGHT 128
#define CA_ITERS 64
int main(
int argc,
char* argv[]) {
char* filename;
int dev_idx = -1;
cl_bool image_ok;
cl_uchar4* input_image;
cl_uchar4** output_images;
unsigned int seed;
int file_write_status;
cl_image_format image_format = { CL_RGBA, CL_UNSIGNED_INT8 };
size_t origin[3] = { 0, 0, 0 };
size_t region[3] = { CA_WIDTH, CA_HEIGHT, 1 };
size_t real_ws[] = { CA_WIDTH, CA_HEIGHT };
size_t gws[2];
size_t lws[2];
if (argc >= 2) {
dev_idx = atoi(argv[1]);
}
if (argc >= 3) {
seed = atoi(argv[2]);
} else {
seed = (unsigned int) time(NULL);
}
srand(seed);
input_image = (cl_uchar4*)
malloc(CA_WIDTH * CA_HEIGHT * sizeof(cl_uchar4));
for (cl_uint i = 0; i < CA_WIDTH * CA_HEIGHT; ++i) {
cl_uchar state = (rand() & 0x3) ? 0xFF : 0x00;
input_image[i] = (cl_uchar4) {{ state, state, state, 0xFF }};
}
output_images = (cl_uchar4**)
malloc((CA_ITERS + 1) * sizeof(cl_uchar4*));
for (cl_uint i = 0; i < CA_ITERS + 1; ++i)
output_images[i] = (cl_uchar4*)
malloc(CA_WIDTH * CA_HEIGHT * sizeof(cl_uchar4));
HANDLE_ERROR(err);
HANDLE_ERROR(err);
dev, CL_DEVICE_IMAGE_SUPPORT, cl_bool, &err);
HANDLE_ERROR(err);
if (!image_ok)
ERROR_MSG_AND_EXIT("Selected device doesn't support images.");
queue_exec =
ccl_queue_new(ctx, dev, CL_QUEUE_PROFILING_ENABLE, &err);
HANDLE_ERROR(err);
queue_comm =
ccl_queue_new(ctx, dev, CL_QUEUE_PROFILING_ENABLE, &err);
HANDLE_ERROR(err);
&image_format, NULL, &err,
"image_type", (cl_mem_object_type) CL_MEM_OBJECT_IMAGE2D,
"image_width", (size_t) CA_WIDTH,
"image_height", (size_t) CA_HEIGHT,
NULL);
HANDLE_ERROR(err);
&image_format, NULL, &err,
"image_type", (cl_mem_object_type) CL_MEM_OBJECT_IMAGE2D,
"image_width", (size_t) CA_WIDTH,
"image_height", (size_t) CA_HEIGHT,
NULL);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
printf("\n * Global work-size: (%d, %d)\n", (int) gws[0], (int) gws[1]);
printf(" * Local work-size: (%d, %d)\n", (int) lws[0], (int) lws[1]);
origin, region, 0, 0, input_image, NULL, &err);
HANDLE_ERROR(err);
for (cl_uint i = 0; i < CA_ITERS; ++i) {
origin, region, 0, 0, output_images[i], NULL, &err);
HANDLE_ERROR(err);
krnl, queue_exec, 2, NULL, gws, lws, NULL, &err,
img1, img2, NULL);
HANDLE_ERROR(err);
HANDLE_ERROR(err);
img_aux = img1;
img1 = img2;
img2 = img_aux;
}
origin, region, 0, 0, output_images[CA_ITERS], &ewl, &err);
HANDLE_ERROR(err);
filename = (char*) malloc(
(strlen(IMAGE_FILE_PREFIX ".png") + IMAGE_FILE_NUM_DIGITS + 1)
* sizeof(char));
for (cl_uint i = 0; i < CA_ITERS; ++i) {
sprintf(filename, "%s%0" G_STRINGIFY(IMAGE_FILE_NUM_DIGITS) "d.png",
IMAGE_FILE_PREFIX, i);
file_write_status = stbi_write_png(filename, CA_WIDTH, CA_HEIGHT, 4,
output_images[i], CA_WIDTH * sizeof(cl_uchar4));
if (!file_write_status) {
ERROR_MSG_AND_EXIT("Unable to save image in file.");
}
}
HANDLE_ERROR(err);
HANDLE_ERROR(err);
free(filename);
free(input_image);
for (cl_uint i = 0; i < CA_ITERS + 1; ++i)
free(output_images[i]);
free(output_images);
return EXIT_SUCCESS;
}