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