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_flags)(CL_MEM_READ_WRITE), sizeof(cl_uint) * num_elements, NULL, &err);
47 test_error(err, "clCreateBuffer failed");
48
49 for (i=0; i<num_tries; i++)
50 {
51 int offset;
52 int cb;
53
54 do {
55 offset = (int)(genrand_int32(d) & 0x7FFFFFFF);
56 if (offset > 0 && offset < num_elements)
57 break;
58 } while (1);
59 cb = (int)(genrand_int32(d) & 0x7FFFFFFF);
60 if (cb > (num_elements - offset))
61 cb = num_elements - offset;
62
63 err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), sizeof(cl_uint)*cb,&inptr[offset], 0, NULL, NULL);
64 test_error(err, "clEnqueueWriteBuffer failed");
65
66 err = clEnqueueReadBuffer( queue, streams[0], CL_TRUE, offset*sizeof(cl_uint), cb*sizeof(cl_uint), &outptr[offset], 0, NULL, NULL );
67 test_error(err, "clEnqueueReadBuffer failed");
68
69 for (j=offset; j<offset+cb; j++)
70 {
71 if (inptr[j] != outptr[j])
72 {
73 log_error("ARRAY read, write test failed\n");
74 err = -1;
75 break;
76 }
77 }
78
79 if (err)
80 break;
81 }
82
83 free_mtdata(d);
84 clReleaseMemObject(streams[0]);
85 free(inptr);
86 free(outptr);
87
88 if (!err)
89 log_info("ARRAY read, write test passed\n");
90
91 return err;
92 }
93
94
95
96