File: test_clSetEventCallback.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 (169 lines) | stat: -rw-r--r-- 5,339 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
/* Tests clSetEventCallback() 

   Copyright (c) 2013 Ville Korhonen / Tampere University of Technology
   
   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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pthread.h>

#include "poclu.h"

int submit = 0;
int running = 0;
int complete = 0;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK pthread_mutex_lock(&mutex)
#define UNLOCK pthread_mutex_unlock(&mutex)


void callback_function(cl_event event, 
                       cl_int   event_command_exec_status, 
                       void     *user_data)
{
  printf("%s ", (const char *)user_data);
  LOCK;
  if(event_command_exec_status == CL_SUBMITTED)
    {
      printf("CL_SUBMITTED\n");
      submit = 1;
    }
  if(event_command_exec_status == CL_RUNNING)
    {
      printf("CL_RUNNING\n");
      running = 1;
    }
  if(event_command_exec_status == CL_COMPLETE)
    {
      printf("CL_COMPLETE\n");
      complete = 1;
    }
  UNLOCK;
  fflush(stdout);
  return;
}

/* TODO this test relied on output of printf() from kernel
 * appearing in specific order WRT event state callbacks,
 * which is likely UB. OpenCL states printf()
 * should happen "at clFinish time" but AFAICT
 * does not state, if it should happen before
 * or after the event-completed callback call.
 * -> test disabled for now. */
char kernelASourceCode[] = 
"kernel \n"
"void test_kernel(constant char* input) {\n"
"    if (input[0] == 'X') printf(\"match\");\n"
"}\n";

int main()
{
  size_t global_work_size[1] = { 1 }, local_work_size[1]= { 1 };
  cl_int err;
  cl_program program = NULL;
  cl_kernel kernel = NULL;
  char input[] = "kernel in execution\n";
  char *user_data = "Callback function: event status:";
  cl_mem inputBuffer = NULL;
  /* events */
  cl_event an_event = NULL;
  int i;

  cl_platform_id platform = NULL;
  cl_context context = NULL;
  cl_device_id device = NULL;
  cl_command_queue queue = NULL;

  CHECK_CL_ERROR (
    poclu_get_any_device2 (&context, &device, &queue, &platform));
  TEST_ASSERT( context );
  TEST_ASSERT( device );
  TEST_ASSERT( queue );

  CHECK_CL_ERROR(clGetContextInfo(context, CL_CONTEXT_DEVICES,
                         sizeof(cl_device_id), &device, NULL));

  inputBuffer = clCreateBuffer(context, 
                               CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 
                               strlen (input)+1, (void *) input, &err);
  CHECK_OPENCL_ERROR_IN("clCreateBuffer");

  size_t kernel_size = strlen (kernelASourceCode);
  char* kernel_buffer = kernelASourceCode;
  
  program = clCreateProgramWithSource (context, 1, 
                                       (const char**)&kernel_buffer, 
                                       &kernel_size, &err);
  CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource");

  CHECK_CL_ERROR(clBuildProgram (program, 1, &device, NULL, NULL, NULL));

  kernel = clCreateKernel (program, "test_kernel", &err);
  CHECK_OPENCL_ERROR_IN("clCreateKernel");
  
  CHECK_CL_ERROR(clSetKernelArg (kernel, 0, sizeof (cl_mem), &inputBuffer));
 
  /* launch kernel*/
  CHECK_CL_ERROR(clEnqueueNDRangeKernel (queue, kernel, 1, NULL, global_work_size,
                                local_work_size, 0, NULL, &an_event));

  CHECK_CL_ERROR(clSetEventCallback(an_event, CL_SUBMITTED, callback_function, user_data));
  CHECK_CL_ERROR(clSetEventCallback(an_event, CL_RUNNING, callback_function, user_data));
  CHECK_CL_ERROR(clSetEventCallback(an_event, CL_COMPLETE, callback_function, user_data));

  CHECK_CL_ERROR(clFinish(queue));

  i = 0;
  LOCK;
  int all_done = submit + running + complete;
  UNLOCK;
  while (all_done != 3)
    {
      sleep(1);
      ++i;
      if (i >= 10)
        {
          puts("Callback functions were not called in 10s -> assume FAIL\n");
          return EXIT_FAILURE;
        }
      LOCK;
      all_done = submit + running + complete;
      UNLOCK;
    }

  CHECK_CL_ERROR (clReleaseEvent (an_event));

  CHECK_CL_ERROR (clReleaseCommandQueue (queue));
  CHECK_CL_ERROR (clReleaseMemObject (inputBuffer));

  CHECK_CL_ERROR (clReleaseKernel (kernel));
  CHECK_CL_ERROR (clReleaseProgram (program));

  CHECK_CL_ERROR (clReleaseContext (context));
  CHECK_CL_ERROR (clUnloadPlatformCompiler (platform));

  printf ("OK\n");
  return EXIT_SUCCESS;
}