File: test_buffer_migration.c

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (223 lines) | stat: -rw-r--r-- 6,813 bytes parent folder | download | duplicates (2)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
  Copyright (c) 2018 Michal Babej / 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 "config.h"
#include "poclu.h"

/*
  Multi-device migration test. Creates two buffers (in & out),
  enqueues the same kernel with different parameters on every device from 0 to
  N-1, then does the same in opposite direction (N-1 to 0th device). Verifies
  that pocl properly migrates the buffer contents across devices.
*/

#define ITEMS 1024

int
main (int argc, char **argv)
{
  cl_float *input = NULL, *output = NULL;
  int err, total_err, spir, spirv;
  cl_mem buf_in, buf_out;
  size_t global_work_size[2] = { 0 };
  size_t local_work_size[2] = { 0 };

  cl_platform_id platform = NULL;
  cl_context context = NULL;
  cl_device_id *devices = NULL;
  cl_command_queue *queues = NULL;
  cl_uint i, j, num_devices = 0;
  cl_program program = NULL;
  cl_kernel kernel = NULL;
  cl_event ev1, ev2;

  err = poclu_get_multiple_devices (&platform, &context, 0, &num_devices,
                                    &devices, &queues, 1);
  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;
    }

  const char *sourcefile = SRCDIR "/tests/runtime/migration_test";
  const char *basename = "migration_test";
  err = poclu_load_program_multidev (context, devices, num_devices, sourcefile,
                                     0, 0, NULL, NULL, &program);
  if (err != CL_SUCCESS)
    goto ERROR;

  char dev_name[1024];
  for (i = 0; i < num_devices; ++i)
    {
      size_t retval;
      err = clGetDeviceInfo (devices[i], CL_DEVICE_NAME, 1024, dev_name,
                             &retval);
      CHECK_CL_ERROR2 (err);

      dev_name[retval] = 0;
      printf ("DEVICE %u is: %s \n", i, dev_name);
    }

  printf ("------------------------\n");

  kernel = clCreateKernel (program, basename, NULL);
  CHECK_CL_ERROR2 (err);

  cl_uint num_floats = num_devices * ITEMS;
  input = (cl_float *)malloc (num_floats * sizeof (cl_float));
  output = (cl_float *)calloc (num_floats, sizeof (cl_float));

  srand48 (0);
  for (i = 0; i < num_floats; ++i)
    {
      input[i] = (cl_float)drand48 ();
    }

  buf_in = clCreateBuffer (context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                           sizeof (cl_float) * num_floats, input, NULL);
  CHECK_CL_ERROR2 (err);

  buf_out = clCreateBuffer (context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
                            sizeof (cl_float) * num_floats, output, NULL);
  CHECK_CL_ERROR2 (err);

  err = clSetKernelArg (kernel, 0, sizeof (cl_mem), (void *)&buf_in);
  CHECK_CL_ERROR2 (err);

  err = clSetKernelArg (kernel, 1, sizeof (cl_mem), (void *)&buf_out);
  CHECK_CL_ERROR2 (err);

  cl_uint num_items = ITEMS;
  err = clSetKernelArg (kernel, 2, sizeof (cl_uint), &num_items);
  CHECK_CL_ERROR2 (err);

  global_work_size[0] = ITEMS;
  local_work_size[0] = 64;

  fprintf (stderr, "FORWARD \n");

  for (i = 0; i < num_devices; ++i)
    {
      uint32_t index_arg = i;
      fprintf (stderr, "index ARG: %u\n", index_arg);
      err = clSetKernelArg (kernel, 3, sizeof (uint32_t), &index_arg);
      CHECK_CL_ERROR2 (err);

      err = clEnqueueNDRangeKernel (
          queues[i], kernel, 1, NULL, global_work_size, local_work_size,
          (i > 0 ? 1 : 0), (i > 0 ? &ev1 : NULL), &ev2);
      if (i > 0)
        clReleaseEvent (ev1);
      ev1 = ev2;

      CHECK_CL_ERROR2 (err);
    }

  clFinish (queues[num_devices - 1]);

  printf ("------------------------\n");

  fprintf (stderr, "NOW REVERSE \n");

  for (i = num_devices; i > 0; --i)
    {
      uint32_t index_arg = i - 1;
      fprintf (stderr, "index ARG: %u\n", index_arg);
      err = clSetKernelArg (kernel, 3, sizeof (uint32_t), &index_arg);
      CHECK_CL_ERROR2 (err);

      err = clEnqueueNDRangeKernel (queues[i - 1], kernel, 1, NULL,
                                    global_work_size, local_work_size, 1, &ev1,
                                    &ev2);
      clReleaseEvent (ev1);
      ev1 = ev2;

      CHECK_CL_ERROR2 (err);
    }

  err = clEnqueueReadBuffer (queues[0], buf_out, CL_TRUE, 0,
                             num_floats * sizeof (cl_float), output, 1, &ev1,
                             NULL);
  CHECK_CL_ERROR2 (err);
  fprintf (stderr, "DONE \n");

  clReleaseEvent (ev1);

  printf ("------------------------\n");

  fprintf (stderr, "VERIFYING RESULTS \n");

  total_err = 0;
  for (i = 0; i < num_devices; ++i)
    {
      err = 0;
      for (j = 0; j < ITEMS; ++j)
        {
          cl_float actual = output[i * ITEMS + j];
          cl_float expected = input[i * ITEMS + j] * (float)(i + 1) * 2.0f;
          if (expected != actual)
            {
              if (err < 10)
                printf ("FAIL at DEV %u ITEM %u: EXPECTED %e ACTUAL %e\n", i,
                        j, expected, actual);
              err += 1;
              total_err += 1;
            }
        }
      if (err > 0)
        printf ("DEV %u FAILED: %i errs\n", i, err);
      else
        printf ("DEV %u PASS\n", i);
    }
  if (total_err == 0)
    printf ("OK\n");
  else
    printf ("FAIL\n");

ERROR:
  CHECK_CL_ERROR (clReleaseMemObject (buf_in));
  CHECK_CL_ERROR (clReleaseMemObject (buf_out));
  CHECK_CL_ERROR (clReleaseKernel (kernel));
  CHECK_CL_ERROR (clReleaseProgram (program));

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 (input);
  free (output);
  free (devices);
  free (queues);

  return err;
}