提交 b0d37e56 编写于 作者: R Ray Liu 提交者: GitHub

Merge pull request #1021 from codeWorm2015/opencl

update cl engineC
...@@ -26,12 +26,13 @@ bool CLEngine::Init() { ...@@ -26,12 +26,13 @@ bool CLEngine::Init() {
cl_int status; cl_int status;
SetPlatform(); SetPlatform();
SetClDeviceId(); SetClDeviceId();
initialized_ = true;
// setClContext(); // setClContext();
// setClCommandQueue(); // setClCommandQueue();
// std::string filename = "./HelloWorld_Kernel.cl"; // std::string filename = "./HelloWorld_Kernel.cl";
// loadKernelFromFile(filename.c_str()); // loadKernelFromFile(filename.c_str());
// buildProgram(); // buildProgram();
initialized_ = true;
} }
CLEngine *CLEngine::Instance() { CLEngine *CLEngine::Instance() {
...@@ -39,20 +40,6 @@ CLEngine *CLEngine::Instance() { ...@@ -39,20 +40,6 @@ CLEngine *CLEngine::Instance() {
return &cl_engine_; return &cl_engine_;
} }
//std::unique_ptr<_cl_kernel, clKernel_deleter> CLEngine::GSetKernel(
// const std::string &kernel_name) {
// std::unique_ptr<_cl_kernel, clKernel_deleter> kernel(
// clCreateKernel(program_.get(), kernel_name.c_str(), NULL));
// return std::move(kernel);
//}
//
//bool CLEngine::SetClCommandQueue() {
// cl_int status;
// command_queue_.reset(
// clCreateCommandQueue(context_.get(), devices_[0], 0, &status));
// return true;
//}
bool CLEngine::SetPlatform() { bool CLEngine::SetPlatform() {
platform_ = NULL; // the chosen platform platform_ = NULL; // the chosen platform
cl_uint numPlatforms; // the NO. of platforms cl_uint numPlatforms; // the NO. of platforms
...@@ -87,6 +74,20 @@ bool CLEngine::SetClDeviceId() { ...@@ -87,6 +74,20 @@ bool CLEngine::SetClDeviceId() {
return false; return false;
} }
//std::unique_ptr<_cl_kernel, clKernel_deleter> CLEngine::GSetKernel(
// const std::string &kernel_name) {
// std::unique_ptr<_cl_kernel, clKernel_deleter> kernel(
// clCreateKernel(program_.get(), kernel_name.c_str(), NULL));
// return std::move(kernel);
//}
//
//bool CLEngine::SetClCommandQueue() {
// cl_int status;
// command_queue_.reset(
// clCreateCommandQueue(context_.get(), devices_[0], 0, &status));
// return true;
//}
//bool CLEngine::SetClContext() { //bool CLEngine::SetClContext() {
// context_.reset(clCreateContext(NULL, 1, devices_, NULL, NULL, NULL)); // context_.reset(clCreateContext(NULL, 1, devices_, NULL, NULL, NULL));
// return true; // return true;
......
...@@ -16,7 +16,6 @@ limitations under the License. */ ...@@ -16,7 +16,6 @@ limitations under the License. */
#include <memory> #include <memory>
#include <string> #include <string>
#include <fstream>
#include "common/enforce.h" #include "common/enforce.h"
#include "framework/cl/cl_deleter.h" #include "framework/cl/cl_deleter.h"
...@@ -45,25 +44,23 @@ class CLEngine { ...@@ -45,25 +44,23 @@ class CLEngine {
} }
std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith(cl_context context, std::string file_name) { std::unique_ptr<_cl_program, CLProgramDeleter> CreateProgramWith(cl_context context, std::string file_name) {
const char *kernel_file = file_name.c_str();
size_t size;
char *str;
std::fstream f(kernel_file, (std::fstream::in | std::fstream::binary));
PADDLE_MOBILE_ENFORCE(f.is_open(), " file open failed")
size_t fileSize; FILE *file = fopen(file_name.c_str(), "rb");
f.seekg(0, std::fstream::end); PADDLE_MOBILE_ENFORCE(file != nullptr, "can't open file: %s ",
size = fileSize = (size_t)f.tellg(); filename.c_str());
f.seekg(0, std::fstream::beg); fseek(file, 0, SEEK_END);
str = new char[size+1]; int64_t size = ftell(file);
PADDLE_MOBILE_ENFORCE(size > 0, "size is too small");
PADDLE_MOBILE_ENFORCE(str != NULL, " str null") rewind(file);
char *data = new char[size + 1];
f.read(str, fileSize); size_t bytes_read = fread(data, 1, size, file);
f.close(); data[size] = '\0';
str[size] = '\0'; PADDLE_MOBILE_ENFORCE(bytes_read == size,
const char *source = str; "read binary file bytes do not match with fseek");
fclose(file);
const char *source = data;
size_t sourceSize[] = {strlen(source)}; size_t sourceSize[] = {strlen(source)};
cl_program p = clCreateProgramWithSource(context, 1, &source, sourceSize, NULL); cl_program p = clCreateProgramWithSource(context, 1, &source, sourceSize, NULL);
std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p); std::unique_ptr<_cl_program, CLProgramDeleter> program_ptr(p);
...@@ -84,13 +81,6 @@ class CLEngine { ...@@ -84,13 +81,6 @@ class CLEngine {
bool SetClDeviceId(); bool SetClDeviceId();
// bool SetClContext();
// bool SetClCommandQueue();
// bool LoadKernelFromFile(const char *kernel_file);
// bool BuildProgram();
bool initialized_; bool initialized_;
...@@ -103,6 +93,15 @@ class CLEngine { ...@@ -103,6 +93,15 @@ class CLEngine {
std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_; std::unique_ptr<_cl_command_queue, CLCommQueueDeleter> command_queue_;
std::unique_ptr<_cl_program, CLProgramDeleter> program_; std::unique_ptr<_cl_program, CLProgramDeleter> program_;
// bool SetClContext();
// bool SetClCommandQueue();
// bool LoadKernelFromFile(const char *kernel_file);
// bool BuildProgram();
}; };
} // namespace framework } // namespace framework
......
...@@ -93,7 +93,7 @@ class Scope { ...@@ -93,7 +93,7 @@ class Scope {
mutable std::unordered_map<std::string, Variable *> vars_; mutable std::unordered_map<std::string, Variable *> vars_;
mutable std::list<Scope *> kids_; mutable std::list<Scope *> kids_;
Scope const *parent_{nullptr}; Scope const *parent_{nullptr};
//#ifdef PADDLE_MOBILE_OCL //#ifdef PADDLE_MOBILE_CL
CLScope *cl_scope_ = new CLScope(); CLScope *cl_scope_ = new CLScope();
//#endif //#endif
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册