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 const char *float2int_kernel_code =
28 "__kernel void test_float2int(__global float *src, __global int *dst)\n"
29 "{\n"
30 " int tid = get_global_id(0);\n"
31 "\n"
32 " dst[tid] = (int)src[tid];\n"
33 "\n"
34 "}\n";
35
36
37 int
verify_float2int(cl_float * inptr,cl_int * outptr,int n)38 verify_float2int(cl_float *inptr, cl_int *outptr, int n)
39 {
40 int i;
41
42 for (i=0; i<n; i++)
43 {
44 if (outptr[i] != (int)inptr[i])
45 {
46 log_error("FLOAT2INT test failed\n");
47 return -1;
48 }
49 }
50
51 log_info("FLOAT2INT test passed\n");
52 return 0;
53 }
54
55
56 int
test_float2int(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)57 test_float2int(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
58 {
59 cl_mem streams[2];
60 cl_float *input_ptr;
61 cl_int *output_ptr;
62 cl_program program;
63 cl_kernel kernel;
64 void *values[2];
65 size_t threads[1];
66 int err;
67 int i;
68 MTdata d;
69
70 input_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements);
71 output_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements);
72 streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
73 sizeof(cl_float) * num_elements, NULL, NULL);
74 if (!streams[0])
75 {
76 log_error("clCreateBuffer failed\n");
77 return -1;
78 }
79 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
80 sizeof(cl_int) * num_elements, NULL, NULL);
81 if (!streams[1])
82 {
83 log_error("clCreateBuffer failed\n");
84 return -1;
85 }
86
87 d = init_genrand( gRandomSeed );
88 for (i=0; i<num_elements; i++)
89 input_ptr[i] = get_random_float(-MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), d);
90 free_mtdata(d); d = NULL;
91
92 err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, sizeof(cl_float)*num_elements, (void *)input_ptr, 0, NULL, NULL);
93 if (err != CL_SUCCESS)
94 {
95 log_error("clWriteArray failed\n");
96 return -1;
97 }
98
99 err = create_single_kernel_helper(context, &program, &kernel, 1, &float2int_kernel_code, "test_float2int");
100 if (err != CL_SUCCESS)
101 {
102 log_error("create_single_kernel_helper failed\n");
103 return -1;
104 }
105
106 values[0] = streams[0];
107 values[1] = streams[1];
108 err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
109 err = clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
110 if (err != CL_SUCCESS)
111 {
112 log_error("clSetKernelArgs failed\n");
113 return -1;
114 }
115
116 threads[0] = (size_t)num_elements;
117 err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL );
118 if (err != CL_SUCCESS)
119 {
120 log_error("clEnqueueNDRangeKernel failed\n");
121 return -1;
122 }
123
124 err = clEnqueueReadBuffer( queue, streams[1], true, 0, sizeof(cl_int)*num_elements, (void *)output_ptr, 0, NULL, NULL );
125 if (err != CL_SUCCESS)
126 {
127 log_error("clEnqueueReadBuffer failed\n");
128 return -1;
129 }
130
131 err = verify_float2int(input_ptr, output_ptr, num_elements);
132
133 // cleanup
134 clReleaseMemObject(streams[0]);
135 clReleaseMemObject(streams[1]);
136 clReleaseKernel(kernel);
137 clReleaseProgram(program);
138 free(input_ptr);
139 free(output_ptr);
140
141 return err;
142 }
143
144
145
146
147
148