/*
* PrepareOpenCL.cpp
*
* Created on: 2017年8月10日
* Author: root
*/
#include "PrepareOpenCL.hpp"
prepareing::prepareing():rgbarray(new uchar[256*256*256]),rgbsize(256*256*256),
imgheight(1456),imgwidth(1936),mysize(cvSize(1936,1456)),
srcdatasize(1456*1936*3*sizeof(uchar)),dstdatasize(1456*1936*sizeof(uchar)),
rgbArray_buffer(NULL),kernel_imgProc(NULL),device(NULL),
context(NULL),program(NULL)
{
boost::mutex::scoped_lock lock(initMutex);
memset(rgbarray, 0, rgbsize * sizeof(uchar));
Ptr<ml::TrainData> mlData2=cv::ml::TrainData::loadFromCSV("/home/jumper/Ore_projects/multiThreadsFluoreOpenCL/fluore_0728pm.csv",0,-2,0);
cv::Mat csvimg2=mlData2->getSamples();
int csvrows2 = csvimg2.rows;
for (int j = 0; j < csvrows2; j++){
float* pixeldata = csvimg2.ptr<float>(j);
float x = pixeldata[0];
float y = pixeldata[1];
float z = pixeldata[2];
int newindex = x + y * 256 + z * 256 * 256;
rgbarray[newindex] = 255;
}
//"prepare for running the OpenCL partion...";
cl_uint platformNum;
cl_int status;
status=clGetPlatformIDs(0,NULL,&platformNum);
if(status!=CL_SUCCESS){
printf("Error:cannot get platforms number.\n");
}
cl_platform_id platforms[1];
status=clGetPlatformIDs(platformNum,platforms,NULL);
if(status!=CL_SUCCESS){
printf("Error:cannot get platforms addresses.\n");
}
cl_platform_id platformInUse=platforms[0];
clGetDeviceIDs(platformInUse,CL_DEVICE_TYPE_GPU,1,&device,NULL);
context=clCreateContext(NULL,1,&device,NULL,NULL,NULL);
std::ifstream srcFile("/home/jumper/Ore_projects/multiThreadsFluoreOpenCL/forFluore.cl");
std::string srcProg(std::istreambuf_iterator<char>(srcFile),(std::istreambuf_iterator<char>()));
const char * src = srcProg.c_str();
size_t length = srcProg.length();
program=clCreateProgramWithSource(context,1,&src,&length,&status);
status=clBuildProgram(program,1,&device,NULL,NULL,NULL);
if (status != CL_SUCCESS)
{
printf("Error:clBuildProgram()...\n");
shrLogEx(LOGBOTH | ERRORMSG, status, STDERROR);
oclLogBuildInfo(program, oclGetFirstDev(context));
oclLogPtx(program, oclGetFirstDev(context), "oclfluore.ptx");
}
rgbArray_buffer=clCreateBuffer(context,CL_MEM_READ_ONLY,rgbsize*sizeof(uchar),rgbarray,&status);
//cout<<"srcdatasize in PrepareOpenCL..."<<srcdatasize<<endl;
for(int i=0;i<threadsnum;i++) //number of threads
{
queue[i]=clCreateCommandQueue(context,device,CL_QUEUE_PROFILING_ENABLE,NULL);
status=clEnqueueWriteBuffer(queue[i], rgbArray_buffer, CL_FALSE, 0, rgbsize* sizeof(uchar),rgbarray, 0, NULL, NULL);
srcdata_buffer[i] = clCreateBuffer(context, CL_MEM_READ_ONLY, srcdatasize, NULL,NULL);
dstdata_buffer[i] = clCreateBuffer(context, CL_MEM_WRITE_ONLY| CL_MEM_ALLOC_HOST_PTR, dstdatasize, NULL,NULL);
}
kernel_imgProc=clCreateKernel(program,"imgProcess",&status);
status = clSetKernelArg(kernel_imgProc, 0, sizeof(cl_mem), (void*)&rgbArray_buffer);
status = clSetKernelArg(kernel_imgProc, 3, sizeof(cl_int), &imgwidth);
status = clSetKernelArg(kernel_imgProc, 4, sizeof(cl_int), &imgheight);
if(status!=CL_SUCCESS)
{
cout<<"common resources failed..."<<status<<endl;
}
}
void prepareing::initCommonResource()
{
}
prepareing& prepareing::operator=(const prepareing &rhs)
{
imgwidth=rhs.imgwidth;
imgheight=rhs.imgheight;
mysize=rhs.mysize;
rgbsize=rhs.rgbsize;
device=rhs.device;
program=rhs.program;
context=rhs.context;
kernel_imgProc=rhs.kernel_imgProc;
rgbArray_buffer=rhs.rgbArray_buffer;
srcdatasize=rhs.srcdatasize;
dstdatasize=rhs.dstdatasize;
if(rgbarray==NULL)
rgbarray=new uchar[rgbsize];
for(int j=0;j<rgbsize;j++)
rgbarray[j]=rhs.rgbarray[j];
for(int i=0;i<5;i++)
{
queue[i]=rhs.queue[i];
srcdata_buffer[i]=rhs.srcdata_buffer[i];
dstdata_buffer[i]=rhs.dstdata_buffer[i];
}
return *this;
}
//int prepareing::getimgwidth()
//{
// return imgwidth;
//}
//int prepareing::getimgheight()
//{
// return imgheight;
//}
//CvSize prepareing::getmysize()
//{
// return mysize;
//}
//int prepareing::getrgbsize()
//{
// return rgbsize;
//}
//
//cl_device_id prepareing::getdevice()
//{
// return device;
//}
//cl_program prepareing::getprogram()
//{
// return program;
//}
//cl_context prepareing::getcontext()
//{
// return context;
//}
//int prepareing::getsrcdatasize()
//{
// return srcdatasize;
//}
//int prepareing::getdstdatasize()
//{
// return dstdatasize;
//}
//cl_kernel prepareing::getkernel_imgProc()
//{
// return kernel_imgProc;
//}
//cl_mem prepareing::getrgbArray_buffer()
//{
// return rgbArray_buffer;
//}
//uchar* prepareing::getrgbarray()
//{
// uchar *rgbreturn=new uchar[rgbsize]; //???????
// for(int j=0;j<rgbsize;j++)
// rgbreturn[j]=rgbarray[j];
// return rgbreturn;
//}
//cl_command_queue* prepareing::getqueue()
//{
//
// cl_command_queue queuereturn[threadsnum];
// for(int j=0;j<threadsnum;j++)
// queuereturn[j]=queue[j];
// return queuereturn;
//}
//cl_mem* prepareing::getsrcdata_buffer()
//{
// cl_mem srcdata_bufferreturn[threadsnum];
// for(int j=0;j<threadsnum;j++)
// srcdata_bufferreturn[j]=srcdata_buffer[j];
// return srcdata_bufferreturn;
//}
//
//cl_mem* prepareing::getdstdata_buffer()
//{
// cl_mem dstdata_bufferreturn[threadsnum];
// for(int j=0;j<threadsnum;j++)
// dstdata_bufferreturn[j]=dstdata_buffer[j];
// return dstdata_bufferreturn;
//}
prepareing::~prepareing()
{
if(rgbarray)
delete [] rgbarray;
clReleaseContext(context);
clReleaseProgram(program);
clReleaseKernel(kernel_imgProc);
for(int i=0;i<threadsnum;i++)
{
clReleaseCommandQueue(queue[i]);
clReleaseMemObject(dstdata_buffer[i]);
clReleaseMemObject(srcdata_buffer[i]);
}
clReleaseMemObject(rgbArray_buffer);
}
//void prepareing::init()
//{
// public_resource common_obj;
// common_obj.rgbsize=256*256*256;
// common_obj.imgheight=1456;
// common_obj.imgwidth=1936;
// common_obj.mysize = cvSize(common_obj.imgwidth, common_obj.imgheight);
// common_obj.rgbarray = new uchar[256*256*256];
//
// memset(common_obj.rgbarray, 0, common_obj.rgbsize * sizeof(uchar));
// Ptr<ml::TrainData> mlData2=cv::ml::TrainData::loadFromCSV("/home/jumper/Ore_projects/multiThreadsFluoreOpenCL/fluore_0728pm.csv",0,-2,0);
// cv::Mat csvimg2=mlData2->getSamples();
// int csvrows2 = csvimg2.rows;
// for (int j = 0; j < csvrows2; j++){
// float* pixeldata = csvimg2.ptr<float>(j);
// float x = pixeldata[0];
// float y = pixeldata[1];
// float z = pixeldata[2];
// int newindex = x + y * 256 + z * 256 * 256;
// common_obj.rgbarray[newindex] = 255;
// }
//
// //"prepare for running the OpenCL partion...";
// cl_uint platformNum;
// cl_int status;
// status=clGetPlatformIDs(0,NULL,&platformNum);
// if(status!=CL_SUCCESS){
// printf("Error:cannot get platforms number.\n");
// }
// cl_platform_id platforms[1];
// status=clGetPlatformIDs(platformNum,platforms,NULL);
// if(status!=CL_SUCCESS){
// printf("Error:cannot get platforms addresses.\n");
// }
// cl_platform_id platformInUse=platforms[0];
//
// clGetDeviceIDs(platformInUse,CL_DEVICE_TYPE_GPU,1,&(common_obj.device),NULL);
// common_obj.context=clCreateContext(NULL,1,&(common_obj.device),NULL,NULL,NULL);
//
//
// std::ifstream srcFile("/home/jumper/Ore_projects/multiThreadsFluoreOpenCL/forFluore.cl");
// std::string srcProg(std::istreambuf_iterator<char>(srcFile),(std::istreambuf_iterator<char>()));
// const char * src = srcProg.c_str();
// size_t length = srcProg.length();
// common_obj.program=clCreateProgramWithSource(common_obj.context,1,&src,&length,&status);
// status=clBuildProgram(common_obj.program,1,&(common_obj.device),NULL,NULL,NULL);
// if (status != CL_SUCCESS)
// {
// printf("Error:clBuildProgram()...\n");
// shrLogEx(LOGBOTH | ERRORMSG, status, STDERROR);
// oclLogBuildInfo(common_obj.program, oclGetFirstDev(common_obj.context));
// oclLogPtx(common_obj.program, oclGetFirstDev(common_obj.context), "oclfluore.ptx");
// }
//
// int pixels=(common_obj.imgheight)*(common_obj.imgwidth);
// common_obj.srcdatasize=pixels*3*sizeof(uchar