ocl_library_wrapper.cpp
Go to the documentation of this file.
1 
8 
9 namespace pses_kinect_utilities
10 {
11 
13 {
14  // get all platforms (drivers), e.g. NVIDIA
15  std::vector<cl::Platform> all_platforms;
16  cl::Platform::get(&all_platforms);
17 
18  if (all_platforms.size() == 0)
19  {
20  throw std::runtime_error("No platforms found. Check OpenCL installation!");
21  }
22  cl::Platform default_platform = all_platforms[0];
23  std::cout << "Using platform: "
24  << default_platform.getInfo<CL_PLATFORM_NAME>() << "\n";
25  // get default device (CPUs, GPUs) of the default platform
26  std::vector<cl::Device> all_devices;
27  default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
28  if (all_devices.size() == 0)
29  {
30  throw std::runtime_error("No devices found. Check OpenCL installation!");
31  }
32  else
33  {
34  for (cl::Device dev : all_devices)
35  {
36  std::cout << "Dev found: " << dev.getInfo<CL_DEVICE_TYPE>() << " "
37  << dev.getInfo<CL_DEVICE_NAME>() << std::endl;
38  }
39  }
40 
41  // use device[1] because that's a GPU; device[0] is the CPU
42  cl::Device default_device = all_devices[0];
43  std::cout << "Using device: " << default_device.getInfo<CL_DEVICE_NAME>()
44  << "\n";
45  return std::make_shared<cl::Device>(default_device);
46 }
47 
49 {
50  // a context is like a "runtime link" to the device and platform;
51  // i.e. communication is possible
52  cl::Context context({*device});
53  return std::make_shared<cl::Context>(context);
54 }
55 
56 StringPtr load_kernel_definition(const std::string& path)
57 {
58  std::ifstream file(path);
59  std::stringstream ss = std::stringstream();
60  char buffer[65500];
61  while (file.getline(buffer, 65500))
62  {
63  ss << buffer << "\n";
64  }
65 
66  return std::make_shared<std::string>(ss.str());
67 }
68 
70  StringPtr kernel)
71 {
72  // create the program that we want to execute on the device
73  cl::Program::Sources sources;
74  sources.push_back({kernel->c_str(), kernel->length()});
75 
76  cl::Program program(*context, sources);
77  if (program.build({*device}) != CL_SUCCESS)
78  {
79  throw std::runtime_error(
80  "Error building: " +
81  program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(*device));
82  }
83  return std::make_shared<cl::Program>(program);
84 }
85 
87 {
88  cl::CommandQueue queue(*context, *device);
89  return std::make_shared<cl::CommandQueue>(queue);
90 }
91 
93  const std::string& program_name)
94 {
95  cl::Kernel kernel = cl::Kernel(*program, program_name.c_str());
96  return std::make_shared<cl::Kernel>(kernel);
97 }
98 } // namespace pses_kinect_utilities
ContextPtr get_ocl_context(DevicePtr device)
QueuePtr create_ocl_command_queue(ContextPtr context, DevicePtr device)
std::shared_ptr< cl::Device > DevicePtr
StringPtr load_kernel_definition(const std::string &path)
std::shared_ptr< std::string > StringPtr
std::shared_ptr< cl::Program > ProgramPtr
std::shared_ptr< cl::CommandQueue > QueuePtr
KernelPtr create_ocl_kernel(ProgramPtr program, const std::string &program_name)
This namespace is used by the nodelets inside our package pses_kinect_utilities.
std::shared_ptr< cl::Context > ContextPtr
std::shared_ptr< cl::Kernel > KernelPtr
ProgramPtr build_ocl_program(DevicePtr device, ContextPtr context, StringPtr kernel)


pses_kinect_utilities
Author(s): Nicolas Acero
autogenerated on Sun Nov 26 2017 19:23:52