55#include < tuple>
66
77namespace 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+
2636cl::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+
2842std::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
5367std::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+
64106std::string get_error_string (int error) {
65107 // source:
66108 // https://stackoverflow.com/questions/24326432/convenient-way-to-show-opencl-error-codes
0 commit comments