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

#include <CL/cl.h>

#include "util.h"

int main(int argc, char ** argv)
{
   cl_int error;

   struct clu_context context;

   cl_mem out_buffer;

   unsigned i;
   int out_data[10];
   size_t global_work_size = 10;

   if (!cluInitGpuDevice(&context.device_id)) {
      return EXIT_FAILURE;
   }

   if (!cluCreateContext(&context.cl_ctx, context.device_id)) {
      return EXIT_FAILURE;
   }

   if (!cluCreateCommandQueue(&context.command_queue, context.cl_ctx,
                                                      context.device_id)) {
      return EXIT_FAILURE;
   }

   if (!cluCreateKernel(context.cl_ctx, context.device_id, &context.kernel,
                                                           "global_id")) {
      return EXIT_FAILURE;
   }

   out_buffer = clCreateBuffer(context.cl_ctx,
                               CL_MEM_WRITE_ONLY, /* Flags */
                               sizeof(out_data), /* Size of buffer */
                               NULL, /* Pointer to the data */
                               &error); /* error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateBuffer() failed: %s\n", cluErrorString(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clCreateBuffer() succeeded.\n");

  if (   !cluKernelSetArg(context.kernel, 0, sizeof(cl_mem), &out_buffer)) {
      return EXIT_FAILURE;
   }

   error = clEnqueueNDRangeKernel(context.command_queue,
                                  context.kernel,
                                  1, /* Number of dimensions */
                                  NULL, /* Global work offset */
                                  &global_work_size,
                                  &global_work_size, /* local work size */
                                  0, /* Events in wait list */
                                  NULL, /* Wait list */
                                  NULL); /* Event object for this event */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clEnqueueNDRangeKernel() failed: %s\n",
                      cluErrorString(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clEnqueueNDRangeKernel() suceeded.\n");

   error = clEnqueueReadBuffer(context.command_queue,
                                out_buffer,
                                CL_TRUE, /* TRUE means it is a blocking read. */
                                0, /* Buffer offset to read from. */
                                sizeof(out_data), /* Bytes to read */
                                out_data, /* Pointer to store the data */
                                0, /* Events in wait list */
                                NULL, /* Wait list */
                                NULL); /* Event object */


   if (error != CL_SUCCESS) {
      fprintf(stderr, "clEnqueueReadBuffer() failed: %s\n",
                      cluErrorString(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clEnqueueReadBuffer() suceeded.\n");

   for (i = 0; i < 10; i++) {
      fprintf(stderr, "id %u = %u\n", i, out_data[i]);
   }

/*

   if (out_value == expected) {
      fprintf(stderr, "Pass\n");
      return EXIT_SUCCESS;
   } else {
      fprintf(stderr, "Expected %d, but got %d\n", expected, out_value);
      return EXIT_FAILURE;
   }
*/
}