improve clGetExtensionFunctionAddress{,ForPlatform}
[ocl-icd] / ocl_test.c
1 /**
2 Copyright (c) 2012, Brice Videau <brice.videau@imag.fr>
3 All rights reserved.
4       
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     
8 1. Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13         
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <CL/opencl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 void print_error(cl_int error) {
32   switch (error) {
33   case CL_SUCCESS: break ;
34   case CL_PLATFORM_NOT_FOUND_KHR:
35     printf("No platforms found!\n");
36     break;
37   case CL_INVALID_PLATFORM:
38     printf("Invalid platform\n");
39     break;
40   default:
41      printf("OpenCL error: %i\n", error);
42   }
43 }
44
45 void show_platform(cl_platform_id pid, int show_extensions) {
46   char *platform_vendor;
47   size_t param_value_size_ret;
48   cl_int error;
49
50   error = clGetPlatformInfo(pid, CL_PLATFORM_VENDOR, 0, NULL, &param_value_size_ret );
51   print_error(error);
52   if (error != CL_SUCCESS)
53     return;
54     
55   platform_vendor = (char *)malloc(param_value_size_ret);
56   clGetPlatformInfo(pid, CL_PLATFORM_VENDOR, param_value_size_ret, platform_vendor, NULL );
57
58   printf("%s\n",platform_vendor);
59   free(platform_vendor);
60
61   if (show_extensions) {  
62     error = clGetPlatformInfo(pid, CL_PLATFORM_EXTENSIONS, 0, NULL, &param_value_size_ret );
63     print_error(error);
64     
65     platform_vendor = (char *)malloc(param_value_size_ret);
66     clGetPlatformInfo(pid, CL_PLATFORM_EXTENSIONS, param_value_size_ret, platform_vendor, NULL );
67
68     printf("Extensions: %s\n",platform_vendor);
69     free(platform_vendor);
70   }
71 }
72
73 int main(int argc, char* argv[]) {
74   cl_platform_id *platforms;
75   cl_uint num_platforms;
76   cl_int error;
77
78   int show_extensions=0;
79   if (argc >= 2 && strcmp(argv[1], "--show-extensions")==0) {
80     show_extensions=1;
81     argv++;
82   }
83
84   int default_platform=0;
85   if (argc >= 2 && strcmp(argv[1], "--default-platform")==0) {
86     default_platform=1;
87   }
88
89   error = clGetPlatformIDs(0, NULL, &num_platforms);
90   if( error == CL_SUCCESS ) {
91     printf("Found %u platforms!\n", num_platforms);
92   } else if( error == CL_PLATFORM_NOT_FOUND_KHR ) {
93     print_error(error);
94     if (default_platform) {
95       show_platform(NULL, show_extensions);
96     }
97     exit(0);
98   } else {
99     print_error(error);
100     exit(-1);
101   }
102   platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id *) * num_platforms);
103   error = clGetPlatformIDs(num_platforms, platforms, NULL);
104   print_error(error);
105   cl_uint i;
106   for(i=0; i<num_platforms; i++){
107     show_platform(platforms[i], show_extensions);
108   }
109   if (default_platform) {
110     show_platform(NULL, show_extensions);
111   }
112   return 0;
113 }