1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "poclu.h"
#ifdef _MSC_VER
# include "vccompat.hpp"
#endif
int main(int argc, char **argv)
{
/* test name */
char name[] = "test_sampler_address_clamp";
size_t global_work_size[1] = { 1 }, local_work_size[1]= { 1 };
size_t srcdir_length, name_length, filename_size;
char *filename = NULL;
char *source = NULL;
cl_platform_id pid = NULL;
cl_context context = NULL;
cl_command_queue queue = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_mem image = NULL;
cl_int result;
int retval = -1;
/* image parameters */
cl_uchar4 *imageData;
cl_image_format image_format;
cl_image_desc image_desc;
printf("Running test %s...\n", name);
memset(&image_desc, 0, sizeof(cl_image_desc));
image_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
image_desc.image_width = 4;
image_desc.image_height = 4;
image_format.image_channel_order = CL_RGBA;
image_format.image_channel_data_type = CL_UNSIGNED_INT8;
imageData = (cl_uchar4*)malloc (4 * 4 * sizeof(cl_uchar4));
if (imageData == NULL)
{
puts("out of host memory\n");
goto error;
}
memset (imageData, 1, 4*4*sizeof(cl_uchar4));
/* determine file name of kernel source to load */
srcdir_length = strlen(SRCDIR);
name_length = strlen(name);
filename_size = srcdir_length + name_length + 16;
filename = (char *)malloc(filename_size + 1);
if (!filename)
{
puts("out of memory");
goto error;
}
snprintf(filename, filename_size, "%s/%s.cl", SRCDIR, name);
/* read source code */
source = poclu_read_file (filename);
TEST_ASSERT (source != NULL && "Kernel .cl not found.");
/* setup an OpenCL context and command queue using default device */
context = poclu_create_any_context2 (&pid);
if (!context)
{
puts("clCreateContextFromType call failed\n");
goto error;
}
size_t device_id_size = 0;
result = clGetContextInfo (context, CL_CONTEXT_DEVICES, 0, NULL,
&device_id_size);
if (result != CL_SUCCESS)
{
puts ("clGetContextInfo call failed while fetching size\n");
goto error;
}
cl_device_id *devices = malloc (device_id_size);
TEST_ASSERT (devices != NULL && "out of host memory\n");
result = clGetContextInfo (context, CL_CONTEXT_DEVICES, device_id_size,
devices, NULL);
if (result != CL_SUCCESS)
{
puts ("clGetContextInfo call failed\n");
goto error;
}
queue = clCreateCommandQueue(context, devices[0], 0, NULL);
if (!queue)
{
puts("clCreateCommandQueue call failed\n");
goto error;
}
/* Create image */
image = clCreateImage (context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
&image_format, &image_desc, imageData, &result);
if (result != CL_SUCCESS)
{
puts("image creation failed\n");
goto error;
}
/* create and build program */
program = clCreateProgramWithSource (context, 1, (const char **)&source,
NULL, NULL);
if (!program)
{
puts("clCreateProgramWithSource call failed\n");
goto error;
}
result = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (result != CL_SUCCESS)
{
puts("clBuildProgram call failed\n");
goto error;
}
/* execute the kernel with give name */
kernel = clCreateKernel(program, name, NULL);
if (!kernel)
{
puts("clCreateKernel call failed\n");
goto error;
}
result = clSetKernelArg( kernel, 0, sizeof(cl_mem), &image);
if (result)
{
puts("clSetKernelArg failed\n");
goto error;
}
result = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size,
local_work_size, 0, NULL, NULL);
if (result != CL_SUCCESS)
{
puts("clEnqueueNDRangeKernel call failed\n");
goto error;
}
result = clFinish(queue);
if (result == CL_SUCCESS)
retval = 0;
error:
if (image)
{
CHECK_CL_ERROR (clReleaseMemObject (image));
}
if (kernel)
{
CHECK_CL_ERROR (clReleaseKernel (kernel));
}
if (program)
{
CHECK_CL_ERROR (clReleaseProgram (program));
}
if (queue)
{
CHECK_CL_ERROR (clReleaseCommandQueue (queue));
}
if (context)
{
CHECK_CL_ERROR (clReleaseContext (context));
CHECK_CL_ERROR (clUnloadPlatformCompiler (pid));
}
if (source)
{
free(source);
}
if (filename)
{
free(filename);
}
if (imageData)
{
free(imageData);
}
free (devices);
if (retval)
{
printf("FAIL\n");
return EXIT_FAILURE;
}
printf("OK\n");
return EXIT_SUCCESS;
}
|