Skip to content

Commit 6c5f089

Browse files
committed
Add more getters
1 parent 0a1fc79 commit 6c5f089

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

oclhelpers.cpp

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,40 @@
55
#include <tuple>
66

77
namespace oclhelpers {
8-
cl::Platform get_default_platform() {
8+
std::vector<cl::Platform> get_platforms() {
99
std::vector<cl::Platform> platforms;
1010
cl::Platform::get(&platforms);
1111
if (!platforms.size()) {
1212
throw OCLHelpersException("No platform found!");
1313
}
14-
return platforms[0];
14+
return platforms;
1515
}
1616

17-
std::vector<cl::Device> get_gpus(const cl::Platform &p) {
17+
cl::Platform get_default_platform() { return get_platforms()[0]; }
18+
19+
std::vector<cl::Device> get_devices(const cl::Platform &p, int type) {
1820
std::vector<cl::Device> devices;
19-
p.getDevices(CL_DEVICE_TYPE_GPU, &devices);
21+
p.getDevices(type, &devices);
2022
if (!devices.size()) {
21-
throw OCLHelpersException("No gpus found!");
23+
throw OCLHelpersException("No device of requested type found!");
2224
}
2325
return devices;
2426
}
2527

28+
std::vector<cl::Device> get_gpus(const cl::Platform &p) {
29+
return get_devices(p, CL_DEVICE_TYPE_GPU);
30+
}
31+
32+
std::vector<cl::Device> get_cpus(const cl::Platform &p) {
33+
return get_devices(p, CL_DEVICE_TYPE_CPU);
34+
}
35+
2636
cl::Device get_default_device(const cl::Platform &p) { return get_gpus(p)[0]; }
2737

38+
// fixme
39+
cl::Device get_default_gpu(const cl::Platform &p) { return get_gpus(p)[0]; }
40+
cl::Device get_default_cpu(const cl::Platform &p) { return get_cpus(p)[0]; }
41+
2842
std::string read_kernel_from_file(const std::string &filename) {
2943
std::ifstream is(filename);
3044
return std::string(std::istreambuf_iterator<char>(is),
@@ -51,16 +65,44 @@ void build(cl::Program &program, cl::Device &device) {
5165
}
5266

5367
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
54-
compile_file_with_defaults(const std::string &filename) {
55-
auto platform = get_default_platform();
56-
auto device = get_default_device(platform);
57-
68+
compile_file_with(const std::string &filename, cl::Platform &platform,
69+
cl::Device &device) {
5870
cl::Context ctx({device});
5971
auto program = make_program_from_file(ctx, filename);
6072
build(program, device);
6173
return std::make_tuple(platform, device, ctx, program);
6274
}
6375

76+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
77+
compile_file_with_defaults(const std::string &filename) {
78+
auto platform = get_default_platform();
79+
auto device = get_default_device(platform);
80+
81+
return compile_file_with(filename, platform, device);
82+
}
83+
84+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
85+
compile_file_with_default_cpu(const std::string &filename) {
86+
auto platform = get_default_platform();
87+
auto device = get_default_cpu(platform);
88+
89+
return compile_file_with(filename, platform, device);
90+
}
91+
92+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
93+
compile_file_with_default_cpu(cl::Platform &platform,
94+
const std::string &filename) {
95+
auto device = get_default_cpu(platform);
96+
return compile_file_with(filename, platform, device);
97+
}
98+
99+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
100+
compile_file_with_default_gpu(cl::Platform &platform,
101+
const std::string &filename) {
102+
auto device = get_default_gpu(platform);
103+
return compile_file_with(filename, platform, device);
104+
}
105+
64106
std::string get_error_string(int error) {
65107
// source:
66108
// https://stackoverflow.com/questions/24326432/convenient-way-to-show-opencl-error-codes

oclhelpers.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ class OCLHelpersException : public std::runtime_error {
2525
virtual ~OCLHelpersException() noexcept {}
2626
};
2727

28+
std::vector<cl::Platform> get_platforms();
2829
cl::Platform get_default_platform();
2930

3031
std::vector<cl::Device> get_gpus(const cl::Platform &p);
32+
std::vector<cl::Device> get_cpus(const cl::Platform &p);
3133
cl::Device get_default_device(const cl::Platform &p);
34+
cl::Device get_default_gpu(const cl::Platform &p);
35+
cl::Device get_default_cpu(const cl::Platform &p);
3236

3337
std::string read_kernel_from_file(const std::string &filename);
3438

@@ -46,6 +50,17 @@ Usage example:
4650
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
4751
compile_file_with_defaults(const std::string &filename);
4852

53+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
54+
compile_file_with_default_cpu(const std::string &filename);
55+
56+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
57+
compile_file_with_default_cpu(cl::Platform &platform,
58+
const std::string &filename);
59+
60+
std::tuple<cl::Platform, cl::Device, cl::Context, cl::Program>
61+
compile_file_with_default_gpu(cl::Platform &platform,
62+
const std::string &filename);
63+
4964
namespace {
5065
inline void _set_args(cl::Kernel &k, int pos) {}
5166
template <class T, class... Ts>

0 commit comments

Comments
 (0)