summaryrefslogtreecommitdiff
path: root/get_global_id.c
blob: e06349a138d9ae95e34b89414483355e244f1804 (plain)
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
#include <stdio.h>
#include <stdlib.h>

#include <CL/cl.h>

#include "cl_simple.h"

int main(int argc, char ** argv)
{
   struct cl_simple_context context;

   unsigned i;
   int * out;
   unsigned out_size;
   size_t global_work_size, local_work_size;

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

   out_size = global_work_size * sizeof(int);
   out = malloc(out_size);

   if (!out) {
      return EXIT_FAILURE;
   }

   if (!clSimpleSimpleInit(&context, "global_id")) {
      return EXIT_FAILURE;
   }

   if (!clSimpleSetOutputBuffer(&context, out_size)) {
      return EXIT_FAILURE;
   }

   if(!clSimpleEnqueueNDRangeKernel(context.command_queue,
                              context.kernel,
                              1, &global_work_size, &global_work_size)) {
      return EXIT_FAILURE;
   }

   if (!clSimpleReadOutput(&context, out, out_size)) {
      return EXIT_FAILURE;
   }

   /* Print the result */
   for (i = 0; i < global_work_size; i++) {
      fprintf(stderr, "id %u = %u\n", i, out[i]);
   }

   /* Check the result */
   for (i = 0; i < global_work_size; i++) {
      if (i != out[i]) {
         fprintf(stderr, "Expected out[%u] = %u, actual: %u\n", i, i, out[i]);
         return EXIT_FAILURE;
      }
   }

   return EXIT_SUCCESS;
}