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_flags)(CL_MEM_READ_WRITE), sizeof(cl_float) * num_elements, NULL, NULL);
73 if (!streams[0])
74 {
75 log_error("clCreateBuffer failed\n");
76 return -1;
77 }
78 streams[1] = clCreateBuffer(context, (cl_mem_flags)(CL_MEM_READ_WRITE), sizeof(cl_int) * num_elements, NULL, NULL);
79 if (!streams[1])
80 {
81 log_error("clCreateBuffer failed\n");
82 return -1;
83 }
84
85 d = init_genrand( gRandomSeed );
86 for (i=0; i<num_elements; i++)
87 input_ptr[i] = get_random_float(-MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), MAKE_HEX_FLOAT( 0x1.0p31f, 0x1, 31), d);
88 free_mtdata(d); d = NULL;
89
90 err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, sizeof(cl_float)*num_elements, (void *)input_ptr, 0, NULL, NULL);
91 if (err != CL_SUCCESS)
92 {
93 log_error("clWriteArray failed\n");
94 return -1;
95 }
96
97 err = create_single_kernel_helper(context, &program, &kernel, 1, &float2int_kernel_code, "test_float2int");
98 if (err != CL_SUCCESS)
99 {
100 log_error("create_single_kernel_helper failed\n");
101 return -1;
102 }
103
104 values[0] = streams[0];
105 values[1] = streams[1];
106 err = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
107 err = clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
108 if (err != CL_SUCCESS)
109 {
110 log_error("clSetKernelArgs failed\n");
111 return -1;
112 }
113
114 threads[0] = (size_t)num_elements;
115 err = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL );
116 if (err != CL_SUCCESS)
117 {
118 log_error("clEnqueueNDRangeKernel failed\n");
119 return -1;
120 }
121
122 err = clEnqueueReadBuffer( queue, streams[1], true, 0, sizeof(cl_int)*num_elements, (void *)output_ptr, 0, NULL, NULL );
123 if (err != CL_SUCCESS)
124 {
125 log_error("clEnqueueReadBuffer failed\n");
126 return -1;
127 }
128
129 err = verify_float2int(input_ptr, output_ptr, num_elements);
130
131 // cleanup
132 clReleaseMemObject(streams[0]);
133 clReleaseMemObject(streams[1]);
134 clReleaseKernel(kernel);
135 clReleaseProgram(program);
136 free(input_ptr);
137 free(output_ptr);
138
139 return err;
140 }
141
142
143
144
145
146