1
1
//
2
- // Copyright (c) 2022 The Khronos Group Inc.
2
+ // Copyright (c) 2024 The Khronos Group Inc.
3
3
//
4
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
5
// you may not use this file except in compliance with the License.
@@ -40,13 +40,10 @@ const VulkanInstance &getVulkanInstance()
40
40
41
41
const VulkanPhysicalDevice &getVulkanPhysicalDevice ()
42
42
{
43
- size_t pdIdx;
43
+ size_t pdIdx = 0 ;
44
44
cl_int errNum = 0 ;
45
- cl_platform_id platform = NULL ;
45
+ cl_platform_id platform = nullptr ;
46
46
cl_uchar uuid[CL_UUID_SIZE_KHR];
47
- cl_device_id *devices;
48
- char *extensions = NULL ;
49
- size_t extensionSize = 0 ;
50
47
cl_uint num_devices = 0 ;
51
48
cl_uint device_no = 0 ;
52
49
const size_t bufsize = BUFFERSIZE;
@@ -69,49 +66,24 @@ const VulkanPhysicalDevice &getVulkanPhysicalDevice()
69
66
throw std::runtime_error (
70
67
" Error: clGetDeviceIDs failed in returning of devices\n " );
71
68
}
72
- devices = (cl_device_id *)malloc (num_devices * sizeof (cl_device_id));
73
- if (NULL == devices)
74
- {
75
- throw std::runtime_error (
76
- " Error: Unable to allocate memory for devices\n " );
77
- }
78
- errNum = clGetDeviceIDs (platform, CL_DEVICE_TYPE_GPU, num_devices, devices,
79
- NULL );
69
+ std::vector<cl_device_id> devices (num_devices);
70
+ errNum = clGetDeviceIDs (platform, CL_DEVICE_TYPE_GPU, num_devices,
71
+ devices.data (), NULL );
80
72
if (CL_SUCCESS != errNum)
81
73
{
82
74
throw std::runtime_error (" Error: Failed to get deviceID.\n " );
83
75
}
84
76
bool is_selected = false ;
85
77
for (device_no = 0 ; device_no < num_devices; device_no++)
86
78
{
87
- errNum = clGetDeviceInfo (devices[device_no], CL_DEVICE_EXTENSIONS, 0 ,
88
- NULL , &extensionSize);
89
- if (CL_SUCCESS != errNum)
90
- {
91
- throw std::runtime_error (" Error in clGetDeviceInfo for getting "
92
- " device_extension size....\n " );
93
- }
94
- extensions = (char *)malloc (extensionSize);
95
- if (NULL == extensions)
96
- {
97
- throw std::runtime_error (
98
- " Unable to allocate memory for extensions\n " );
99
- }
100
- errNum = clGetDeviceInfo (devices[device_no], CL_DEVICE_EXTENSIONS,
101
- extensionSize, extensions, NULL );
102
- if (CL_SUCCESS != errNum)
103
- {
104
- throw std::runtime_error (" Error: Error in clGetDeviceInfo for "
105
- " getting device_extension\n " );
106
- }
107
79
errNum = clGetDeviceInfo (devices[device_no], CL_DEVICE_UUID_KHR,
108
- CL_UUID_SIZE_KHR, uuid, &extensionSize );
80
+ CL_UUID_SIZE_KHR, uuid, nullptr );
109
81
if (CL_SUCCESS != errNum)
110
82
{
111
83
throw std::runtime_error (
112
84
" Error: clGetDeviceInfo failed with error\n " );
113
85
}
114
- free (extensions);
86
+
115
87
for (pdIdx = 0 ; pdIdx < physicalDeviceList.size (); pdIdx++)
116
88
{
117
89
if (!memcmp (&uuid, physicalDeviceList[pdIdx].getUUID (),
@@ -139,10 +111,48 @@ const VulkanPhysicalDevice &getVulkanPhysicalDevice()
139
111
return physicalDeviceList[pdIdx];
140
112
}
141
113
142
- const VulkanQueueFamily &getVulkanQueueFamily (uint32_t queueFlags)
114
+ const VulkanPhysicalDevice &
115
+ getAssociatedVulkanPhysicalDevice (cl_device_id deviceId)
116
+ {
117
+ size_t pdIdx;
118
+ cl_int errNum = 0 ;
119
+ cl_uchar uuid[CL_UUID_SIZE_KHR];
120
+ const VulkanInstance &instance = getVulkanInstance ();
121
+ const VulkanPhysicalDeviceList &physicalDeviceList =
122
+ instance.getPhysicalDeviceList ();
123
+
124
+ errNum = clGetDeviceInfo (deviceId, CL_DEVICE_UUID_KHR, CL_UUID_SIZE_KHR,
125
+ uuid, nullptr );
126
+ if (CL_SUCCESS != errNum)
127
+ {
128
+ throw std::runtime_error (" Error: clGetDeviceInfo failed with error\n " );
129
+ }
130
+ for (pdIdx = 0 ; pdIdx < physicalDeviceList.size (); pdIdx++)
131
+ {
132
+ if (!memcmp (&uuid, physicalDeviceList[pdIdx].getUUID (), VK_UUID_SIZE))
133
+ {
134
+ std::cout << " Selected physical device = "
135
+ << physicalDeviceList[pdIdx] << std::endl;
136
+ break ;
137
+ }
138
+ }
139
+
140
+ if ((pdIdx >= physicalDeviceList.size ())
141
+ || (physicalDeviceList[pdIdx] == (VkPhysicalDevice)VK_NULL_HANDLE))
142
+ {
143
+ throw std::runtime_error (" failed to find a suitable GPU!" );
144
+ }
145
+ std::cout << " Selected physical device is: " << physicalDeviceList[pdIdx]
146
+ << std::endl;
147
+ return physicalDeviceList[pdIdx];
148
+ }
149
+
150
+
151
+ const VulkanQueueFamily &
152
+ getVulkanQueueFamily (const VulkanPhysicalDevice &physicalDevice,
153
+ uint32_t queueFlags)
143
154
{
144
155
size_t qfIdx;
145
- const VulkanPhysicalDevice &physicalDevice = getVulkanPhysicalDevice ();
146
156
const VulkanQueueFamilyList &queueFamilyList =
147
157
physicalDevice.getQueueFamilyList ();
148
158
0 commit comments