Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. // (C) 2015 Nandor Licker. All rights reserved.
  2.  
  3. #include "kernel.h"
  4.  
  5.  
  6. namespace lenet {
  7. namespace cl {
  8.  
  9. void CheckCLError(const std::string &file, size_t line, const std::string &func, cl_int err)
  10. {
  11. if (err == CL_SUCCESS) {
  12. return;
  13. }
  14. throw std::runtime_error(
  15. file + " " +
  16. std::to_string(line) + " " +
  17. func + " " +
  18. std::to_string(err));
  19. }
  20.  
  21. Context::Context()
  22. : context_(nullptr)
  23. {
  24. cl_int err;
  25.  
  26. err = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_GPU, 1, &device_, nullptr);
  27. clCheck(err);
  28. context_ = clCreateContext(0, 1, &device_, nullptr, nullptr, &err);
  29. clCheck(err);
  30. command_ = clCreateCommandQueue(context_, device_, 0, &err);
  31. clCheck(err);
  32. }
  33.  
  34. Context::~Context()
  35. {
  36. if (context_) {
  37. clReleaseContext(context_);
  38. }
  39. }
  40.  
  41. std::shared_ptr<Program> Context::program(const std::string &source)
  42. {
  43. return std::make_shared<Program>(shared_from_this(), source);
  44. }
  45.  
  46. Program::Program(
  47. const std::shared_ptr<Context> &context,
  48. const std::string &source)
  49. : context_(context)
  50. {
  51. assert(context_ != nullptr);
  52.  
  53. cl_int err;
  54. size_t logSize;
  55. const char *ptr;
  56. size_t length;
  57. std::string buildLog;
  58. cl_device_id device = *context_;
  59.  
  60. // Create the program object.
  61. ptr = source.c_str();
  62. length = source.size() - 1;
  63. program_ = clCreateProgramWithSource(*context_, 1, &ptr, &length, &err);
  64. clCheck(err);
  65.  
  66. // Compile the program.
  67. err = clBuildProgram(program_, 1, &device, nullptr, nullptr, nullptr);
  68. if (err) {
  69. err = clGetProgramBuildInfo(
  70. program_,
  71. device,
  72. CL_PROGRAM_BUILD_LOG,
  73. 0,
  74. nullptr,
  75. &logSize);
  76. clCheck(err);
  77.  
  78. buildLog.resize(logSize + 1);
  79. err = clGetProgramBuildInfo(
  80. program_,
  81. device,
  82. CL_PROGRAM_BUILD_LOG,
  83. logSize,
  84. &buildLog[0],
  85. nullptr);
  86. clCheck(err);
  87.  
  88. throw std::runtime_error(buildLog);
  89. }
  90. }
  91.  
  92. Program::~Program()
  93. {
  94. if (program_) {
  95. clReleaseProgram(program_);
  96. }
  97. }
  98.  
  99. } // namespace cl
  100. } // namespace lenet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement