• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_write_only__h
17 #define test_conformance_check_mem_host_write_only__h
18 
19 #include "checker.h"
20 
21 template < class T> class cBuffer_check_mem_host_write_only : public cBuffer_checker<T>
22 {
23 public:
cBuffer_check_mem_host_write_only(cl_device_id deviceID,cl_context context,cl_command_queue queue)24   cBuffer_check_mem_host_write_only(cl_device_id deviceID, cl_context context, cl_command_queue queue)
25   : cBuffer_checker < T > (deviceID, context, queue)
26   {
27     this->m_nNumber_elements = 1000;
28   };
29 
~cBuffer_check_mem_host_write_only()30   ~cBuffer_check_mem_host_write_only()
31   {
32   };
33 
34   cl_program program;
35   cl_kernel kernel;
36 
37   clMemWrapper m_buffer2;
38 
39   cl_int Setup_Test_Environment();
40 
41   cl_int SetupBuffer();
42   cl_int SetupASSubBuffer(cl_mem_flags flag_p);
43 
44   cl_int verifyData(cl_int err, cl_event &event );
45   cl_int update_host_mem_2();
46 
47   cl_int verify_RW_Buffer();
48   cl_int verify_RW_Buffer_rect();
49   cl_int verify_RW_Buffer_mapping();
50 
51   C_host_memory_block<T> tmp_host_m;
52 
53   virtual cl_int verify_Buffer_initialization();
54 };
55 
56 template < class T >
SetupBuffer()57 cl_int cBuffer_check_mem_host_write_only< T >::SetupBuffer()
58 {
59   T vv1 = 0;
60   this->host_m_1.Init( this->m_nNumber_elements, vv1); // zero out buffer
61 
62   // init buffer to 0
63   cl_int err;
64   int block_size_in_byte = this->get_block_size_bytes();
65 
66   this->m_buffer = clCreateBuffer(this->m_context, this->buffer_mem_flag,
67                                   block_size_in_byte, this->host_m_1.pData, &err);
68   test_error(err, "clCreateBuffer error");
69 
70   err = this->Check_GetMemObjectInfo(this->buffer_mem_flag);
71 
72   if (this->buffer_mem_flag | CL_MEM_USE_HOST_PTR)
73   {
74     this->pHost_ptr = (void *)this->host_m_1.pData;
75   }
76 
77   return err;
78 }
79 
80 template < class T >
SetupASSubBuffer(cl_mem_flags flag_p)81 cl_int cBuffer_check_mem_host_write_only<T>::SetupASSubBuffer(cl_mem_flags flag_p)
82 {
83   return cBuffer_checker<T>::SetupASSubBuffer(flag_p);
84 }
85 
86 template < class T >
Setup_Test_Environment()87 cl_int cBuffer_check_mem_host_write_only< T >::Setup_Test_Environment()
88 {
89   cl_int err;
90   T vv2 = 0;
91   this->host_m_2.Init(this->m_nNumber_elements, vv2);
92 
93   // init buffer2 to 0
94   cl_mem_flags buffer_mem_flag2 = CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR | CL_MEM_HOST_READ_ONLY;
95   this->m_buffer2 = clCreateBuffer(this->m_context, buffer_mem_flag2,
96                                    this->get_block_size_bytes(), this->host_m_2.pData, &err);
97   test_error(err, "clCreateBuffer error\n");
98 
99   return err;
100 }
101 
102 template < class T >
verify_Buffer_initialization()103 cl_int cBuffer_check_mem_host_write_only< T >::verify_Buffer_initialization()
104 {
105   cl_int err = CL_SUCCESS;
106 
107   if (this->host_m_1.pData == NULL || this->host_m_2.pData == NULL) {
108     log_error("Data not ready\n");
109     return FAILURE;
110   }
111 
112   update_host_mem_2();
113 
114   if (!this->host_m_1.Equal(this->host_m_2)){
115     log_error("Buffer content difference found\n");
116     return FAILURE;
117   }
118 
119   return err;
120 }
121 
122 template < class T >
verify_RW_Buffer()123 cl_int cBuffer_check_mem_host_write_only< T >::verify_RW_Buffer()
124 {
125   T vv1 = TEST_VALUE;
126   T vv2 = 0;
127   this->host_m_2.Set_to(vv2);
128 
129   tmp_host_m.Init(this->host_m_1.num_elements, vv1) ;
130 
131   cl_event event;
132   cl_int err = CL_SUCCESS;
133   err = clEnqueueWriteBuffer(this->m_queue, this->m_buffer, this->m_blocking, 0,
134                              this->get_block_size_bytes(), tmp_host_m.pData,
135                              0, NULL, &event);
136   if (err != CL_SUCCESS ) {
137     test_error(err, "clEnqueueWriteBuffer error");
138   }
139 
140   if (!this->m_blocking){
141     err = clWaitForEvents(1, &event);
142     test_error(err, "clWaitForEvents error")
143   }
144 
145   err = clReleaseEvent(event);
146   test_error(err, "clReleaseEvent error");
147 
148   if (tmp_host_m.Equal(this->host_m_2)){
149     log_error("Test data should be different\n");
150     return FAILURE;
151   }
152 
153   update_host_mem_2();
154 
155   if (!tmp_host_m.Equal(this->host_m_2)){
156     log_error("Buffer content difference found\n");
157     return FAILURE;
158   }
159 
160   err = clEnqueueReadBuffer(this->m_queue, this->m_buffer, CL_TRUE, 0,
161                             this->get_block_size_bytes(), this->host_m_2.pData,
162                             0, NULL, &event);
163 
164   if ( err == CL_SUCCESS ) {
165     log_error("Calling clEnqueueReadBuffer on a memory object created with the CL_MEM_HOST_WRITE_ONLY flag should not return CL_SUCCESS\n");
166     err = FAILURE;
167     return FAILURE;
168 
169   } else {
170     log_info("Test succeeded\n\n");
171     err = CL_SUCCESS;
172   }
173 
174   return  err;
175 }
176 
177 template < class T >
verify_RW_Buffer_rect()178 cl_int cBuffer_check_mem_host_write_only< T >::verify_RW_Buffer_rect()
179 {
180   this->Init_rect();
181 
182   T vv1= TEST_VALUE;
183   this->host_m_1.Set_to(vv1);
184 
185   T vv2 = 0;
186   this->host_m_2.Set_to(vv2);
187 
188   cl_event event, event_1;
189 
190   cl_int err = CL_SUCCESS;
191 
192   vv1 = 0;
193   C_host_memory_block< T > tmp_host_m;
194   tmp_host_m.Init(this->host_m_1.num_elements, vv1); // zero out the buffer
195   err = clEnqueueWriteBuffer(this->m_queue, this->m_buffer, CL_TRUE, 0,
196                              this->get_block_size_bytes(), tmp_host_m.pData,
197                              0, NULL, &event_1);
198   test_error(err, "clEnqueueWriteBuffer error");
199 
200   vv1 = TEST_VALUE;
201   tmp_host_m.Set_to(vv1);
202   err = clEnqueueWriteBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
203                                  this->buffer_origin_bytes,
204                                  this->host_origin_bytes,
205                                  this->region_bytes,
206                                  this->buffer_row_pitch_bytes,
207                                  this->buffer_slice_pitch_bytes,
208                                  this->host_row_pitch_bytes,
209                                  this->host_slice_pitch_bytes,
210                                  tmp_host_m.pData,
211                                  1, &event_1, &event);
212   test_error(err, "clEnqueueWriteBufferRect error");
213 
214   if (!this->m_blocking) {
215     err = clWaitForEvents(1, &event);
216     test_error(err, "clWaitForEvents error")
217   }
218 
219   if (tmp_host_m.Equal(this->host_m_2)) {
220     log_error("Test data should be different\n");
221     return FAILURE;
222   }
223 
224   err = clReleaseEvent(event_1);
225   test_error(err, "clReleaseEvent error");
226   err = clReleaseEvent(event);
227   test_error(err, "clReleaseEvent error");
228 
229   update_host_mem_2();
230 
231   size_t tot_in_reg = this->region[0] * this->region[1] * this->region[2];
232   if (!tmp_host_m.Equal_rect(this->host_m_2, this->host_origin, this->region,
233                              this->host_row_pitch, this->host_slice_pitch)) {
234     log_error("Buffer rect content difference found\n");
235     return FAILURE;
236   }
237 
238   if (this->host_m_2.Count(vv1) != tot_in_reg)
239   {
240     log_error("Buffer rect content difference found\n");
241     return FAILURE;
242   }
243 
244   err = clEnqueueReadBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
245                                 this->buffer_origin_bytes,
246                                 this->host_origin_bytes,
247                                 this->region_bytes,
248                                 this->buffer_row_pitch_bytes,
249                                 this->buffer_slice_pitch_bytes,
250                                 this->host_row_pitch_bytes,
251                                 this->host_slice_pitch_bytes,
252                                 this->host_m_2.pData,
253                                 0, NULL, &event);
254 
255   if (err == CL_SUCCESS) {
256     log_error("Calling clEnqueueReadBufferRect on a memory object created with the CL_MEM_HOST_WRITE_ONLY flag should not return CL_SUCCESS\n");
257     err = FAILURE;
258     return FAILURE;
259 
260   } else {
261     log_info("Test succeeded\n\n");
262     err = CL_SUCCESS;
263   }
264 
265   return err;
266 }
267 
268 template < class T >
update_host_mem_2()269 cl_int cBuffer_check_mem_host_write_only< T >::update_host_mem_2()
270 {
271   size_t global_work_size[3] = {0, 1, 1};
272   global_work_size[0] = this->get_block_size_bytes();
273 
274   cl_event event, event_2;
275   cl_int err = clEnqueueCopyBuffer(this->m_queue, this->m_buffer, this->m_buffer2, 0, 0,
276                                    this->m_nNumber_elements* sizeof (T), 0, NULL, &event);
277   test_error(err, "clEnqueueCopyBuffer error");
278 
279   this->host_m_2.Set_to_zero();
280   err = clEnqueueReadBuffer(this->m_queue, this->m_buffer2, CL_TRUE, 0,
281                             this->get_block_size_bytes(), this->host_m_2.pData,
282                             1, &event, &event_2);
283   test_error(err, "clEnqueueReadBuffer error");
284 
285   clWaitForEvents(1, &event_2);
286   test_error(err, "clWaitForEvents error");
287 
288   err = clReleaseEvent(event_2);
289   test_error(err, "clReleaseEvent error");
290 
291   err = clReleaseEvent(event);
292   test_error(err, "clReleaseEvent error");
293   return err;
294 }
295 
296 template < class T >
verify_RW_Buffer_mapping()297 cl_int cBuffer_check_mem_host_write_only< T >::verify_RW_Buffer_mapping()
298 {
299   T vv2 = 0;
300   this->host_m_2.Set_to(vv2);
301 
302   cl_event event;
303   cl_int err = CL_SUCCESS;
304 
305   void *dataPtr;
306   int size = this->get_block_size_bytes();
307   dataPtr = clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
308                                CL_MAP_WRITE,
309                                0, size,
310                                0, NULL, &event, &err);
311   test_error(err, "clEnqueueMapBuffer error");
312 
313   if (!this->m_blocking) {
314     err = clWaitForEvents(1, &event);
315     test_error(err, "clWaitForEvents error");
316   }
317 
318   err = clReleaseEvent(event);
319   test_error(err, "clReleaseEvent error");
320 
321   update_host_mem_2();
322 
323   if ((this->buffer_mem_flag & CL_MEM_USE_HOST_PTR) && dataPtr != this->pHost_ptr){
324     log_error("Mapped host pointer difference found\n");
325     return FAILURE;
326   }
327 
328   if(!this->host_m_2.Equal((T*)dataPtr, this->m_nNumber_elements)) {
329     log_error("Buffer content difference found\n");
330     return FAILURE;
331   }
332 
333   err = clEnqueueUnmapMemObject(this->m_queue, this->m_buffer, dataPtr, 0,
334                                 nullptr, nullptr);
335   test_error(err, "clEnqueueUnmapMemObject error");
336 
337   // test map read
338   clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
339                      CL_MAP_READ,
340                      0, this->get_block_size_bytes(),
341                      0, NULL, &event, &err);
342 
343   if (err == CL_SUCCESS) {
344     log_error("Calling clEnqueueMapBuffer (CL_MAP_READ) on a memory object created with the MEM_HOST_WRITE_ONLY flag should not return CL_SUCCESS\n");
345     err = FAILURE;
346 
347   } else {
348     log_info("Test succeeded\n\n");
349     err = CL_SUCCESS;
350   }
351 
352   return err;
353 }
354 
355 #endif
356