1 /* trig - Trigonometric functions.
2 
3    Copyright (c) 2011 Universidad Rey Juan Carlos
4 
5    Permission is hereby granted, free of charge, to any person obtaining a copy
6    of this software and associated documentation files (the "Software"), to deal
7    in the Software without restriction, including without limitation the rights
8    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9    copies of the Software, and to permit persons to whom the Software is
10    furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21    THE SOFTWARE.
22 */
23 
24 #include <assert.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <CL/opencl.h>
29 
30 #define N 8
31 
32 extern int exec_trig_kernel (const char *program_source,
33                              int n, void *srcA, void *dst);
34 
35 int
main(void)36 main (void)
37 {
38   FILE *source_file;
39   char *source;
40   int source_size;
41   cl_float4 *srcA;
42   cl_float4 *dst;
43   cl_float *dstS;
44   int i;
45 
46   source_file = fopen("trig.cl", "r");
47   if (source_file == NULL)
48     source_file = fopen (SRCDIR "/trig.cl", "r");
49 
50   assert(source_file != NULL && "trig.cl not found!");
51 
52   fseek (source_file, 0, SEEK_END);
53   source_size = ftell (source_file);
54   fseek (source_file, 0, SEEK_SET);
55 
56   source = (char *) malloc (source_size + 1);
57   assert (source != NULL);
58 
59   fread (source, source_size, 1, source_file);
60   source[source_size] = '\0';
61 
62   fclose (source_file);
63 
64   srcA = (cl_float4 *) malloc (N * sizeof (cl_float4));
65   dst = (cl_float4 *) malloc (N * sizeof (cl_float4));
66   dstS = (cl_float *) malloc (N * sizeof (cl_float));
67 
68   for (i = 0; i < N; ++i)
69     {
70       srcA[i].s[0] = (cl_float)i;
71       srcA[i].s[1] = (cl_float)i;
72       srcA[i].s[2] = (cl_float)i;
73       srcA[i].s[3] = (cl_float)i;
74       switch (i % 5) {
75       case 0: dstS[i] = cosf((float)i); break;
76       case 1: dstS[i] = fabsf((float)i) + 7.3f; break;
77       case 2: dstS[i] = sinf((float)i); break;
78       case 3: dstS[i] = sqrtf((float)i); break;
79       case 4: dstS[i] = tanf((float)i); break;
80       }
81     }
82 
83   if (exec_trig_kernel (source, N, srcA, dst) < 0)
84     {
85       printf("Failed to run the kernel.\n");
86       return -1;
87     }
88 
89   for (i = 0; i < N; ++i)
90     {
91       if (fabsf(dst[i].s[0] - dstS[i]) > 1.0e-6f ||
92           fabsf(dst[i].s[1] - dstS[i]) > 1.0e-6f ||
93           fabsf(dst[i].s[2] - dstS[i]) > 1.0e-6f ||
94           fabsf(dst[i].s[3] - dstS[i]) > 1.0e-6f)
95   {
96           printf ("input:    [%.7f, %.7f, %.7f, %.7f]\n"
97                   "output:   [%.7f, %.7f, %.7f, %.7f]\n"
98                   "expected: [%.7f, %.7f, %.7f, %.7f]\n",
99                   srcA[i].s[0], srcA[i].s[1], srcA[i].s[2], srcA[i].s[3],
100                   dst[i].s[0], dst[i].s[1], dst[i].s[2], dst[i].s[3],
101                   dstS[i], dstS[i], dstS[i], dstS[i]);
102     printf ("FAIL\n");
103     return -1;
104   }
105     }
106 
107   printf ("OK\n");
108 
109   free (srcA);
110   free (dst);
111   free (dstS);
112 
113   return 0;
114 }
115