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

Merge pull request #1021 from codeWorm2015/opencl

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