Skip to content

Commit 3833055

Browse files
committed
clean up and simplify the tester
1 parent e916b1d commit 3833055

1 file changed

Lines changed: 106 additions & 94 deletions

File tree

samples/99_globalvar/main.cpp

Lines changed: 106 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
#include "util.hpp"
1515

16-
static std::vector<cl_uchar> readSPIRVFromFile(
16+
static std::vector<char> readSPIRVFromFile(
1717
const std::string& filename )
1818
{
1919
std::ifstream is(filename, std::ios::binary);
20-
std::vector<cl_uchar> ret;
20+
std::vector<char> ret;
2121
if (!is.good()) {
2222
printf("Couldn't open file '%s'!\n", filename.c_str());
2323
return ret;
@@ -37,67 +37,7 @@ static std::vector<cl_uchar> readSPIRVFromFile(
3737
return ret;
3838
}
3939

40-
static cl::Program createProgramWithIL(
41-
const cl::Context& context,
42-
const std::vector<cl_uchar>& il )
43-
{
44-
cl_program program = nullptr;
45-
46-
// Use the core clCreateProgramWithIL if a device supports OpenCL 2.1 or
47-
// newer and SPIR-V.
48-
bool useCore = false;
49-
50-
// Use the extension clCreateProgramWithILKHR if a device supports
51-
// cl_khr_il_program.
52-
bool useExtension = false;
53-
54-
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
55-
for (auto device : devices) {
56-
#ifdef CL_VERSION_2_1
57-
// Note: This could look for "SPIR-V" in CL_DEVICE_IL_VERSION.
58-
if (getDeviceOpenCLVersion(device) >= CL_MAKE_VERSION(2, 1, 0) &&
59-
!device.getInfo<CL_DEVICE_IL_VERSION>().empty()) {
60-
useCore = true;
61-
}
62-
#endif
63-
if (checkDeviceForExtension(device, "cl_khr_il_program")) {
64-
useExtension = true;
65-
}
66-
}
67-
68-
#ifdef CL_VERSION_2_1
69-
if (useCore) {
70-
program = clCreateProgramWithIL(
71-
context(),
72-
il.data(),
73-
il.size(),
74-
nullptr);
75-
}
76-
else
77-
#endif
78-
if (useExtension) {
79-
cl::Platform platform{ devices[0].getInfo<CL_DEVICE_PLATFORM>() };
80-
81-
auto clCreateProgramWithILKHR_ = (clCreateProgramWithILKHR_fn)
82-
clGetExtensionFunctionAddressForPlatform(
83-
platform(),
84-
"clCreateProgramWithILKHR");
85-
86-
if (clCreateProgramWithILKHR_) {
87-
program = clCreateProgramWithILKHR_(
88-
context(),
89-
il.data(),
90-
il.size(),
91-
nullptr);
92-
}
93-
}
94-
95-
return cl::Program{ program };
96-
}
97-
98-
int main(
99-
int argc,
100-
char** argv )
40+
int main(int argc, char** argv )
10141
{
10242
int platformIndex = 0;
10343
int deviceIndex = 0;
@@ -131,45 +71,25 @@ int main(
13171
cl::Platform::get(&platforms);
13272
cl::Platform& platform = platforms[platformIndex];
13373

134-
printf("Running on platform: %s\n",
135-
platform.getInfo<CL_PLATFORM_NAME>().c_str() );
74+
printf("Running on platform: %s\n", platform.getInfo<CL_PLATFORM_NAME>().c_str() );
13675

13776
std::vector<cl::Device> devices;
13877
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
13978
cl::Device& device = devices[deviceIndex];
14079

141-
printf("Running on device: %s\n",
142-
device.getInfo<CL_DEVICE_NAME>().c_str() );
143-
printf("CL_DEVICE_ADDRESS_BITS is %d for this device.\n",
144-
device.getInfo<CL_DEVICE_ADDRESS_BITS>() );
145-
146-
// Check for SPIR-V support. If the device supports OpenCL 2.1 or newer
147-
// we can use the core clCreateProgramWithIL API. Otherwise, if the device
148-
// the cl_khr_il_program extension we can use the clCreateProgramWithILKHR
149-
// extension API. If neither is supported then we cannot run this sample.
150-
#ifdef CL_VERSION_2_1
151-
// Note: This could look for "SPIR-V" in CL_DEVICE_IL_VERSION.
152-
if (getDeviceOpenCLVersion(device) >= CL_MAKE_VERSION(2, 1, 0) &&
153-
!device.getInfo<CL_DEVICE_IL_VERSION>().empty()) {
154-
printf("Device supports OpenCL 2.1 or newer, using clCreateProgramWithIL.\n");
155-
} else
156-
#endif
157-
if (checkDeviceForExtension(device, "cl_khr_il_program")) {
158-
printf("Device supports cl_khr_il_program, using clCreateProgramWithILKHR.\n");
159-
} else {
160-
printf("Device does not support SPIR-V, exiting.\n");
161-
return -1;
162-
}
80+
printf("Running on device: %s\n", device.getInfo<CL_DEVICE_NAME>().c_str() );
81+
printf("Running on drivers: %s\n", device.getInfo<CL_DRIVER_VERSION>().c_str() );
82+
printf("CL_DEVICE_ADDRESS_BITS is %d for this device.\n", device.getInfo<CL_DEVICE_ADDRESS_BITS>() );
16383

16484
cl::Context context{device};
16585
cl::CommandQueue commandQueue{context, device};
16686

16787
printf("Reading SPIR-V from file: %s\n", fileName.c_str());
168-
std::vector<cl_uchar> spirv = readSPIRVFromFile(fileName);
88+
std::vector<char> spirv = readSPIRVFromFile(fileName);
16989

17090
printf("Building program with build options: %s\n",
17191
buildOptions.empty() ? "(none)" : buildOptions.c_str() );
172-
cl::Program program = createProgramWithIL( context, spirv );
92+
cl::Program program{context, spirv};
17393
program.build(buildOptions.c_str());
17494
for( auto& device : program.getInfo<CL_PROGRAM_DEVICES>() )
17595
{
@@ -208,6 +128,9 @@ int main(
208128
auto clGetMemAllocInfoINTEL = (clGetMemAllocInfoINTEL_fn)
209129
clGetExtensionFunctionAddressForPlatform(platform(), "clGetMemAllocInfoINTEL");
210130

131+
constexpr const char* HostAccessName = "HostAccessName";
132+
constexpr const char* ExportName = "ExportName";
133+
211134
if (clGetDeviceGlobalVariablePointerINTEL == nullptr) {
212135
printf("Couldn't get function pointer for clGetDeviceGlobalVariablePointerINTEL!\n");
213136
} else if (clGetMemAllocInfoINTEL == nullptr) {
@@ -216,13 +139,13 @@ int main(
216139
cl_int errorCode = CL_SUCCESS;
217140
size_t gvsize = 0; void* gvptr = nullptr;
218141
errorCode = clGetDeviceGlobalVariablePointerINTEL(
219-
device(), program(), "HostAccessName", &gvsize, &gvptr);
220-
printf("clGetDeviceGlobalVariablePointerINTEL with HostAccessName returned %d: %zu %p\n", errorCode, gvsize, gvptr);
142+
device(), program(), HostAccessName, &gvsize, &gvptr);
143+
printf("clGetDeviceGlobalVariablePointerINTEL with %s returned %d: %p (size %zu)\n", HostAccessName, errorCode, gvptr, gvsize);
221144

222145
gvsize = 0; gvptr = nullptr;
223146
errorCode = clGetDeviceGlobalVariablePointerINTEL(
224-
device(), program(), "ExportName", &gvsize, &gvptr);
225-
printf("clGetDeviceGlobalVariablePointerINTEL with ExportName returned %d: %zu %p\n", errorCode, gvsize, gvptr);
147+
device(), program(), ExportName, &gvsize, &gvptr);
148+
printf("clGetDeviceGlobalVariablePointerINTEL with %s returned %d: %p (size %zu)\n", ExportName, errorCode, gvptr, gvsize);
226149

227150
cl_unified_shared_memory_type_intel gvtype = 0;
228151
errorCode = clGetMemAllocInfoINTEL(
@@ -231,7 +154,96 @@ int main(
231154
printf("clGetMemAllocInfoINTEL returned %d: %04X\n", errorCode, gvtype);
232155
}
233156

234-
printf("Done.\n");
157+
typedef cl_int CL_API_CALL
158+
clEnqueueWriteGlobalVariableINTEL_t(
159+
cl_command_queue command_queue,
160+
cl_program program,
161+
const char* name,
162+
cl_bool blocking_write,
163+
size_t size,
164+
size_t offset,
165+
const void* ptr,
166+
cl_uint num_events_in_wait_list,
167+
const cl_event* event_wait_list,
168+
cl_event* event);
169+
170+
typedef clEnqueueWriteGlobalVariableINTEL_t *
171+
clEnqueueWriteGlobalVariableINTEL_fn;
172+
173+
auto clEnqueueWriteGlobalVariableINTEL = (clEnqueueWriteGlobalVariableINTEL_fn)
174+
clGetExtensionFunctionAddressForPlatform(platform(), "clEnqueueWriteGlobalVariableINTEL");
175+
176+
if (clEnqueueWriteGlobalVariableINTEL == nullptr) {
177+
printf("Couldn't get function pointer for clEnqueueWriteGlobalVariableINTEL!\n");
178+
} else {
179+
const int value = 0xDEADBEEF;
180+
cl_int errorCode;
181+
182+
errorCode = clEnqueueWriteGlobalVariableINTEL(
183+
commandQueue(),
184+
program(),
185+
HostAccessName,
186+
CL_TRUE,
187+
sizeof(value), 0, &value,
188+
0, nullptr, nullptr);
189+
printf("clEnqueueWriteGlobalVariableINTEL with %s to write %08X returned %d\n", HostAccessName, value, errorCode);
190+
191+
errorCode = clEnqueueWriteGlobalVariableINTEL(
192+
commandQueue(),
193+
program(),
194+
ExportName,
195+
CL_TRUE,
196+
sizeof(value), 0, &value,
197+
0, nullptr, nullptr);
198+
printf("clEnqueueWriteGlobalVariableINTEL with %s to write %08X returned %d\n", ExportName, value, errorCode);
199+
}
235200

201+
typedef cl_int CL_API_CALL
202+
clEnqueueReadGlobalVariableINTEL_t(
203+
cl_command_queue command_queue,
204+
cl_program program,
205+
const char* name,
206+
cl_bool blocking_read,
207+
size_t size,
208+
size_t offset,
209+
void* ptr,
210+
cl_uint num_events_in_wait_list,
211+
const cl_event* event_wait_list,
212+
cl_event* event);
213+
214+
typedef clEnqueueReadGlobalVariableINTEL_t *
215+
clEnqueueReadGlobalVariableINTEL_fn;
216+
217+
auto clEnqueueReadGlobalVariableINTEL = (clEnqueueReadGlobalVariableINTEL_fn)
218+
clGetExtensionFunctionAddressForPlatform(platform(), "clEnqueueReadGlobalVariableINTEL");
219+
220+
if (clEnqueueReadGlobalVariableINTEL == nullptr) {
221+
printf("Couldn't get function pointer for clEnqueueReadGlobalVariableINTEL!\n");
222+
} else {
223+
int value;
224+
cl_int errorCode;
225+
226+
value = -1;
227+
errorCode = clEnqueueReadGlobalVariableINTEL(
228+
commandQueue(),
229+
program(),
230+
HostAccessName,
231+
CL_TRUE,
232+
sizeof(value), 0, &value,
233+
0, nullptr, nullptr);
234+
printf("clEnqueueReadGlobalVariableINTEL with %s returned %d, read %08X\n", HostAccessName, errorCode, value);
235+
236+
value = -1;
237+
errorCode = clEnqueueReadGlobalVariableINTEL(
238+
commandQueue(),
239+
program(),
240+
ExportName,
241+
CL_TRUE,
242+
sizeof(value), 0, &value,
243+
0, nullptr, nullptr);
244+
printf("clEnqueueReadGlobalVariableINTEL with %s returned %d, read %08X\n", ExportName, errorCode, value);
245+
}
246+
247+
printf("Done.\n");
236248
return 0;
237249
}

0 commit comments

Comments
 (0)