-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpenCLModule.cpp
executable file
·323 lines (274 loc) · 8.22 KB
/
OpenCLModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//==============================================================================
// Copyright (c) 2012-2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools
/// \file
/// \brief This class manages the dynamic loading of OpenCL.
//==============================================================================
#include <string>
#include <vector>
#include <iostream>
#include "OpenCLModule.h"
#if _WIN32
const char* OpenCLModule::s_DefaultModuleName = "OpenCL.dll";
#else
const char* OpenCLModule::s_DefaultModuleName = "libOpenCL.so";
#endif
OpenCLModule::OpenCLModule(const std::string& moduleName)
{
Initialize();
m_openCLVersion = LoadModule(moduleName);
#ifndef _WIN32
// Bug 8385 - The automatic installer of Catalyst 12.1 on CentOS6
// installs libOpenCL.so.1, but does not install a symbolic link to libOpenCL.so
// It does not appear to be possible to dynamically construct a vector
// inside the namespace declaration where the dispatch table is constructed
// in the DebugAPI code, so we have to do that here, and I was not sure how to
// modify that code accordingly
if ((moduleName == OpenCLModule::s_DefaultModuleName)
&& (OpenCLLoaded() == OpenCL_None))
{
std::vector<std::string> vMods;
// Generic first, followed in descending order by most-recent
// a .2 version does not yet exist
vMods.push_back("libOpenCL.so");
vMods.push_back("libOpenCL.so.2");
vMods.push_back("libOpenCL.so.1");
m_openCLVersion = LoadModule(vMods);
// But if this fails, we have no fallback...
}
#endif
}
OpenCLModule::OpenCLModule(const std::vector<std::string>& names)
{
Initialize();
LoadModule(names);
}
OpenCLModule::~OpenCLModule()
{
UnloadModule();
}
OpenCLModule::OpenCLVersion
OpenCLModule::LoadModule(const std::string& moduleName)
{
if (m_DynamicLibraryHelper.LoadModule(moduleName))
{
#define MAKE_STRING(s) #s
#define X(SYM) SYM = (cl##SYM##_fn) m_DynamicLibraryHelper.GetProcAddress("cl" MAKE_STRING(SYM));
OPENCL10_API_TABLE;
// Even though I never expect to find this one, try to load it anyway.
OPENCL10_API_TABLE_SPECIAL;
D3D10_KHR_RESERVED_TABLE;
OPENCL11_API_TABLE;
DEVICE_FISSION_EXT_RESERVE_DTABLE;
CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
OPENCL12_API_TABLE;
// Skip _reservedD3DExtensions
// Skip _reservedEGLExtensions
OPENCL20_API_TABLE;
OPENCL21_API_TABLE;
OPENCL22_API_TABLE;
#undef X
#undef MAKE_STRING
}
return OpenCLLoaded();
}
// The vector version
OpenCLModule::OpenCLVersion
OpenCLModule::LoadModule(const std::vector<std::string>& names)
{
OpenCLVersion loadEnum = OpenCL_None;
for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); it++)
{
loadEnum = LoadModule(*it);
if (loadEnum != OpenCL_None)
{
return loadEnum;
}
}
return loadEnum;
}
void
OpenCLModule::UnloadModule()
{
m_DynamicLibraryHelper.UnloadModule();
Initialize();
}
OpenCLModule::OpenCLVersion
OpenCLModule::OpenCLLoaded()
{
OpenCLVersion moduleLoaded = OpenCL_None;
#define X(SYM) && SYM != NULL
// Note how I don't check OPENCL10_API_TABLE_SPECIAL.
if (moduleLoaded == OpenCL_None OPENCL10_API_TABLE)
{
moduleLoaded = OpenCL_1_0;
}
if (moduleLoaded == OpenCL_1_0 OPENCL11_API_TABLE)
{
moduleLoaded = OpenCL_1_1;
}
if (moduleLoaded == OpenCL_1_1 OPENCL12_API_TABLE)
{
moduleLoaded = OpenCL_1_2;
}
if (moduleLoaded == OpenCL_1_2 OPENCL20_API_TABLE)
{
moduleLoaded = OpenCL_2_0;
}
if (moduleLoaded == OpenCL_2_0 OPENCL21_API_TABLE)
{
moduleLoaded = OpenCL_2_1;
}
if (moduleLoaded == OpenCL_2_1 OPENCL22_API_TABLE)
{
moduleLoaded = OpenCL_2_2;
}
#undef X
return moduleLoaded;
}
bool OpenCLModule::LoadOpenCLExtensions(
cl_platform_id platform)
{
if ((GetExtensionFunctionAddressForPlatform == NULL) &&
(GetExtensionFunctionAddress == NULL))
{
return false;
}
#define MAKE_STRING(s) #s
#define X(SYM) \
if (GetExtensionFunctionAddressForPlatform != NULL) { \
SYM = (cl##SYM##_fn)(*GetExtensionFunctionAddressForPlatform)(platform, "cl" MAKE_STRING(SYM)); \
} else { \
SYM = (cl##SYM##_fn)(*GetExtensionFunctionAddress)("cl" MAKE_STRING(SYM)); \
}
EXTENSIONS_TABLE;
#ifdef _WIN32
WINDOWS_ONLY_EXTENSIONS_TABLE;
#endif
#undef MAKE_STRING
#undef X
return true;
}
bool
OpenCLModule::IsExtensionSupported(
OpenCLModule::OpenCLExt ext)
{
#define X(SYM) && SYM != NULL
switch (ext)
{
case OpenCL_khr_d3d10_sharing:
#if D3D10_KHR_RESERVED_TABLE_AVAILABLE
return true D3D10_KHR_RESERVED_TABLE;
#else
return false;
#endif
case OpenCL_ext_device_fission:
return true DEVICE_FISSION_EXT_RESERVE_DTABLE;
case OpenCL_khr_gl_event:
return true CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
default:
return false;
}
#undef X
}
void
OpenCLModule::GetAsCLDispatchTable(cl_icd_dispatch_table& dispatchTable)
{
#define X(SYM) dispatchTable.SYM = SYM;
#define X2(RES, SYM) dispatchTable.RES = (void *) SYM;
OPENCL10_API_TABLE;
OPENCL10_API_TABLE_SPECIAL;
D3D10_KHR_RESERVED_TABLE_2;
OPENCL11_API_TABLE;
DEVICE_FISSION_EXT_RESERVE_DTABLE_2;
CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
OPENCL12_API_TABLE;
// Skip _reservedD3DExtensions
// Skip _reservedEGLExtensions
OPENCL20_API_TABLE;
#undef X2
#undef X
#define X(SYM) dispatchTable.cl##SYM = SYM;
OPENCL21_API_TABLE;
OPENCL22_API_TABLE;
#undef X
}
void
OpenCLModule::SetFromCLDispatchTable(const cl_icd_dispatch_table& dispatchTable)
{
#define X(SYM) SYM = dispatchTable.SYM;
#define X2(RES, SYM) SYM = (cl##SYM##_fn) dispatchTable.RES;
OPENCL10_API_TABLE;
OPENCL10_API_TABLE_SPECIAL;
D3D10_KHR_RESERVED_TABLE_2;
OPENCL11_API_TABLE;
DEVICE_FISSION_EXT_RESERVE_DTABLE_2;
CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
OPENCL12_API_TABLE;
// Skip _reservedD3DExtensions
// Skip _reservedEGLExtensions
OPENCL20_API_TABLE;
#undef X
#undef X2
#define X(SYM) SYM = dispatchTable.cl##SYM;
OPENCL21_API_TABLE;
OPENCL22_API_TABLE;
#undef X
}
void OpenCLModule::Initialize()
{
#define X(SYM) SYM = NULL;
OPENCL10_API_TABLE;
OPENCL10_API_TABLE_SPECIAL;
D3D10_KHR_RESERVED_TABLE;
OPENCL11_API_TABLE;
DEVICE_FISSION_EXT_RESERVE_DTABLE;
CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
OPENCL12_API_TABLE;
_reservedD3DExtensions[0] = NULL;
_reservedD3DExtensions[1] = NULL;
_reservedD3DExtensions[2] = NULL;
_reservedD3DExtensions[3] = NULL;
_reservedD3DExtensions[4] = NULL;
_reservedD3DExtensions[5] = NULL;
_reservedD3DExtensions[6] = NULL;
_reservedD3DExtensions[7] = NULL;
_reservedD3DExtensions[8] = NULL;
_reservedD3DExtensions[9] = NULL;
_reservedEGLExtensions[0] = NULL;
_reservedEGLExtensions[1] = NULL;
_reservedEGLExtensions[2] = NULL;
_reservedEGLExtensions[3] = NULL;
OPENCL20_API_TABLE;
OPENCL21_API_TABLE;
OPENCL22_API_TABLE;
EXTENSIONS_TABLE;
#ifdef _WIN32
WINDOWS_ONLY_EXTENSIONS_TABLE;
#endif
#undef X
}
std::ostream& operator<<(std::ostream& output, const OpenCLModule& module)
{
#define MAKE_STRING(s) #s
#define X(SYM) output << MAKE_STRING( SYM ) << " = " << module.SYM << "\n";
OPENCL10_API_TABLE;
OPENCL10_API_TABLE_SPECIAL;
D3D10_KHR_RESERVED_TABLE;
OPENCL11_API_TABLE;
DEVICE_FISSION_EXT_RESERVE_DTABLE;
CREATE_EVENT_FROM_GL_SYNC_RESERVED_TABLE;
OPENCL12_API_TABLE;
// Skip _reservedD3DExtensions
// Skip _reservedEGLExtensions
OPENCL20_API_TABLE;
OPENCL21_API_TABLE;
OPENCL22_API_TABLE;
EXTENSIONS_TABLE;
#ifdef _WIN32
WINDOWS_ONLY_EXTENSIONS_TABLE;
#endif
#undef X
#undef MAKE_STRING
return output;
}