xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/basic/test_arrayreadwrite.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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 
25 #include "procs.h"
26 
27 
28 int
test_arrayreadwrite(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)29 test_arrayreadwrite(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
30 {
31     cl_uint                *inptr, *outptr;
32     cl_mem              streams[1];
33     int                 num_tries = 400;
34     num_elements = 1024 * 1024 * 4;
35     int                 i, j, err;
36     MTdata              d;
37 
38     inptr = (cl_uint*)malloc(num_elements*sizeof(cl_uint));
39     outptr = (cl_uint*)malloc(num_elements*sizeof(cl_uint));
40 
41     // randomize data
42     d = init_genrand( gRandomSeed );
43     for (i=0; i<num_elements; i++)
44         inptr[i] = (cl_uint)(genrand_int32(d) & 0x7FFFFFFF);
45 
46     streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
47                                 sizeof(cl_uint) * num_elements, NULL, &err);
48     test_error(err, "clCreateBuffer failed");
49 
50     for (i=0; i<num_tries; i++)
51     {
52         int        offset;
53         int        cb;
54 
55         do {
56             offset = (int)(genrand_int32(d) & 0x7FFFFFFF);
57             if (offset > 0 && offset < num_elements)
58                 break;
59         } while (1);
60         cb = (int)(genrand_int32(d) & 0x7FFFFFFF);
61         if (cb > (num_elements - offset))
62             cb = num_elements - offset;
63 
64         err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), sizeof(cl_uint)*cb,&inptr[offset], 0, NULL, NULL);
65         test_error(err, "clEnqueueWriteBuffer failed");
66 
67         err = clEnqueueReadBuffer( queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), cb*sizeof(cl_uint), &outptr[offset], 0, NULL, NULL );
68         test_error(err, "clEnqueueReadBuffer failed");
69 
70         for (j=offset; j<offset+cb; j++)
71         {
72             if (inptr[j] != outptr[j])
73             {
74                 log_error("ARRAY read, write test failed\n");
75                 err = -1;
76                 break;
77             }
78         }
79 
80         if (err)
81             break;
82     }
83 
84     free_mtdata(d);
85     clReleaseMemObject(streams[0]);
86     free(inptr);
87     free(outptr);
88 
89     if (!err)
90         log_info("ARRAY read, write test passed\n");
91 
92     return err;
93 }
94 
95 
96 
97