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
|
/*
Copyright (c) 2018 Jan Solanti / Tampere University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "poclu.h"
#ifdef _WIN32
#include "vccompat.hpp"
#endif
/*
Back-and-forth migration test. Creates one mutable buffer,
enqueues three kernels that have to be executed in order and reads the result
back.
*/
char kernelASourceCode[] = "kernel \n"
"void write_a(global int* input) {\n"
" input[0] = input[0] + 2;\n"
"}\n";
char kernelBSourceCode[] = "kernel \n"
"void write_b(global int* input) {\n"
" input[0] = input[0] / 2;\n"
"}\n";
char kernelCSourceCode[] = "kernel \n"
"void write_c(global int* input) {\n"
" input[0] = input[0] == 5 ? 1 : 0;\n"
"}\n";
int
main (int argc, char **argv)
{
cl_int input = 8, output = 0;
int err, total_err, spir, spirv;
cl_mem buf;
size_t global_work_size = 1;
size_t local_work_size = 1;
cl_platform_id platform = NULL;
cl_context context = NULL;
cl_device_id *devices = NULL;
cl_command_queue *queues = NULL;
cl_uint i, num_devices = 0;
cl_program program_a = NULL, program_b = NULL, program_c = NULL;
cl_kernel kernel_a = NULL, kernel_b = NULL, kernel_c = NULL;
const char *kernel_buffer = NULL;
err = poclu_get_multiple_devices (&platform, &context, 0, &num_devices,
&devices, &queues, 0);
CHECK_OPENCL_ERROR_IN ("poclu_get_multiple_devices");
printf ("NUM DEVICES: %u \n", num_devices);
if (num_devices < 2)
{
printf ("NOT ENOUGH DEVICES! (need 2)\n");
err = 77;
goto EARLY_EXIT;
}
kernel_buffer = kernelASourceCode;
program_a = clCreateProgramWithSource (
context, 1, (const char **)&kernel_buffer, NULL, &err);
CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource A");
CHECK_CL_ERROR (clBuildProgram (program_a, 0, NULL, NULL, NULL, NULL));
kernel_a = clCreateKernel (program_a, "write_a", &err);
CHECK_CL_ERROR2 (err);
kernel_buffer = kernelBSourceCode;
program_b = clCreateProgramWithSource (
context, 1, (const char **)&kernel_buffer, NULL, &err);
CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource B");
CHECK_CL_ERROR (clBuildProgram (program_b, 0, NULL, NULL, NULL, NULL));
kernel_b = clCreateKernel (program_b, "write_b", &err);
CHECK_CL_ERROR2 (err);
kernel_buffer = kernelCSourceCode;
program_c = clCreateProgramWithSource (
context, 1, (const char **)&kernel_buffer, NULL, &err);
CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource C");
CHECK_CL_ERROR (clBuildProgram (program_c, 0, NULL, NULL, NULL, NULL));
kernel_c = clCreateKernel (program_c, "write_c", &err);
CHECK_CL_ERROR2 (err);
buf = clCreateBuffer (context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof (cl_int), &input, &err);
CHECK_CL_ERROR2 (err);
err = clSetKernelArg (kernel_a, 0, sizeof (cl_mem), &buf);
CHECK_CL_ERROR2 (err);
err = clSetKernelArg (kernel_b, 0, sizeof (cl_mem), &buf);
CHECK_CL_ERROR2 (err);
err = clSetKernelArg (kernel_c, 0, sizeof (cl_mem), &buf);
CHECK_CL_ERROR2 (err);
cl_event event_a = NULL, event_b = NULL, event_c = NULL;
err = clEnqueueNDRangeKernel (queues[0], kernel_a, 1, NULL,
&global_work_size, &local_work_size, 0, NULL,
&event_a);
CHECK_CL_ERROR2 (err);
err = clEnqueueNDRangeKernel (queues[1], kernel_b, 1, NULL,
&global_work_size, &local_work_size, 1,
&event_a, &event_b);
CHECK_CL_ERROR2 (err);
err = clEnqueueNDRangeKernel (queues[0], kernel_c, 1, NULL,
&global_work_size, &local_work_size, 1,
&event_b, &event_c);
CHECK_CL_ERROR2 (err);
err = clEnqueueReadBuffer (queues[0], buf, CL_TRUE, 0, sizeof (cl_int),
&output, 1, &event_c, NULL);
CHECK_CL_ERROR2 (err);
fprintf (stderr, "DONE \n");
if (output == 1)
printf ("OK\n");
else
printf ("FAIL\n");
ERROR:
CHECK_CL_ERROR (clReleaseMemObject (buf));
CHECK_CL_ERROR (clReleaseKernel (kernel_c));
CHECK_CL_ERROR (clReleaseProgram (program_c));
CHECK_CL_ERROR (clReleaseKernel (kernel_b));
CHECK_CL_ERROR (clReleaseProgram (program_b));
CHECK_CL_ERROR (clReleaseKernel (kernel_a));
CHECK_CL_ERROR (clReleaseProgram (program_a));
if (event_c)
CHECK_CL_ERROR (clReleaseEvent (event_c));
if (event_b)
CHECK_CL_ERROR (clReleaseEvent (event_b));
if (event_a)
CHECK_CL_ERROR (clReleaseEvent (event_a));
EARLY_EXIT:
for (i = 0; i < num_devices; ++i)
{
CHECK_CL_ERROR (clReleaseCommandQueue (queues[i]));
}
CHECK_CL_ERROR (clReleaseContext (context));
CHECK_CL_ERROR (clUnloadPlatformCompiler (platform));
free (devices);
free (queues);
return err;
}
|