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