File: run_kernel.c

package info (click to toggle)
pocl 7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 29,768 kB
  • sloc: lisp: 151,669; ansic: 135,425; cpp: 65,801; python: 1,846; sh: 1,084; ruby: 255; pascal: 231; tcl: 180; makefile: 174; asm: 81; java: 72; xml: 49
file content (197 lines) | stat: -rw-r--r-- 6,684 bytes parent folder | download
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
/* run_kernel - a generic launcher for a kernel without inputs and outputs

   Copyright (c) 2012,2019 Pekka Jääskeläinen
                 2025 Pekka Jääskeläinen / Intel Finland Oy

   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 "poclu.h"
#include <CL/opencl.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
#  include "vccompat.hpp"
#endif

/**
 * The test kernels are assumed to:
 *
 * 1) Be called 'test_kernel'
 * 2) a) have no inputs or outputs, then only work item id printfs to verify
 *       the correct workgroup transformations, or
 *    b) have a single output buffer with the grid size of integers, which
 *       is dumped after executing the test or
 *    c) have a separate input and an output buffer with the grid size of
 * integers, which is dumped after executing the test, to avoid the need for
 * relying on printf or work-item ordering in the verification 3) be executable
 * with any local and global dimensions and sizes
 *
 * Usage:
 *
 * ./run_kernel somekernel.cl 2 2 3 4
 *
 * Where the first integer is the number of work groups to execute and the
 * rest are the local dimensions.
 */
int
main (int argc, char **argv)
{
  char *source;
  cl_platform_id pid = NULL;
  cl_context context = NULL;
  size_t cb = 0;
  cl_device_id *devices = NULL;
  cl_command_queue cmd_queue = NULL;
  cl_program program = NULL;
  cl_int err = CL_SUCCESS;
  cl_kernel kernel = NULL;
  cl_mem inbuf = NULL, outbuf = NULL;
  size_t global_work_size[3];
  size_t local_work_size[3];
  char kernel_path[2048];

  snprintf (kernel_path, 2048,  "%s/%s", SRCDIR, argv[1]);

  source = poclu_read_file (kernel_path);
  TEST_ASSERT (source != NULL && "Kernel .cl not found.");

  local_work_size[0] = atoi(argv[3]);
  local_work_size[1] = atoi(argv[4]);
  local_work_size[2] = atoi(argv[5]);

  global_work_size[0] = local_work_size[0] * atoi(argv[2]);
  global_work_size[1] = local_work_size[1];
  global_work_size[2] = local_work_size[2];

  context = poclu_create_any_context2 (&pid);
  TEST_ASSERT (context != NULL && "clCreateContextFromType call failed\n");

  err = clGetContextInfo (context, CL_CONTEXT_DEVICES, 0, NULL, &cb);
  CHECK_OPENCL_ERROR_IN ("clGetContextInfo 1\n");
  devices = (cl_device_id *) malloc(cb);
  clGetContextInfo (context, CL_CONTEXT_DEVICES, cb, devices, NULL);
  CHECK_OPENCL_ERROR_IN ("clGetContextInfo 2\n");

  cmd_queue = clCreateCommandQueue (context, devices[0], 0, &err);
  CHECK_CL_ERROR2 (err);

  program = clCreateProgramWithSource (context, 1, (const char **)&source,
                                       NULL, &err);
  CHECK_CL_ERROR2 (err);

  err = clBuildProgram (program, 0, NULL, NULL, NULL, NULL);
  if (err != CL_SUCCESS)
    poclu_show_program_build_log (program);
  CHECK_CL_ERROR2 (err);

  kernel = clCreateKernel (program, "test_kernel", &err);
  CHECK_CL_ERROR2 (err);

  cl_uint num_args = 0;
  err = clGetKernelInfo (kernel, CL_KERNEL_NUM_ARGS, sizeof (num_args),
                         &num_args, NULL);
  CHECK_CL_ERROR2 (err);

  size_t grid_size
      = global_work_size[0] * global_work_size[1] * global_work_size[2];
  if (num_args > 0)
    {
      assert (num_args <= 2);
      cl_int err;
      /* Input 4x the grid side of data to allow playing around with
         strided memory access patterns. Note that we read back only
         grid_size worth of data, so the output should be written to
         the lower part of the array, if it's also used for output. */
      cl_int *init_data = (cl_int *)malloc (4 * grid_size * sizeof (cl_int));
      for (int i = 0; i < 4 * grid_size; ++i)
        {
          init_data[i] = i;
        }
      inbuf
        = clCreateBuffer (context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
                          4 * grid_size * sizeof (cl_int), init_data, &err);
      CHECK_CL_ERROR2 (err);
      err = clSetKernelArg (kernel, 0, sizeof (inbuf), &inbuf);
      CHECK_CL_ERROR2 (err);
      if (num_args == 2)
        {
          /* A separate output buffer. */

          outbuf = clCreateBuffer (
            context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
            grid_size * sizeof (cl_int), init_data, &err);
          CHECK_CL_ERROR2 (err);
          err = clSetKernelArg (kernel, 1, sizeof (outbuf), &outbuf);
          CHECK_CL_ERROR2 (err);
        }
      free (init_data);
    }

  err = clEnqueueNDRangeKernel (cmd_queue, kernel, 3, NULL, global_work_size,
                                local_work_size, 0, NULL, NULL);
  CHECK_CL_ERROR2 (err);

  cl_int *kern_output = NULL;
  if (num_args > 0)
    {
      kern_output = malloc (grid_size * sizeof (cl_int));

      cl_mem result_buf = num_args == 1 ? inbuf : outbuf;

      err = clEnqueueReadBuffer (cmd_queue, result_buf, CL_TRUE, 0,
                                 grid_size * sizeof (cl_int), kern_output, 0,
                                 NULL, NULL);
      CHECK_CL_ERROR2 (err);

      size_t i;
      for (i = 0; i < grid_size; ++i)
        printf ("%zu: %d\n", i, kern_output[i]);
    }

  err = clFinish (cmd_queue);
  CHECK_CL_ERROR2 (err);

ERROR:
  if (outbuf)
    CHECK_CL_ERROR (clReleaseMemObject (outbuf));
  if (kernel)
    CHECK_CL_ERROR (clReleaseKernel (kernel));
  if (program)
    CHECK_CL_ERROR (clReleaseProgram (program));
  if (cmd_queue)
    CHECK_CL_ERROR (clReleaseCommandQueue (cmd_queue));
  if (context)
    CHECK_CL_ERROR (clReleaseContext (context));
  if (pid)
    CHECK_CL_ERROR (clUnloadPlatformCompiler (pid));

  free (source);
  free (devices);
  free (kern_output);

  if (err == CL_SUCCESS)
    {
      printf ("OK\n");
      return EXIT_SUCCESS;
    }
  return EXIT_FAILURE;
}