Skip to content

Commit 0e03bb3

Browse files
authored
improve sample look and feel consistency (#176)
* improve sample look and feel consistency Move code to setup the platform and device to a shared utility function. Add support for verbose setup, which will print more information about the platform and device than the default. * only setup the OpenCL device, not the OpenCL platform Very few samples need the OpenCL platform, and when they do, they can query it from the device. * handle negative indices * rename variable typo
1 parent 07a69d8 commit 0e03bb3

56 files changed

Lines changed: 450 additions & 823 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎include/util.hpp‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ static bool checkPlatformIndex(
9898
fprintf(stderr, "Error: No OpenCL platforms found.\n");
9999
return false;
100100
}
101+
if (platformIndex < 0) {
102+
fprintf(stderr, "Error: Invalid platform index %d specified\n",
103+
platformIndex);
104+
return false;
105+
}
101106
if (platformIndex >= (int)platforms.size()) {
102107
fprintf(stderr, "Error: Invalid platform index %d specified (max %d)\n",
103108
platformIndex,
@@ -106,3 +111,67 @@ static bool checkPlatformIndex(
106111
}
107112
return true;
108113
}
114+
115+
static bool checkDeviceIndex(
116+
const std::vector<cl::Device>& devices,
117+
int deviceIndex)
118+
{
119+
if (devices.size() == 0) {
120+
fprintf(stderr, "Error: No OpenCL devices found.\n");
121+
return false;
122+
}
123+
if (deviceIndex < 0) {
124+
fprintf(stderr, "Error: Invalid device index %d specified\n",
125+
deviceIndex);
126+
return false;
127+
}
128+
if (deviceIndex >= (int)devices.size()) {
129+
fprintf(stderr, "Error: Invalid device index %d specified (max %d)\n",
130+
deviceIndex,
131+
(int)(devices.size() - 1) );
132+
return false;
133+
}
134+
return true;
135+
}
136+
137+
static bool setupDevice(
138+
cl::Device& device,
139+
int platformIndex,
140+
int deviceIndex,
141+
bool verbose = false)
142+
{
143+
std::vector<cl::Platform> platforms;
144+
cl::Platform::get(&platforms);
145+
146+
if (!checkPlatformIndex(platforms, platformIndex)) {
147+
return false;
148+
}
149+
150+
const cl::Platform& platform = platforms[platformIndex];
151+
printf("Running on platform: %s\n",
152+
platform.getInfo<CL_PLATFORM_NAME>().c_str() );
153+
154+
std::vector<cl::Device> devices;
155+
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
156+
157+
if (!checkDeviceIndex(devices, deviceIndex)) {
158+
return false;
159+
}
160+
161+
device = devices[deviceIndex];
162+
if (verbose) {
163+
printf("Running on device: %s (%uCUs, %uMHz)\n",
164+
device.getInfo<CL_DEVICE_NAME>().c_str(),
165+
device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(),
166+
device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() );
167+
printf("Device version: %s\n",
168+
device.getInfo<CL_DEVICE_VERSION>().c_str() );
169+
printf("Driver version: %s\n",
170+
device.getInfo<CL_DRIVER_VERSION>().c_str() );
171+
} else {
172+
printf("Running on device: %s\n",
173+
device.getInfo<CL_DEVICE_NAME>().c_str() );
174+
}
175+
176+
return true;
177+
}

‎samples/00_enumopencl/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ static cl_int PrintDeviceInfoSummary(
243243
return errorCode;
244244
}
245245

246-
int main(
247-
int argc,
248-
char** argv )
246+
int main(int argc, char** argv)
249247
{
250248
{
251249
popl::OptionParser op("Supported Options");

‎samples/00_enumopenclpp/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ static cl_int PrintDeviceInfoSummary(
5252
return CL_SUCCESS;
5353
}
5454

55-
int main(
56-
int argc,
57-
char** argv )
55+
int main(int argc, char** argv)
5856
{
5957
{
6058
popl::OptionParser op("Supported Options");

‎samples/00_enumqueuefamilies/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ static void PrintDeviceInfoSummary(
106106
}
107107
}
108108

109-
int main(
110-
int argc,
111-
char** argv )
109+
int main(int argc, char** argv)
112110
{
113111
{
114112
popl::OptionParser op("Supported Options");

‎samples/00_extendeddevicequeries/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ static void PrintDeviceInfoSummary(
160160
}
161161
}
162162

163-
int main(
164-
int argc,
165-
char** argv )
163+
int main(int argc, char** argv)
166164
{
167165
{
168166
popl::OptionParser op("Supported Options");

‎samples/00_loaderinfo/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ static void PrintLoaderInfo(const char* label, cl_icdl_info info)
3131
printf("Query for for %s (size = %zu) returned: %s\n", label, sz, str.data());
3232
}
3333

34-
int main(
35-
int argc,
36-
char** argv )
34+
int main(int argc, char** argv)
3735
{
3836
{
3937
popl::OptionParser op("Supported Options");

‎samples/00_newqueries/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,7 @@ static void PrintDeviceInfoSummary(
595595
}
596596
}
597597

598-
int main(
599-
int argc,
600-
char** argv )
598+
int main(int argc, char** argv)
601599
{
602600
{
603601
popl::OptionParser op("Supported Options");

‎samples/00_newqueriespp/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,7 @@ static void PrintDeviceInfoSummary(
234234
}
235235
}
236236

237-
int main(
238-
int argc,
239-
char** argv )
237+
int main(int argc, char** argv)
240238
{
241239
{
242240
popl::OptionParser op("Supported Options");

‎samples/00_spirvqueries/main.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626

2727
#include "util.hpp"
2828

29-
int main(
30-
int argc,
31-
char** argv )
29+
int main(int argc, char** argv)
3230
{
3331
{
3432
popl::OptionParser op("Supported Options");

‎samples/01_copybuffer/main.cpp‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
const size_t gwx = 1024*1024;
1414

15-
int main(
16-
int argc,
17-
char** argv )
15+
int main(int argc, char** argv)
1816
{
17+
bool verbose = false;
1918
int platformIndex = 0;
2019
int deviceIndex = 0;
2120

2221
{
2322
popl::OptionParser op("Supported Options");
23+
op.add<popl::Switch, popl::Attribute::advanced>("v", "verbose", "Verbose Output", &verbose);
2424
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
2525
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
2626

@@ -40,26 +40,15 @@ int main(
4040
}
4141
}
4242

43-
std::vector<cl::Platform> platforms;
44-
cl::Platform::get(&platforms);
45-
46-
if (!checkPlatformIndex(platforms, platformIndex)) {
43+
cl::Device device;
44+
if (!setupDevice(device, platformIndex, deviceIndex, verbose)) {
4745
return -1;
4846
}
4947

50-
printf("Running on platform: %s\n",
51-
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
52-
53-
std::vector<cl::Device> devices;
54-
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);
55-
56-
printf("Running on device: %s\n",
57-
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );
58-
59-
cl::Context context{devices[deviceIndex]};
60-
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
48+
cl::Context context{device};
49+
cl::CommandQueue commandQueue{context, device};
6150

62-
cl::Buffer deviceMemSrc = cl::Buffer{
51+
cl::Buffer deviceMemSrc = cl::Buffer{
6352
context,
6453
CL_MEM_ALLOC_HOST_PTR,
6554
gwx * sizeof( cl_uint ) };

0 commit comments

Comments
 (0)