Skip to content

Commit 655a803

Browse files
committed
Fix default api
1 parent 6c5f089 commit 655a803

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

oclhelpers.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ cl::Platform get_default_platform() { return get_platforms()[0]; }
1919
std::vector<cl::Device> get_devices(const cl::Platform &p, int type) {
2020
std::vector<cl::Device> devices;
2121
p.getDevices(type, &devices);
22-
if (!devices.size()) {
23-
throw OCLHelpersException("No device of requested type found!");
24-
}
2522
return devices;
2623
}
2724

@@ -55,7 +52,7 @@ cl::Program make_program_from_file(cl::Context &ctx,
5552
return {ctx, kernel_code};
5653
}
5754

58-
void build(cl::Program &program, cl::Device &device) {
55+
void build(cl::Program &program, const cl::Device &device) {
5956
if (program.build({device}) != CL_SUCCESS) {
6057
std::stringstream ss;
6158
ss << "Building failed: "
@@ -65,8 +62,8 @@ void build(cl::Program &program, cl::Device &device) {
6562
}
6663

6764
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
68-
compile_file_with(const std::string &filename, cl::Platform &platform,
69-
cl::Device &device) {
65+
compile_file_with(const std::string &filename, const cl::Platform &platform,
66+
const cl::Device &device) {
7067
cl::Context ctx({device});
7168
auto program = make_program_from_file(ctx, filename);
7269
build(program, device);
@@ -83,10 +80,13 @@ compile_file_with_defaults(const std::string &filename) {
8380

8481
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
8582
compile_file_with_default_cpu(const std::string &filename) {
86-
auto platform = get_default_platform();
87-
auto device = get_default_cpu(platform);
83+
for (auto &p : get_platforms()) {
84+
if (get_cpus(p).size()) {
85+
return compile_file_with(filename, p, get_default_cpu(p));
86+
}
87+
}
8888

89-
return compile_file_with(filename, platform, device);
89+
throw OCLHelpersException("No cpu found!");
9090
}
9191

9292
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
@@ -96,6 +96,17 @@ compile_file_with_default_cpu(cl::Platform &platform,
9696
return compile_file_with(filename, platform, device);
9797
}
9898

99+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
100+
compile_file_with_default_gpu(const std::string &filename) {
101+
for (auto &p : get_platforms()) {
102+
if (get_gpus(p).size()) {
103+
return compile_file_with(filename, p, get_default_gpu(p));
104+
}
105+
}
106+
107+
throw OCLHelpersException("No gpu found!");
108+
}
109+
99110
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
100111
compile_file_with_default_gpu(cl::Platform &platform,
101112
const std::string &filename) {

oclhelpers.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::string read_kernel_from_file(const std::string &filename);
3939
cl::Program make_program_from_file(cl::Context &ctx,
4040
const std::string &filename);
4141

42-
void build(cl::Program &program, cl::Device &device);
42+
void build(cl::Program &program, const cl::Device &device);
4343

4444
/*
4545
The program is compiled with the default device on default platform.
@@ -57,6 +57,9 @@ std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
5757
compile_file_with_default_cpu(cl::Platform &platform,
5858
const std::string &filename);
5959

60+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
61+
compile_file_with_default_gpu(const std::string &filename);
62+
6063
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
6164
compile_file_with_default_gpu(cl::Platform &platform,
6265
const std::string &filename);

0 commit comments

Comments
 (0)