aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/events/test_userevents.cpp
blob: 0a4954f98ec1f1fd94df747c6cf4eb1f0540e2c7 (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
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
//
// Copyright (c) 2017 The Khronos Group Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if defined(__APPLE__)
    #include <OpenCL/opencl.h>
    #include <mach/mach_time.h>
#else
    #include <CL/cl.h>
  #include <malloc.h>
#endif
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "harness/kernelHelpers.h"

///////////////////////////////////////////////////////////////////////////////
// CL error checking.

#if defined(_MSC_VER)
#define CL_EXIT_ERROR(cmd,...) \
{ \
if ((cmd) != CL_SUCCESS) { \
log_error("CL ERROR: %s %u: ", __FILE__,__LINE__);\
log_error(## __VA_ARGS__ );\
log_error("\n");\
return -1;\
}\
}
#else
#define CL_EXIT_ERROR(cmd,format,...) \
{ \
if ((cmd) != CL_SUCCESS) { \
log_error("CL ERROR: %s %u: ", __FILE__,__LINE__);\
log_error(format,## __VA_ARGS__ );\
log_error("\n");\
return -1;\
}\
}
#endif

#define CL_EXIT_BUILD_ERROR(cmd,program,format,...) \
{ \
if ((cmd) != CL_SUCCESS) { \
cl_uint num_devices_;\
clGetProgramInfo(program,CL_PROGRAM_NUM_DEVICES,sizeof(num_devices_),&num_devices_,NULL);\
cl_device_id *device_list;\
device_list=(cl_device_id *)malloc(num_devices_*sizeof(cl_device_id));\
clGetProgramInfo(program,CL_PROGRAM_DEVICES,num_devices_*sizeof(cl_device_id),device_list,NULL);\
for (unsigned i=0;i<num_devices_;++i) {\
size_t len;\
char buffer[2048];\
clGetProgramBuildInfo(program,device_list[i],CL_PROGRAM_BUILD_LOG,sizeof(buffer),buffer,&len);\
log_error("DEVICE %u CL BUILD ERROR: %s(%u): ",i,__FILE__,__LINE__);\
log_error(format,## __VA_ARGS__ );\
log_error("\n");\
}\
free(device_list);\
return -1;\
}\
}

const char* src[] = {
  "__kernel void simple_task(__global float* output) {\n"
  "  output[0] += 1;\n"
  "}\n"
};

enum { MaxDevices = 8 };

int test_userevents( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
{

  cl_int err;

  cl_event u1 = clCreateUserEvent( context, &err );
  CL_EXIT_ERROR(err,"clCreateUserEvent failed");

  // Test event properties.
  cl_int s;
  size_t sizeofs;
  CL_EXIT_ERROR(clGetEventInfo(u1, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof s, &s, &sizeofs),"clGetEventInfo failed");
  CL_EXIT_ERROR((sizeof s == sizeofs) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong size for CL_EVENT_COMMAND_EXECUTION_STATUS");
  CL_EXIT_ERROR((s == CL_SUBMITTED) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong value for CL_EVENT_COMMAND_EXECUTION_STATUS");

  cl_command_type t;
  size_t sizeoft;
  CL_EXIT_ERROR(clGetEventInfo(u1, CL_EVENT_COMMAND_TYPE, sizeof t, &t, &sizeoft),"clGetEventInfo failed");
  CL_EXIT_ERROR((sizeof t == sizeoft) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong size for CL_EVENT_COMMAND_TYPE");
  CL_EXIT_ERROR((t == CL_COMMAND_USER) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong value for CL_EVENT_COMMAND_TYPE");

  cl_command_queue q;
  size_t sizeofq;
  CL_EXIT_ERROR(clGetEventInfo(u1, CL_EVENT_COMMAND_QUEUE, sizeof q, &q, &sizeofq),"clGetEventInfo failed");
  CL_EXIT_ERROR((sizeof q == sizeofq) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong size for CL_EVENT_COMMAND_QUEUE");
  CL_EXIT_ERROR((q == NULL) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong value for CL_EVENT_COMMAND_QUEUE");

  cl_context c;
  size_t sizeofc;
  CL_EXIT_ERROR(clGetEventInfo(u1, CL_EVENT_CONTEXT, sizeof c, &c, &sizeofc),"clGetEventInfo failed");
  CL_EXIT_ERROR((sizeof c == sizeofc) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong size for CL_EVENT_CONTEXT");
  CL_EXIT_ERROR((c == context) ? CL_SUCCESS : -1,"clGetEventInfo returned wrong value for CL_EVENT_CONTEXT");

  cl_ulong p;
  err = clGetEventProfilingInfo(u1,CL_PROFILING_COMMAND_QUEUED,sizeof p,&p,0);
  CL_EXIT_ERROR((err != CL_SUCCESS) ? CL_SUCCESS : -1,"clGetEventProfilingInfo returned wrong error.");

  // Test semantics.
  cl_program program;
  err = create_single_kernel_helper_create_program(context, &program, 1, src);
  CL_EXIT_ERROR(err,"clCreateProgramWithSource failed");

  CL_EXIT_BUILD_ERROR(clBuildProgram(program,0,NULL,"",NULL,NULL),program,"Building program from inline src:\t%s",src[0]);

  cl_kernel k0 = clCreateKernel(program,"simple_task",&err);
  CL_EXIT_ERROR(err,"clCreateKernel failed");

  float buffer[1];
  cl_mem output = clCreateBuffer(context,CL_MEM_USE_HOST_PTR,sizeof buffer, buffer, &err);
  CL_EXIT_ERROR(err,"clCreateBuffer failed.");

  CL_EXIT_ERROR(clSetKernelArg(k0,0,sizeof(output),&output),"clSetKernelArg failed");


  // Successful case. //////////////////////////////////////////////////////////////////////////////////////
  {
    cl_event e[4];
    cl_uint  N = sizeof e / sizeof(cl_event);

    log_info("Enqueuing tasks\n");
    for (cl_uint i = 0; i != N; ++i)
      CL_EXIT_ERROR(clEnqueueTask(queue,k0,1,&u1,&e[i]),"clEnqueueTaskFailed");

    log_info("Checking task status before setting user event status\n");
    for (cl_uint i = 0; i != N; ++i) {
      CL_EXIT_ERROR(clGetEventInfo(e[i],CL_EVENT_COMMAND_EXECUTION_STATUS,sizeof s,&s,0),"clGetEventInfo failed");
      CL_EXIT_ERROR((s >= CL_SUBMITTED) ? CL_SUCCESS : -1,"clGetEventInfo %u returned wrong status before user event",i);
    }

    log_info("Setting user event status to complete\n");
    CL_EXIT_ERROR(clSetUserEventStatus(u1,CL_COMPLETE),"clSetUserEventStatus failed");

    log_info("Waiting for tasks to finish executing\n");
    CL_EXIT_ERROR(clWaitForEvents( 1, &e[N-1] ),"clWaitForEvent failed");

    log_info("Checking task status after setting user event status\n");
    for (cl_uint i = 0; i != N; ++i) {
      CL_EXIT_ERROR(clGetEventInfo(e[i],CL_EVENT_COMMAND_EXECUTION_STATUS,sizeof s,&s,0),"clGetEventInfo failed");
      CL_EXIT_ERROR((s != CL_QUEUED) ? CL_SUCCESS : -1,"clGetEventInfo %u returned wrong status %04x after successful user event",i,s);
    }

    CL_EXIT_ERROR(clReleaseEvent(u1),"clReleaseEvent failed");

    for (cl_uint i = 0; i != N; ++i)
      CL_EXIT_ERROR(clReleaseEvent(e[i]),"clReleaseEvent failed");

    log_info("Successful user event case passed.\n");

  }

  // Test unsuccessful user event case. ///////////////////////////////////////////////////////////////////
  {
    cl_event u2 = clCreateUserEvent( context, &err );
    CL_EXIT_ERROR(err,"clCreateUserEvent failed");

    cl_event e[4];
    cl_uint  N = sizeof e / sizeof(cl_event);

    log_info("Enqueuing tasks\n");
    for (cl_uint i = 0; i != N; ++i)
      CL_EXIT_ERROR(clEnqueueTask(queue,k0,1,&u2,&e[i]),"clEnqueueTaskFailed");

    log_info("Checking task status before setting user event status\n");
    for (cl_uint i = 0; i != N; ++i) {
      CL_EXIT_ERROR(clGetEventInfo(e[i],CL_EVENT_COMMAND_EXECUTION_STATUS,sizeof s,&s,0),"clGetEventInfo failed");
      CL_EXIT_ERROR((s == CL_QUEUED || s == CL_SUBMITTED) ? CL_SUCCESS : -1,"clGetEventInfo %u returned wrong status %d before user event",i, (int) s);
    }

    log_info("Setting user event status to unsuccessful result\n");
    CL_EXIT_ERROR(clSetUserEventStatus(u2,-1),"clSetUserEventStatus failed");

    log_info("Waiting for tasks to finish executing\n");
    CL_EXIT_ERROR((clWaitForEvents( N, &e[0] )!=CL_SUCCESS) ? CL_SUCCESS : -1,"clWaitForEvent succeeded when it should have failed");

    log_info("Checking task status after setting user event status\n");
    for (cl_uint i = 0; i != N; ++i) {
      CL_EXIT_ERROR(clGetEventInfo(e[i],CL_EVENT_COMMAND_EXECUTION_STATUS,sizeof s,&s,0),"clGetEventInfo failed");
      CL_EXIT_ERROR((s != CL_QUEUED) ? CL_SUCCESS : -1,"clGetEventInfo %u returned wrong status %04x after unsuccessful user event",i,s);
    }

    CL_EXIT_ERROR(clReleaseEvent(u2),"clReleaseEvent failed");

    for (cl_uint i = 0; i != N; ++i)
      CL_EXIT_ERROR(clReleaseEvent(e[i]),"clReleaseEvent failed");

    log_info("Unsuccessful user event case passed.\n");
  }

  clReleaseKernel(k0);
  clReleaseProgram(program);
  clReleaseMemObject(output);

  return 0;

}