• 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 #include "harness/compat.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include "procs.h"
25 
26 #ifndef uchar
27 typedef unsigned char uchar;
28 #endif
29 
30 
31 const char *mem_read_write_kernel_code =
32 "__kernel void test_mem_read_write(__global int *dst)\n"
33 "{\n"
34 "    int  tid = get_global_id(0);\n"
35 "\n"
36 "    dst[tid] = dst[tid]+1;\n"
37 "}\n";
38 
39 const char *mem_read_kernel_code =
40     "__kernel void test_mem_read(__global int *dst, __global int *src)\n"
41     "{\n"
42     "    int  tid = get_global_id(0);\n"
43     "\n"
44     "    dst[tid] = src[tid]+1;\n"
45     "}\n";
46 
47 const char *mem_write_kernel_code =
48 "__kernel void test_mem_write(__global int *dst)\n"
49 "{\n"
50 "    int  tid = get_global_id(0);\n"
51 "\n"
52 "    dst[tid] = dst[tid]+1;\n"
53 "}\n";
54 
55 
verify_mem(int * outptr,int n)56 static int verify_mem( int *outptr, int n )
57 {
58     int i;
59 
60     for ( i = 0; i < n; i++ ){
61         if ( outptr[i] != ( i + 1 ) )
62             return -1;
63     }
64 
65     return 0;
66 }
67 
68 
test_mem_flags(cl_context context,cl_command_queue queue,int num_elements,cl_mem_flags flags,const char ** kernel_program,const char * kernel_name)69 int test_mem_flags(cl_context context, cl_command_queue queue, int num_elements,
70                    cl_mem_flags flags, const char **kernel_program,
71                    const char *kernel_name)
72 {
73     clMemWrapper buffers[2];
74     cl_int      *inptr, *outptr;
75     clProgramWrapper program;
76     clKernelWrapper kernel;
77     size_t      global_work_size[3];
78     cl_int      err;
79     int         i;
80 
81     size_t      min_alignment = get_min_alignment(context);
82     bool test_read_only = (flags & CL_MEM_READ_ONLY) != 0;
83     bool test_write_only = (flags & CL_MEM_WRITE_ONLY) != 0;
84     bool copy_host_ptr = (flags & CL_MEM_COPY_HOST_PTR) != 0;
85 
86     global_work_size[0] = (cl_uint)num_elements;
87 
88     inptr = (cl_int*)align_malloc(sizeof(cl_int)  * num_elements, min_alignment);
89     if (!inptr)
90     {
91         log_error(" unable to allocate %d bytes of memory\n",
92                   (int)sizeof(cl_int) * num_elements);
93         return -1;
94     }
95     outptr = (cl_int*)align_malloc(sizeof(cl_int) * num_elements, min_alignment);
96     if (!outptr)
97     {
98         log_error(" unable to allocate %d bytes of memory\n",
99                   (int)sizeof(cl_int) * num_elements);
100         align_free((void *)inptr);
101         return -1;
102     }
103 
104     for (i = 0; i < num_elements; i++) inptr[i] = i;
105 
106     buffers[0] = clCreateBuffer(context, flags, sizeof(cl_int) * num_elements,
107                                 copy_host_ptr ? inptr : NULL, &err);
108     if (err != CL_SUCCESS)
109     {
110         print_error(err, "clCreateBuffer failed");
111         align_free((void *)outptr);
112         align_free((void *)inptr);
113         return -1;
114     }
115     if (!copy_host_ptr)
116     {
117         err = clEnqueueWriteBuffer(queue, buffers[0], CL_TRUE, 0,
118                                    sizeof(cl_int) * num_elements, (void *)inptr,
119                                    0, NULL, NULL);
120         if (err != CL_SUCCESS)
121         {
122             print_error(err, "clEnqueueWriteBuffer failed");
123             align_free((void *)outptr);
124             align_free((void *)inptr);
125             return -1;
126         }
127     }
128 
129     if (test_read_only)
130     {
131         /* The read only buffer for mem_read_only_flags should be created above
132         with the correct flags as in other tests. However to make later test
133         code simpler, the additional read_write buffer required is stored as
134         the first buffer */
135         buffers[1] = buffers[0];
136         buffers[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
137                                     sizeof(cl_int) * num_elements, NULL, &err);
138         if (err != CL_SUCCESS)
139         {
140             print_error(err, " clCreateBuffer failed \n");
141             align_free((void *)inptr);
142             align_free((void *)outptr);
143             return -1;
144         }
145     }
146 
147     err = create_single_kernel_helper(context, &program, &kernel, 1,
148                                       kernel_program, kernel_name);
149     if (err){
150         print_error(err, "creating kernel failed");
151         align_free( (void *)outptr );
152         align_free( (void *)inptr );
153         return -1;
154     }
155 
156     err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&buffers[0]);
157     if (test_read_only && (err == CL_SUCCESS))
158     {
159         err = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&buffers[1]);
160     }
161     if ( err != CL_SUCCESS ){
162         print_error( err, "clSetKernelArg failed" );
163         align_free( (void *)outptr );
164         align_free( (void *)inptr );
165         return -1;
166     }
167 
168     err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, NULL,
169                                  0, NULL, NULL);
170     if (err != CL_SUCCESS){
171         log_error("clEnqueueNDRangeKernel failed\n");
172         align_free( (void *)outptr );
173         align_free( (void *)inptr );
174         return -1;
175     }
176 
177     err = clEnqueueReadBuffer(queue, buffers[0], true, 0,
178                               sizeof(cl_int) * num_elements, (void *)outptr, 0,
179                               NULL, NULL);
180     if ( err != CL_SUCCESS ){
181         print_error( err, "clEnqueueReadBuffer failed" );
182         align_free( (void *)outptr );
183         align_free( (void *)inptr );
184         return -1;
185     }
186 
187     if (!test_write_only)
188     {
189         if (verify_mem(outptr, num_elements))
190         {
191             log_error("test failed\n");
192             err = -1;
193         }
194         else
195         {
196             log_info("test passed\n");
197             err = 0;
198         }
199     }
200 
201     // cleanup
202     align_free( (void *)outptr );
203     align_free( (void *)inptr );
204 
205     return err;
206 } // end test_mem_flags()
207 
test_mem_read_write_flags(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)208 int test_mem_read_write_flags(cl_device_id deviceID, cl_context context,
209                               cl_command_queue queue, int num_elements)
210 {
211     return test_mem_flags(context, queue, num_elements, CL_MEM_READ_WRITE,
212                           &mem_read_write_kernel_code, "test_mem_read_write");
213 }
214 
215 
test_mem_write_only_flags(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)216 int test_mem_write_only_flags(cl_device_id deviceID, cl_context context,
217                               cl_command_queue queue, int num_elements)
218 {
219     return test_mem_flags(context, queue, num_elements, CL_MEM_WRITE_ONLY,
220                           &mem_write_kernel_code, "test_mem_write");
221 }
222 
223 
test_mem_read_only_flags(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)224 int test_mem_read_only_flags( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
225 {
226     return test_mem_flags(context, queue, num_elements, CL_MEM_READ_ONLY,
227                           &mem_read_kernel_code, "test_mem_read");
228 }
229 
230 
test_mem_copy_host_flags(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)231 int test_mem_copy_host_flags( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
232 {
233     return test_mem_flags(context, queue, num_elements,
234                           CL_MEM_COPY_HOST_PTR | CL_MEM_READ_WRITE,
235                           &mem_read_write_kernel_code, "test_mem_read_write");
236 }
237 
test_mem_alloc_ref_flags(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)238 int test_mem_alloc_ref_flags(cl_device_id deviceID, cl_context context,
239                              cl_command_queue queue, int num_elements)
240 {
241     return test_mem_flags(context, queue, num_elements,
242                           CL_MEM_ALLOC_HOST_PTR | CL_MEM_READ_WRITE,
243                           &mem_read_write_kernel_code, "test_mem_read_write");
244 }
245