aboutsummaryrefslogtreecommitdiff
path: root/test_conformance/mem_host_flags/checker_mem_host_read_only.hpp
blob: 44910b04a5605229cdd17485523f942065a07fdf (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// 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.
//
#ifndef test_conformance_check_mem_host_read_only_h
#define test_conformance_check_mem_host_read_only_h

#include "checker.h"

template < class T> class cBuffer_check_mem_host_read_only : public cBuffer_checker<T>
{
public:
  cBuffer_check_mem_host_read_only(cl_device_id deviceID, cl_context context, cl_command_queue queue)
  : cBuffer_checker <T> (deviceID, context, queue)
  {
  };

  ~cBuffer_check_mem_host_read_only()
  {
  };

  virtual cl_int Check_GetMemObjectInfo(cl_mem_flags buffer_mem_flag);
  virtual cl_int SetupBuffer();
  virtual cl_int SetupASSubBuffer( cl_mem_flags flag_p);
  virtual cl_int Setup_Test_Environment();

  cl_int verifyData(cl_int err, cl_event & event);
  cl_int verify_RW_Buffer();
  cl_int verify_RW_Buffer_rect();
  cl_int verify_RW_Buffer_mapping();
};

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::SetupBuffer()
{
  this->m_buffer_type = _BUFFER;

  this->m_nNumber_elements = 888;
  T vv1 = TEST_VALUE;
  this->host_m_1.Init(this->m_nNumber_elements, vv1);
  this->host_m_0.Init(this->m_nNumber_elements, vv1);

  cl_int err = CL_SUCCESS;
  int block_size_in_byte = (int)(this->m_nNumber_elements * sizeof(T));
  this->m_buffer = clCreateBuffer(this->m_context, this->buffer_mem_flag,
                                  block_size_in_byte, this->host_m_1.pData, &err);
  test_error(err, "clCreateBuffer error");

  if (this->buffer_mem_flag & CL_MEM_USE_HOST_PTR)
  {
    this->pHost_ptr = (void *)this->host_m_1.pData;
  }

  return err;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only<T>::SetupASSubBuffer(cl_mem_flags flag_p)
{
  return cBuffer_checker<T>::SetupASSubBuffer(flag_p);
}

template < class T>
cl_int cBuffer_check_mem_host_read_only<T>::Setup_Test_Environment()
{
  cBuffer_checker<T>::Setup_Test_Environment();
  T vv2 = 0;
  this->host_m_2.Init(this->m_nNumber_elements, vv2);

  return CL_SUCCESS;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::Check_GetMemObjectInfo(cl_mem_flags buffer_mem_flag)
{
  cl_int err = CL_SUCCESS;
  cBuffer_checker<T>::Check_GetMemObjectInfo(buffer_mem_flag);

  if (buffer_mem_flag & CL_MEM_ALLOC_HOST_PTR)
  {
    size_t size = 0;
    err = clGetMemObjectInfo(this->m_buffer, CL_MEM_SIZE, sizeof(size), &size, NULL);
    void *pp = NULL;
    err = clGetMemObjectInfo(this->m_buffer, CL_MEM_HOST_PTR, sizeof( pp ), &pp, NULL);

    if (!this->host_m_1.Equal( (T*) (this->pData), this->m_nNumber_elements )) {
      log_error("Buffer data difference found\n");
      return FAILURE;
    }
  }

  return err;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::verifyData( cl_int err, cl_event  &   event )
{
  if (err != CL_SUCCESS) {
    err = this->m_nERROR_RETURN_CODE;
    test_error(err, "clEnqueueReadBuffer error");
  }

  if (!this->host_m_1.Equal(this->host_m_2)) {
    err = this->m_nERROR_RETURN_CODE;
    test_error(err, "clEnqueueReadBuffer data difference found");
  }

  return err;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer()
{
  cl_event event;
  cl_int err = CL_SUCCESS;

  err = clEnqueueReadBuffer(this->m_queue, this->m_buffer, this->m_blocking,
                            0, this->get_block_size_bytes(), this->host_m_2.pData,
                            0, NULL, &event);
  test_error(err, "clEnqueueReadBuffer error");

  if (!this->m_blocking) {
    err = clWaitForEvents(1, &event);
    test_error(err, "clWaitForEvents error");
  }

  if (!this->host_m_1.Equal(this->host_m_2)) {
    log_error("Buffer data difference found\n");
    return FAILURE;
  }
  err = clReleaseEvent(event);
  test_error(err, "clReleaseEvent error");

  // test write
  err = clEnqueueWriteBuffer(this->m_queue, this->m_buffer, this->m_blocking,
                             0, this->get_block_size_bytes(), this->host_m_2.pData,
                             0, NULL, &event);

  if (err == CL_SUCCESS) {
    log_error("Calling clEnqueueWriteBuffer on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
    err = FAILURE;
    return FAILURE;

  } else {
    log_info("Test succeeded\n\n");
    err = CL_SUCCESS;
  }

  return err;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer_rect()
{
  this->Init_rect();

  T vv2 = 0;
  this->host_m_2.Set_to( vv2 );
  cl_event event;
  cl_int err = CL_SUCCESS;

  err = clEnqueueReadBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
                                this->buffer_origin_bytes,
                                this->host_origin_bytes,
                                this->region_bytes,
                                this->buffer_row_pitch_bytes,
                                this->buffer_slice_pitch_bytes,
                                this->host_row_pitch_bytes,
                                this->host_slice_pitch_bytes,
                                this->host_m_2.pData,
                                0, NULL, &event);
  test_error(err, "clEnqueueReadBufferRect error");

  if (!this->m_blocking) {
    err = clWaitForEvents(1, &event);
    test_error(err, "clWaitForEvents error");
  }

  if (! this->host_m_1.Equal_rect(this->host_m_2, this->host_origin, this->region,
                                  this->host_row_pitch, this->host_slice_pitch)) {
    log_error("Buffer data diffeence found\n");
    return FAILURE;
  }
  err = clReleaseEvent(event);
  test_error(err, "clReleaseEvent error");

  // test blocking write rect
  err = clEnqueueWriteBufferRect(this->m_queue, this->m_buffer, this->m_blocking,
                                 this->buffer_origin_bytes,
                                 this->host_origin_bytes,
                                 this->region_bytes,
                                 this->buffer_row_pitch_bytes,
                                 this->buffer_slice_pitch_bytes,
                                 this->host_row_pitch_bytes,
                                 this->host_slice_pitch_bytes,
                                 this->host_m_2.pData,
                                 0, NULL, &event);

  if (err == CL_SUCCESS) {
    log_error("Calling clEnqueueWriteBufferRect on a memory object created with the CL_MEM_HOST_READ_ONLY flag should not return CL_SUCCESS\n");
    err = FAILURE;
    return FAILURE;

  } else {
    log_info("Test succeeded\n\n");
    err = CL_SUCCESS;
  }

  return err;
}

template < class T >
cl_int cBuffer_check_mem_host_read_only< T >::verify_RW_Buffer_mapping()
{
  cl_int err = CL_SUCCESS;
  cl_event event;
  void *dataPtr;
  dataPtr = clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
                               CL_MAP_READ,
                               0, this->get_block_size_bytes(),
                               0, NULL, &event, &err);
  test_error(err, "clEnqueueMapBuffer error");

  if (!this->m_blocking) {
    err = clWaitForEvents(1, &event );
    test_error(err, "clWaitForEvents error");
  }

  if ((this->buffer_mem_flag & CL_MEM_USE_HOST_PTR) && dataPtr != this->pHost_ptr ) {
    log_error("Mapped host pointer difference found\n");
    return FAILURE;
  }

  if(!this->host_m_1.Equal((T*)dataPtr, this->m_nNumber_elements)) {
    log_error("Buffer content difference found\n");
    return FAILURE;
  }

  err = clReleaseEvent(event);
  test_error(err, "clReleaseEvent error");

  err = clEnqueueUnmapMemObject(this->m_queue, this->m_buffer, dataPtr, 0,
                                nullptr, nullptr);
  test_error(err, "clEnqueueUnmapMemObject error");

  //  test blocking map read
  clEnqueueMapBuffer(this->m_queue, this->m_buffer, this->m_blocking,
                     CL_MAP_WRITE,
                     0, this->get_block_size_bytes(),
                     0, NULL, &event, &err);

  if (err == CL_SUCCESS) {
    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");
    err = FAILURE;
    return FAILURE;

  } else {
    log_info("Test succeeded\n\n");
    err = CL_SUCCESS;
  }

  return err;
}

#endif