Skip to content

Commit 196d94c

Browse files
wenju-hebashbaug
andauthored
Check if platform index is out of range (#155)
* Check if platform index is out of range * Update include/util.hpp Co-authored-by: Ben Ashbaugh <ben.ashbaugh@intel.com> --------- Co-authored-by: Ben Ashbaugh <ben.ashbaugh@intel.com>
1 parent a5c214a commit 196d94c

File tree

47 files changed

+253
-0
lines changed

Some content is hidden

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

47 files changed

+253
-0
lines changed

include/util.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,20 @@ static bool checkDeviceForExtension(
6767

6868
return supported;
6969
}
70+
71+
static bool checkPlatformIndex(
72+
const std::vector<cl::Platform>& platforms,
73+
int platformIndex)
74+
{
75+
if (platforms.size() == 0) {
76+
fprintf(stderr, "Error: No OpenCL platforms found.\n");
77+
return false;
78+
}
79+
if (platformIndex >= (int)platforms.size()) {
80+
fprintf(stderr, "Error: Invalid platform index %d specified (max %d)\n",
81+
platformIndex,
82+
(int)(platforms.size() - 1) );
83+
return false;
84+
}
85+
return true;
86+
}

samples/01_copybuffer/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <CL/opencl.hpp>
1010

11+
#include "util.hpp"
12+
1113
const size_t gwx = 1024*1024;
1214

1315
int main(
@@ -41,6 +43,10 @@ int main(
4143
std::vector<cl::Platform> platforms;
4244
cl::Platform::get(&platforms);
4345

46+
if (!checkPlatformIndex(platforms, platformIndex)) {
47+
return -1;
48+
}
49+
4450
printf("Running on platform: %s\n",
4551
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
4652

samples/02_copybufferkernel/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <CL/opencl.hpp>
1010

11+
#include "util.hpp"
12+
1113
const size_t gwx = 1024*1024;
1214

1315
static const char kernelString[] = R"CLC(
@@ -49,6 +51,10 @@ int main(
4951
std::vector<cl::Platform> platforms;
5052
cl::Platform::get(&platforms);
5153

54+
if (!checkPlatformIndex(platforms, platformIndex)) {
55+
return -1;
56+
}
57+
5258
printf("Running on platform: %s\n",
5359
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
5460

samples/03_mandelbrot/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <CL/opencl.hpp>
1313

14+
#include "util.hpp"
15+
1416
const char* filename = "mandelbrot.bmp";
1517

1618
const cl_uint width = 768;
@@ -83,6 +85,10 @@ int main(
8385
std::vector<cl::Platform> platforms;
8486
cl::Platform::get(&platforms);
8587

88+
if (!checkPlatformIndex(platforms, platformIndex)) {
89+
return -1;
90+
}
91+
8692
printf("Running on platform: %s\n",
8793
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
8894

samples/04_julia/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <chrono>
1515

16+
#include "util.hpp"
17+
1618
const char* filename = "julia.bmp";
1719

1820
const float cr = -0.123f;
@@ -104,6 +106,10 @@ int main(
104106
std::vector<cl::Platform> platforms;
105107
cl::Platform::get(&platforms);
106108

109+
if (!checkPlatformIndex(platforms, platformIndex)) {
110+
return -1;
111+
}
112+
107113
printf("Running on platform: %s\n",
108114
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
109115

samples/04_sobel/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <chrono>
1515

16+
#include "util.hpp"
17+
1618
const char* filename = "sobel.bmp";
1719

1820
const float cr = -0.123f;
@@ -151,6 +153,10 @@ int main(
151153
std::vector<cl::Platform> platforms;
152154
cl::Platform::get(&platforms);
153155

156+
if (!checkPlatformIndex(platforms, platformIndex)) {
157+
return -1;
158+
}
159+
154160
printf("Running on platform: %s\n",
155161
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
156162

samples/05_kernelfromfile/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <fstream>
1212
#include <string>
1313

14+
#include "util.hpp"
15+
1416
static std::string readStringFromFile(
1517
const std::string& filename )
1618
{
@@ -71,6 +73,10 @@ int main(
7173
std::vector<cl::Platform> platforms;
7274
cl::Platform::get(&platforms);
7375

76+
if (!checkPlatformIndex(platforms, platformIndex)) {
77+
return -1;
78+
}
79+
7480
printf("Running on platform: %s\n",
7581
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
7682

samples/05_spirvkernelfromfile/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ int main(
134134
std::vector<cl::Platform> platforms;
135135
cl::Platform::get(&platforms);
136136

137+
if (!checkPlatformIndex(platforms, platformIndex)) {
138+
return -1;
139+
}
140+
137141
printf("Running on platform: %s\n",
138142
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
139143

samples/06_ndrangekernelfromfile/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <fstream>
1212
#include <string>
1313

14+
#include "util.hpp"
15+
1416
static std::string readStringFromFile(
1517
const std::string& filename )
1618
{
@@ -86,6 +88,10 @@ int main(
8688
std::vector<cl::Platform> platforms;
8789
cl::Platform::get(&platforms);
8890

91+
if (!checkPlatformIndex(platforms, platformIndex)) {
92+
return -1;
93+
}
94+
8995
printf("Running on platform: %s\n",
9096
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
9197

samples/10_queueexperiments/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ int main(
413413
std::vector<cl::Platform> platforms;
414414
cl::Platform::get(&platforms);
415415

416+
if (!checkPlatformIndex(platforms, platformIndex)) {
417+
return -1;
418+
}
419+
416420
printf("Running on platform: %s\n",
417421
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
418422

0 commit comments

Comments
 (0)