Skip to content

Commit e6e40cf

Browse files
committed
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.
1 parent 07a69d8 commit e6e40cf

56 files changed

Lines changed: 468 additions & 815 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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,63 @@ static bool checkPlatformIndex(
106106
}
107107
return true;
108108
}
109+
110+
static bool checkDeviceIndex(
111+
const std::vector<cl::Device>& devices,
112+
int deviceIndex)
113+
{
114+
if (devices.size() == 0) {
115+
fprintf(stderr, "Error: No OpenCL devices found.\n");
116+
return false;
117+
}
118+
if (deviceIndex >= (int)devices.size()) {
119+
fprintf(stderr, "Error: Invalid device index %d specified (max %d)\n",
120+
deviceIndex,
121+
(int)(devices.size() - 1) );
122+
return false;
123+
}
124+
return true;
125+
}
126+
127+
static bool setupPlatformAndDevice(
128+
cl::Platform& platform,
129+
cl::Device& device,
130+
int platformIndex,
131+
int deviceIndex,
132+
bool verbose = false)
133+
{
134+
std::vector<cl::Platform> platforms;
135+
cl::Platform::get(&platforms);
136+
137+
if (!checkPlatformIndex(platforms, platformIndex)) {
138+
return false;
139+
}
140+
141+
platform = std::move(platforms[platformIndex]);
142+
printf("Running on platform: %s\n",
143+
platform.getInfo<CL_PLATFORM_NAME>().c_str() );
144+
145+
std::vector<cl::Device> devices;
146+
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
147+
148+
if (!checkDeviceIndex(devices, deviceIndex)) {
149+
return false;
150+
}
151+
152+
device = std::move(devices[deviceIndex]);
153+
if (verbose) {
154+
printf("Running on device: %s (%uCUs, %uMHz)\n",
155+
device.getInfo<CL_DEVICE_NAME>().c_str(),
156+
device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(),
157+
device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() );
158+
printf("Device version: %s\n",
159+
device.getInfo<CL_DEVICE_VERSION>().c_str() );
160+
printf("Driver version: %s\n",
161+
device.getInfo<CL_DRIVER_VERSION>().c_str() );
162+
} else {
163+
printf("Running on device: %s\n",
164+
device.getInfo<CL_DEVICE_NAME>().c_str() );
165+
}
166+
167+
return true;
168+
}

‎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: 9 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,16 @@ int main(
4040
}
4141
}
4242

43-
std::vector<cl::Platform> platforms;
44-
cl::Platform::get(&platforms);
45-
46-
if (!checkPlatformIndex(platforms, platformIndex)) {
43+
cl::Platform platform;
44+
cl::Device device;
45+
if (!setupPlatformAndDevice(platform, device, platformIndex, deviceIndex, verbose)) {
4746
return -1;
4847
}
4948

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]};
49+
cl::Context context{device};
50+
cl::CommandQueue commandQueue{context, device};
6151

62-
cl::Buffer deviceMemSrc = cl::Buffer{
52+
cl::Buffer deviceMemSrc = cl::Buffer{
6353
context,
6454
CL_MEM_ALLOC_HOST_PTR,
6555
gwx * sizeof( cl_uint ) };

0 commit comments

Comments
 (0)