Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (C) 2015 Nandor Licker. All rights reserved.
- #include "kernel.h"
- namespace lenet {
- namespace cl {
- void CheckCLError(const std::string &file, size_t line, const std::string &func, cl_int err)
- {
- if (err == CL_SUCCESS) {
- return;
- }
- throw std::runtime_error(
- file + " " +
- std::to_string(line) + " " +
- func + " " +
- std::to_string(err));
- }
- Context::Context()
- : context_(nullptr)
- {
- cl_int err;
- err = clGetDeviceIDs(nullptr, CL_DEVICE_TYPE_GPU, 1, &device_, nullptr);
- clCheck(err);
- context_ = clCreateContext(0, 1, &device_, nullptr, nullptr, &err);
- clCheck(err);
- command_ = clCreateCommandQueue(context_, device_, 0, &err);
- clCheck(err);
- }
- Context::~Context()
- {
- if (context_) {
- clReleaseContext(context_);
- }
- }
- std::shared_ptr<Program> Context::program(const std::string &source)
- {
- return std::make_shared<Program>(shared_from_this(), source);
- }
- Program::Program(
- const std::shared_ptr<Context> &context,
- const std::string &source)
- : context_(context)
- {
- assert(context_ != nullptr);
- cl_int err;
- size_t logSize;
- const char *ptr;
- size_t length;
- std::string buildLog;
- cl_device_id device = *context_;
- // Create the program object.
- ptr = source.c_str();
- length = source.size() - 1;
- program_ = clCreateProgramWithSource(*context_, 1, &ptr, &length, &err);
- clCheck(err);
- // Compile the program.
- err = clBuildProgram(program_, 1, &device, nullptr, nullptr, nullptr);
- if (err) {
- err = clGetProgramBuildInfo(
- program_,
- device,
- CL_PROGRAM_BUILD_LOG,
- 0,
- nullptr,
- &logSize);
- clCheck(err);
- buildLog.resize(logSize + 1);
- err = clGetProgramBuildInfo(
- program_,
- device,
- CL_PROGRAM_BUILD_LOG,
- logSize,
- &buildLog[0],
- nullptr);
- clCheck(err);
- throw std::runtime_error(buildLog);
- }
- }
- Program::~Program()
- {
- if (program_) {
- clReleaseProgram(program_);
- }
- }
- } // namespace cl
- } // namespace lenet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement