summaryrefslogtreecommitdiff
path: root/math-int.c
blob: e02439e5760132f46cf8ee471559ed1efa128d8d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>

#include <CL/cl.h>


int main(int argc, char ** argv)
{
   const char * kernel_name = argv[1];
   int arg0, arg1, expected, result;
   cl_int error;
   cl_device_id device_id;

   cl_context context;

   cl_command_queue command_queue;

   cl_kernel kernel;

   cl_mem out_buffer;
   int out_value = 0;
   size_t global_work_size = 1;

   /* Parse command line args */
   arg0 = atoi(argv[2]);
   arg1 = atoi(argv[3]);
   expected = atoi(argv[4]);

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

   context = clCreateContext(NULL, /* Properties */
                           1, /* Number of devices */
                           &device_id, /* Device pointer */
                           NULL, /* Callback for reporting errors */
                           NULL, /* User data to pass to error callback */
                           &error); /* Error code */

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

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

   command_queue = clCreateCommandQueue(context,
                                        device_id,
                                        0, /* Command queue properties */
                                        &error); /* Error code */

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

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

   if (!cluCreateKernel(context, device_id, &kernel, kernel_name)) {
      return EXIT_FAILURE;
   }

   out_buffer = clCreateBuffer(context,
                               CL_MEM_WRITE_ONLY, /* Flags */
                               sizeof(int), /* 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(kernel, 0, sizeof(cl_mem), &out_buffer)
      || !cluKernelSetArg(kernel, 1, sizeof(int), &arg0)
      || !cluKernelSetArg(kernel, 2, sizeof(int), &arg1)) {
      return EXIT_FAILURE;
   }

   error = clEnqueueNDRangeKernel(command_queue,
                                  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(command_queue,
                                out_buffer,
                                CL_TRUE, /* TRUE means it is a blocking read. */
                                0, /* Buffer offset to read from. */
                                sizeof(int), /* Bytes to read */
                                &out_value, /* 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");

   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;
   }
}