25
25
#include < iostream>
26
26
#include < limits>
27
27
#include < map>
28
- #include < memory>
29
- #include < mutex>
30
28
#include < sstream>
31
29
#include < string>
32
30
#include < vector>
@@ -73,93 +71,19 @@ CONSTFIX char clGetDeviceFunctionPointerName[] =
73
71
74
72
#undef CONSTFIX
75
73
76
- typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetDeviceFunctionPointer_fn)(
77
- cl_device_id device, cl_program program, const char *FuncName,
78
- cl_ulong *ret_ptr);
79
-
80
- typedef CL_API_ENTRY cl_int (CL_API_CALL *clSetProgramSpecializationConstant_fn)(
81
- cl_program program, cl_uint spec_id, size_t spec_size,
82
- const void *spec_value);
83
-
84
- struct ExtFuncsPerContextT ;
85
-
86
- namespace detail {
87
- template <const char *FuncName, typename FuncT>
88
- std::pair<FuncT &, bool &> get (ExtFuncsPerContextT &);
89
- } // namespace detail
90
-
91
- struct ExtFuncsPerContextT {
92
- #define _EXT_FUNCTION_INTEL (t_pfx ) \
93
- t_pfx##INTEL_fn t_pfx##Func = nullptr ; \
94
- bool t_pfx##Initialized = false ;
95
-
96
- #define _EXT_FUNCTION (t_pfx ) \
97
- t_pfx##_fn t_pfx##Func = nullptr ; \
98
- bool t_pfx##Initialized = false ;
99
-
100
- #include " ext_functions.inc"
101
-
102
- #undef _EXT_FUNCTION
103
- #undef _EXT_FUNCTION_INTEL
104
-
105
- std::mutex Mtx;
106
-
107
- template <const char *FuncName, typename FuncT>
108
- std::pair<FuncT &, bool &> get () {
109
- return detail::get<FuncName, FuncT>(*this );
110
- }
111
- };
112
-
113
- namespace detail {
114
-
115
- #define _EXT_FUNCTION_COMMON (t_pfx, t_pfx_suff ) \
116
- template <> \
117
- std::pair<t_pfx_suff##_fn &, bool &> get<t_pfx##Name, t_pfx_suff##_fn>( \
118
- ExtFuncsPerContextT & Funcs) { \
119
- using FPtrT = t_pfx_suff##_fn; \
120
- std::pair<FPtrT &, bool &> Ret{Funcs.t_pfx ##Func, \
121
- Funcs.t_pfx ##Initialized}; \
122
- return Ret; \
123
- }
124
- #define _EXT_FUNCTION_INTEL (t_pfx ) _EXT_FUNCTION_COMMON(t_pfx, t_pfx##INTEL)
125
- #define _EXT_FUNCTION (t_pfx ) _EXT_FUNCTION_COMMON(t_pfx, t_pfx)
126
-
127
- #include " ext_functions.inc"
128
-
129
- #undef _EXT_FUNCTION
130
- #undef _EXT_FUNCTION_INTEL
131
- #undef _EXT_FUNCTION_COMMON
132
- } // namespace detail
133
-
134
- struct ExtFuncsCachesT {
135
- std::map<pi_context, ExtFuncsPerContextT> Caches;
136
- std::mutex Mtx;
137
- };
138
-
139
- ExtFuncsCachesT *ExtFuncsCaches = nullptr ;
140
-
141
74
// USM helper function to get an extension function pointer
142
75
template <const char *FuncName, typename T>
143
76
static pi_result getExtFuncFromContext (pi_context context, T *fptr) {
144
77
// TODO
145
78
// Potentially redo caching as PI interface changes.
146
- ExtFuncsPerContextT *PerContext = nullptr ;
147
- {
148
- assert (ExtFuncsCaches);
149
- std::lock_guard<std::mutex> Lock{ExtFuncsCaches->Mtx };
150
-
151
- PerContext = &ExtFuncsCaches->Caches [context];
152
- }
153
-
154
- std::lock_guard<std::mutex> Lock{PerContext->Mtx };
155
- std::pair<T &, bool &> FuncInitialized = PerContext->get <FuncName, T>();
79
+ thread_local static std::map<pi_context, T> FuncPtrs;
156
80
157
81
// if cached, return cached FuncPtr
158
- if (FuncInitialized. second ) {
82
+ if (auto F = FuncPtrs[context] ) {
159
83
// if cached that extension is not available return nullptr and
160
84
// PI_INVALID_VALUE
161
- *fptr = FuncInitialized. first ;
162
- return *fptr ? PI_SUCCESS : PI_INVALID_VALUE;
85
+ *fptr = F ;
86
+ return F ? PI_SUCCESS : PI_INVALID_VALUE;
163
87
}
164
88
165
89
cl_uint deviceCount;
@@ -191,17 +115,14 @@ static pi_result getExtFuncFromContext(pi_context context, T *fptr) {
191
115
T FuncPtr =
192
116
(T)clGetExtensionFunctionAddressForPlatform (curPlatform, FuncName);
193
117
194
- // We're about to store the cached value. Mark this cache entry initialized.
195
- FuncInitialized.second = true ;
196
-
197
118
if (!FuncPtr) {
198
119
// Cache that the extension is not available
199
- FuncInitialized. first = nullptr ;
120
+ FuncPtrs[context] = nullptr ;
200
121
return PI_INVALID_VALUE;
201
122
}
202
123
203
- FuncInitialized.first = FuncPtr;
204
124
*fptr = FuncPtr;
125
+ FuncPtrs[context] = FuncPtr;
205
126
206
127
return cast<pi_result>(ret_err);
207
128
}
@@ -641,6 +562,9 @@ static bool is_in_separated_string(const std::string &str, char delimiter,
641
562
return false ;
642
563
}
643
564
565
+ typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetDeviceFunctionPointer_fn)(
566
+ cl_device_id device, cl_program program, const char *FuncName,
567
+ cl_ulong *ret_ptr);
644
568
pi_result piextGetDeviceFunctionPointer (pi_device device, pi_program program,
645
569
const char *func_name,
646
570
pi_uint64 *function_pointer_ret) {
@@ -1381,6 +1305,10 @@ pi_result piKernelSetExecInfo(pi_kernel kernel, pi_kernel_exec_info param_name,
1381
1305
}
1382
1306
}
1383
1307
1308
+ typedef CL_API_ENTRY cl_int (CL_API_CALL *clSetProgramSpecializationConstant_fn)(
1309
+ cl_program program, cl_uint spec_id, size_t spec_size,
1310
+ const void *spec_value);
1311
+
1384
1312
pi_result piextProgramSetSpecializationConstant (pi_program prog,
1385
1313
pi_uint32 spec_id,
1386
1314
size_t spec_size,
@@ -1456,21 +1384,9 @@ pi_result piextKernelGetNativeHandle(pi_kernel kernel,
1456
1384
// pi_level_zero.cpp for reference) Currently this is just a NOOP.
1457
1385
pi_result piTearDown (void *PluginParameter) {
1458
1386
(void )PluginParameter;
1459
- delete ExtFuncsCaches;
1460
- ExtFuncsCaches = nullptr ;
1461
1387
return PI_SUCCESS;
1462
1388
}
1463
1389
1464
- pi_result piContextRelease (pi_context Context) {
1465
- {
1466
- std::lock_guard<std::mutex> Lock{ExtFuncsCaches->Mtx };
1467
-
1468
- ExtFuncsCaches->Caches .erase (Context);
1469
- }
1470
-
1471
- return cast<pi_result>(clReleaseContext (cast<cl_context>(Context)));
1472
- }
1473
-
1474
1390
pi_result piPluginInit (pi_plugin *PluginInit) {
1475
1391
int CompareVersions = strcmp (PluginInit->PiVersion , SupportedVersion);
1476
1392
if (CompareVersions < 0 ) {
@@ -1482,8 +1398,6 @@ pi_result piPluginInit(pi_plugin *PluginInit) {
1482
1398
// PI interface supports higher version or the same version.
1483
1399
strncpy (PluginInit->PluginVersion , SupportedVersion, 4 );
1484
1400
1485
- ExtFuncsCaches = new ExtFuncsCachesT;
1486
-
1487
1401
#define _PI_CL (pi_api, ocl_api ) \
1488
1402
(PluginInit->PiFunctionTable ).pi_api = (decltype (&::pi_api))(&ocl_api);
1489
1403
@@ -1507,7 +1421,7 @@ pi_result piPluginInit(pi_plugin *PluginInit) {
1507
1421
_PI_CL (piContextCreate, piContextCreate)
1508
1422
_PI_CL (piContextGetInfo, clGetContextInfo)
1509
1423
_PI_CL (piContextRetain, clRetainContext)
1510
- _PI_CL (piContextRelease, piContextRelease )
1424
+ _PI_CL (piContextRelease, clReleaseContext )
1511
1425
_PI_CL (piextContextGetNativeHandle, piextContextGetNativeHandle)
1512
1426
_PI_CL (piextContextCreateWithNativeHandle, piextContextCreateWithNativeHandle)
1513
1427
// Queue
0 commit comments