1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef test_conformance_check_mem_host_read_only_h
17 #define test_conformance_check_mem_host_read_only_h
18
19 #include "checker.h"
20
21 template < class T> class cBuffer_check_mem_host_read_only : public cBuffer_checker<T>
22 {
23 public:
cBuffer_check_mem_host_read_only(cl_device_id deviceID,cl_context context,cl_command_queue queue)24 cBuffer_check_mem_host_read_only(cl_device_id deviceID, cl_context context, cl_command_queue queue)
25 : cBuffer_checker <T> (deviceID, context, queue)
26 {
27 };
28
~cBuffer_check_mem_host_read_only()29 ~cBuffer_check_mem_host_read_only()
30 {
31 };
32
33 virtual cl_int Check_GetMemObjectInfo(cl_mem_flags buffer_mem_flag);
34 virtual cl_int SetupBuffer();
35 virtual cl_int SetupASSubBuffer( cl_mem_flags flag_p);
36 virtual cl_int Setup_Test_Environment();
37
38 cl_int verifyData(cl_int err, cl_event & event);
39 cl_int verify_RW_Buffer();
40 cl_int verify_RW_Buffer_rect();
41 cl_int verify_RW_Buffer_mapping();
42 };
43
44 template < class T >
SetupBuffer()45 cl_int cBuffer_check_mem_host_read_only< T >::SetupBuffer()
46 {
47 this->m_buffer_type = _BUFFER;
48
49 this->m_nNumber_elements = 888;
50 T vv1 = TEST_VALUE;
51 this->host_m_1.Init(this->m_nNumber_elements, vv1);
52 this->host_m_0.Init(this->m_nNumber_elements, vv1);
53
54 cl_int err = CL_SUCCESS;
55 int block_size_in_byte = (int)(this->m_nNumber_elements * sizeof(T));
56 this->m_buffer = clCreateBuffer(this->m_context, this->buffer_mem_flag,
57 block_size_in_byte, this->host_m_1.pData, &err);
58 test_error(err, "clCreateBuffer error");
59
60 if (this->buffer_mem_flag & CL_MEM_USE_HOST_PTR)
61 {
62 this->pHost_ptr = (void *)this->host_m_1.pData;
63 }
64
65 return err;
66 }
67
68 template < class T >
SetupASSubBuffer(cl_mem_flags flag_p)69 cl_int cBuffer_check_mem_host_read_only<T>::SetupASSubBuffer(cl_mem_flags flag_p)
70 {
71 return cBuffer_checker<T>::SetupASSubBuffer(flag_p);
72 }
73
74 template < class T>
Setup_Test_Environment()75 cl_int cBuffer_check_mem_host_read_only<T>::Setup_Test_Environment()
76 {
77 cBuffer_checker<T>::Setup_Test_Environment();
78 T vv2 = 0;
79 this->host_m_2.Init(this->m_nNumber_elements, vv2);
80
81 return CL_SUCCESS;
82 }
83
84 template < class T >
Check_GetMemObjectInfo(cl_mem_flags buffer_mem_flag)85 cl_int cBuffer_check_mem_host_read_only< T >::Check_GetMemObjectInfo(cl_mem_flags buffer_mem_flag)
86 {
87 cl_int err = CL_SUCCESS;
88 cBuffer_checker<T>::Check_GetMemObjectInfo(buffer_mem_flag);
89
90 if (buffer_mem_flag & CL_MEM_ALLOC_HOST_PTR)
91 {
92 size_t size = 0;
93 err = clGetMemObjectInfo(this->m_buffer, CL_MEM_SIZE, sizeof(size), &size, NULL);
94 void *pp = NULL;
95 err = clGetMemObjectInfo(this->m_buffer, CL_MEM_HOST_PTR, sizeof( pp ), &pp, NULL);
96
97 if (!this->host_m_1.Equal( (T*) (this->pData), this->m_nNumber_elements )) {
98 log_error("Buffer data difference found\n");
99 return FAILURE;
100 }
101 }
102
103 return err;
104 }
105
106 template < class T >
verifyData(cl_int err,cl_event & event)107 cl_int cBuffer_check_mem_host_read_only< T >::verifyData( cl_int err, cl_event & event )
108 {
109 if (err != CL_SUCCESS) {
110 err = this->m_nERROR_RETURN_CODE;
111 test_error(err, "clEnqueueReadBuffer error");
112 }
113
114 if (!this->host_m_1.Equal(this->host_m_2)) {
115 err = this->m_nERROR_RETURN_CODE;
116 test_error(err, "clEnqueueReadBuffer data difference found");
117 }
118
119 return err;
120 }
121
122 template < class T >
verify_RW_Buffer()123 cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer()
124 {
125 cl_event event;
126 cl_int err = CL_SUCCESS;
127
128 err = clEnqueueReadBuffer(this->m_queue, this->m_buffer, this->m_blocking,
129 0, this->get_block_size_bytes(), this->host_m_2.pData,
130 0, NULL, &event);
131 test_error(err, "clEnqueueReadBuffer error");
132
133 if (!this->m_blocking) {
134 err = clWaitForEvents(1, &event);
135 test_error(err, "clWaitForEvents error");
136 }
137
138 if (!this->host_m_1.Equal(this->host_m_2)) {
139 log_error("Buffer data difference found\n");
140 return FAILURE;
141 }
142 err = clReleaseEvent(event);
143 test_error(err, "clReleaseEvent error");
144
145 // test write
146 err = clEnqueueWriteBuffer(this->m_queue, this->m_buffer, this->m_blocking,
147 0, this->get_block_size_bytes(), this->host_m_2.pData,
148 0, NULL, &event);
149
150 if (err == CL_SUCCESS) {
151 log_error("Calling clEnqueueWriteBuffer on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
152 err = FAILURE;
153 return FAILURE;
154
155 } else {
156 log_info("Test succeeded\n\n");
157 err = CL_SUCCESS;
158 }
159
160 return err;
161 }
162
163 template < class T >
verify_RW_Buffer_rect()164 cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer_rect()
165 {
166 this->Init_rect();
167
168 T vv2 = 0;
169 this->host_m_2.Set_to( vv2 );
170 cl_event event;
171 cl_int err = CL_SUCCESS;
172
173 err = clEnqueueReadBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
174 this->buffer_origin_bytes,
175 this->host_origin_bytes,
176 this->region_bytes,
177 this->buffer_row_pitch_bytes,
178 this->buffer_slice_pitch_bytes,
179 this->host_row_pitch_bytes,
180 this->host_slice_pitch_bytes,
181 this->host_m_2.pData,
182 0, NULL, &event);
183 test_error(err, "clEnqueueReadBufferRect error");
184
185 if (!this->m_blocking) {
186 err = clWaitForEvents(1, &event);
187 test_error(err, "clWaitForEvents error");
188 }
189
190 if (! this->host_m_1.Equal_rect(this->host_m_2, this->host_origin, this->region,
191 this->host_row_pitch, this->host_slice_pitch)) {
192 log_error("Buffer data diffeence found\n");
193 return FAILURE;
194 }
195 err = clReleaseEvent(event);
196 test_error(err, "clReleaseEvent error");
197
198 // test blocking write rect
199 err = clEnqueueWriteBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
200 this->buffer_origin_bytes,
201 this->host_origin_bytes,
202 this->region_bytes,
203 this->buffer_row_pitch_bytes,
204 this->buffer_slice_pitch_bytes,
205 this->host_row_pitch_bytes,
206 this->host_slice_pitch_bytes,
207 this->host_m_2.pData,
208 0, NULL, &event);
209
210 if (err == CL_SUCCESS) {
211 log_error("Calling clEnqueueWriteBufferRect on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
212 err = FAILURE;
213 return FAILURE;
214
215 } else {
216 log_info("Test succeeded\n\n");
217 err = CL_SUCCESS;
218 }
219
220 return err;
221 }
222
223 template < class T >
verify_RW_Buffer_mapping()224 cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer_mapping()
225 {
226 cl_int err = CL_SUCCESS;
227 cl_event event;
228 void *dataPtr;
229 dataPtr = clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
230 CL_MAP_READ,
231 0, this->get_block_size_bytes(),
232 0, NULL, &event, &err);
233 test_error(err, "clEnqueueMapBuffer error");
234
235 if (!this->m_blocking) {
236 err = clWaitForEvents(1, &event );
237 test_error(err, "clWaitForEvents error");
238 }
239
240 if ((this->buffer_mem_flag & CL_MEM_USE_HOST_PTR) && dataPtr != this->pHost_ptr ) {
241 log_error("Mapped host pointer difference found\n");
242 return FAILURE;
243 }
244
245 if(!this->host_m_1.Equal((T*)dataPtr, this->m_nNumber_elements)) {
246 log_error("Buffer content difference found\n");
247 return FAILURE;
248 }
249
250 err = clReleaseEvent(event);
251 test_error(err, "clReleaseEvent error");
252
253 err = clEnqueueUnmapMemObject(this->m_queue, this->m_buffer, dataPtr, 0,
254 nullptr, nullptr);
255 test_error(err, "clEnqueueUnmapMemObject error");
256
257 // test blocking map read
258 clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
259 CL_MAP_WRITE,
260 0, this->get_block_size_bytes(),
261 0, NULL, &event, &err);
262
263 if (err == CL_SUCCESS) {
264 log_error("Calling clEnqueueMapBuffer (CL_MAP_WRITE) on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
265 err = FAILURE;
266 return FAILURE;
267
268 } else {
269 log_info("Test succeeded\n\n");
270 err = CL_SUCCESS;
271 }
272
273 return err;
274 }
275
276 #endif
277