1 /* OpenCL runtime library: clGetDeviceIDs()
2 
3    Copyright (c) 2011 Kalle Raiskila
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 "pocl_cl.h"
25 #include "devices/devices.h"
26 #include <string.h>
27 
28 CL_API_ENTRY cl_int CL_API_CALL
POname(clGetDeviceIDs)29 POname(clGetDeviceIDs)(cl_platform_id   platform,
30                cl_device_type   device_type,
31                cl_uint          num_entries,
32                cl_device_id *   devices,
33                cl_uint *        num_devices) CL_API_SUFFIX__VERSION_1_0
34 {
35   int total_num = 0;
36   int devices_added = 0;
37   cl_platform_id tmp_platform;
38 
39   /* TODO: OpenCL API specification allows implementation dependent
40      behaviour if platform == NULL. Should we just allow it? */
41   POCL_RETURN_ERROR_COND((platform == NULL), CL_INVALID_PLATFORM);
42 
43   POCL_RETURN_ERROR_COND((num_entries == 0 && devices != NULL), CL_INVALID_VALUE);
44   POCL_RETURN_ERROR_COND((num_devices == NULL && devices == NULL), CL_INVALID_VALUE);
45 
46   POname (clGetPlatformIDs) (1, &tmp_platform, NULL);
47   POCL_RETURN_ERROR_ON ((platform != tmp_platform), CL_INVALID_PLATFORM,
48                         "Can only return devices from the POCL platform\n");
49 
50   int err = pocl_init_devices();
51   if (err)
52     return err;
53 
54   total_num = pocl_get_device_type_count(device_type);
55 
56   if (total_num == 0)
57       return CL_DEVICE_NOT_FOUND;
58 
59   if (devices != NULL)
60     devices_added = pocl_get_devices(device_type, devices, num_entries);
61 
62   if (num_devices != NULL)
63     *num_devices = total_num;
64 
65   if (devices_added > 0 || num_entries == 0)
66     return CL_SUCCESS;
67   else
68     return CL_DEVICE_NOT_FOUND;
69 }
70 POsym(clGetDeviceIDs)
71