add banchmark image alignment
This commit is contained in:
+40
-28
@@ -4,40 +4,39 @@
|
||||
|
||||
#ifndef VIO_OPENCL_CL_CLASS_H
|
||||
#define VIO_OPENCL_CL_CLASS_H
|
||||
#include "cl.hpp"
|
||||
#include <cl.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <exception>
|
||||
#include <CL/opencl.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
class kernel{
|
||||
public:
|
||||
kernel(cl::Program* program,std::string name){_kernel = new cl::Kernel(*program,name.c_str());};
|
||||
~kernel(){
|
||||
for(auto&& i:_images)i.first.setDestructorCallback((void (*)(_cl_mem *, void *))notify, NULL);
|
||||
for(auto&& i:_buffers)i.first.first.setDestructorCallback((void (*)(_cl_mem *, void *))notify, NULL);
|
||||
_buffers.clear();
|
||||
_images.clear();
|
||||
};
|
||||
template<typename T>
|
||||
int32_t write(size_t id/*buffer ID*/,T* buf,cl::CommandQueue* queue,cl::Context* context,size_t buf_size){
|
||||
_buffers.push_back(std::pair<std::pair<cl::Buffer,size_t>,size_t>(std::pair<cl::Buffer,size_t>(cl::Buffer(*context, CL_MEM_READ_WRITE, sizeof(T) * buf_size),
|
||||
_buffers.push_back(std::pair<std::pair<cl::Buffer,size_t>,size_t>(std::pair<cl::Buffer,size_t>(cl::Buffer(*context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR
|
||||
, sizeof(T) * buf_size,buf),
|
||||
buf_size),id));
|
||||
cl_int err=queue->enqueueWriteBuffer(_buffers.back().first.first,CL_TRUE, 0, sizeof(T) * buf_size, buf);
|
||||
cl_int err=_kernel->setArg(id,_buffers.back().first.first);
|
||||
if(err!=0){
|
||||
std::cerr<<"Error C:Kernel, F:write, L:enqueueWriteBuffer\n"<<err<<'\n';
|
||||
return err;
|
||||
}
|
||||
err=_kernel->setArg(id,_buffers.back().first.first);
|
||||
if(err!=0){
|
||||
std::cerr<<"Error C:Kernel, F:write, L3:setArg\n"<<err<<'\n';
|
||||
std::cerr<<"Error C:Kernel, F:write, L3:setArg\t"<<id<<"\t"<<err<<'\n';
|
||||
return err;
|
||||
}
|
||||
return CL_SUCCESS;
|
||||
|
||||
};
|
||||
int32_t write(size_t id/*buffer ID*/,cv::Mat& buf,cl::Context* context){
|
||||
_images.push_back(std::pair<cl::Image2D,size_t>(cl::Image2D(*context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
||||
_images.push_back(std::pair<cl::Image2D,size_t>(cl::Image2D(*context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
|
||||
cl::ImageFormat(CL_R, CL_UNSIGNED_INT8),
|
||||
buf.size().width,
|
||||
buf.size().height,
|
||||
@@ -46,7 +45,7 @@ public:
|
||||
|
||||
cl_int err=_kernel->setArg(id,_images.back().first);
|
||||
if(err!=0){
|
||||
std::cerr<<"Error C:Kernel, F:write image, L:setArg\n"<<err<<'\n';
|
||||
std::cerr<<"Error C:Kernel, F:write image, L:setArg ID:\t"<<id<<"\t"<<err<<'\n';
|
||||
return err;
|
||||
}
|
||||
return CL_SUCCESS;
|
||||
@@ -54,25 +53,29 @@ public:
|
||||
|
||||
template<typename T>
|
||||
int32_t reload(size_t id,T* buf,cl::CommandQueue* queue){
|
||||
try{
|
||||
if(id>_buffers.size())return CL_MAP_FAILURE;
|
||||
for(auto i:_buffers)if(i.second==id)
|
||||
queue->enqueueWriteBuffer(i.first.first,CL_TRUE, 0, sizeof(T) * i.first.second, buf);
|
||||
return CL_SUCCESS;
|
||||
}catch (std::exception& err){
|
||||
std::cerr<<"Reload buffer on GPU failed\n"<<err.what()<<'\n';
|
||||
return CL_MAP_FAILURE;
|
||||
for(auto&& i:_buffers)if(i.second==id){
|
||||
T* Map_buf=(T*)queue->enqueueMapBuffer(i.first.first,CL_TRUE,CL_MAP_WRITE,0,sizeof(T) * i.first.second);
|
||||
memcpy(Map_buf,buf,sizeof(T) * i.first.second);
|
||||
if(queue->enqueueUnmapMemObject(i.first.first,Map_buf)!=CL_SUCCESS)std::cerr<<"Reload buffer on GPU failed"<<'\n';
|
||||
}
|
||||
return CL_SUCCESS;
|
||||
};
|
||||
int32_t reload(size_t id,cv::Mat& buf,cl::Context* context){
|
||||
try{
|
||||
for(auto i:_images)if(i.second==id)
|
||||
i.first=cl::Image2D(*context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
||||
for(auto&& i:_images)if(i.second==id){
|
||||
i.first.setDestructorCallback((void (*)(_cl_mem *, void *))notify, NULL);
|
||||
i.first=cl::Image2D(*context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
|
||||
cl::ImageFormat(CL_R, CL_UNSIGNED_INT8),
|
||||
buf.size().width,
|
||||
buf.size().height,
|
||||
0,
|
||||
reinterpret_cast<uchar*>(buf.data));
|
||||
cl_int err=_kernel->setArg(id,i.first);
|
||||
if(err!=0){
|
||||
std::cerr<<"Error C:Kernel, F:write image, L:setArg ID:\t"<<id<<"\t"<<err<<'\n';
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return CL_SUCCESS;
|
||||
}catch (std::exception& err){
|
||||
std::cerr<<"Reload buffer on GPU failed\n"<<err.what()<<'\n';
|
||||
@@ -87,6 +90,9 @@ public:
|
||||
for(auto i:_images)if(i.second==id)return i.first;
|
||||
}
|
||||
private:
|
||||
static void notify(cl_mem *, void * user_data) {
|
||||
//std::cerr << "Memory object was deleted." << std::endl;
|
||||
}
|
||||
std::vector<std::pair<std::pair<cl::Buffer,size_t>,size_t>> _buffers;
|
||||
std::vector<std::pair<cl::Image2D,size_t>> _images;
|
||||
|
||||
@@ -126,7 +132,7 @@ public:
|
||||
_kernels.at(id1).write(id2,buf,context);
|
||||
return CL_SUCCESS;
|
||||
}catch (std::exception& err){
|
||||
std::cerr<<"Write buffer on GPU failed\n"<<err.what()<<'\n';
|
||||
std::cerr<<"Write image on GPU failed\n"<<err.what()<<'\n';
|
||||
return CL_MAP_FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -155,6 +161,7 @@ public:
|
||||
}
|
||||
int32_t run(size_t id1/*kernal ID*/,std::size_t x=1,std::size_t y=1,std::size_t z=1) {
|
||||
cl_int err=0;
|
||||
queue->flush();
|
||||
if(z>1 && y>1){
|
||||
err=queue->enqueueNDRangeKernel(*_kernels.at(id1)._kernel, cl::NullRange/*offset*/, cl::NDRange(x,y,z)/*Global*/, cl::NullRange/*local*/);
|
||||
}else if(z<2 && y>1){
|
||||
@@ -162,27 +169,32 @@ public:
|
||||
}else{
|
||||
err=queue->enqueueNDRangeKernel(*_kernels.at(id1)._kernel, cl::NullRange/*offset*/, cl::NDRange(x)/*Global*/, cl::NullRange/*local*/);
|
||||
};
|
||||
queue->finish();
|
||||
if(err!=0){
|
||||
std::cerr<<"kernel: "<<_kernels.at(id1)._kernel->getInfo<CL_KERNEL_FUNCTION_NAME>();
|
||||
std::cerr<<" ,Args: "<<_kernels.at(id1)._kernel->getInfo<CL_KERNEL_NUM_ARGS>()<<" ,Run out: "<<err<<'\n';
|
||||
for(int i=0;i<_kernels.at(id1)._kernel->getInfo<CL_KERNEL_NUM_ARGS>();++i)
|
||||
std::cerr<<"argument type "<<i<<":"<<_kernels.at(id1)._kernel->getArgInfo<CL_KERNEL_ARG_TYPE_NAME>(i)<<'\n';
|
||||
}
|
||||
return err;
|
||||
return queue->finish();
|
||||
|
||||
}
|
||||
template<typename T>
|
||||
void read(size_t id1/*kernal ID*/,size_t id2/*buffer ID*/, T* out){
|
||||
queue->enqueueReadBuffer(_kernels.at(id1).read(id2).first, CL_TRUE, 0, sizeof(T) * _kernels.at(id1).read(id2).second, out);
|
||||
}
|
||||
template<typename T>
|
||||
void read(size_t id1/*kernal ID*/,size_t id2/*buffer ID*/,size_t size/*size*/, T* out){
|
||||
T* Map_buf=(T*)queue->enqueueMapBuffer(_kernels.at(id1).read(id2).first,CL_TRUE,CL_MAP_READ,0,sizeof(T) * size);
|
||||
memcpy(out,Map_buf,sizeof(T) * size);
|
||||
if(queue->enqueueUnmapMemObject(_kernels.at(id1).read(id2).first,Map_buf)!=CL_SUCCESS)std::cerr<<"Read buffer on GPU failed"<<'\n';
|
||||
}
|
||||
void clear_buf();
|
||||
private:
|
||||
std::vector<kernel> _kernels;
|
||||
cl::Context* context = NULL;
|
||||
cl::Device device;
|
||||
cl::Program* program = NULL;
|
||||
cl::CommandQueue* queue=NULL;
|
||||
cl::Context* context = nullptr;
|
||||
cl::Device* device = nullptr;
|
||||
cl::Program* program = nullptr;
|
||||
cl::CommandQueue* queue= nullptr;
|
||||
};
|
||||
|
||||
#endif //VIO_OPENCL_CL_CLASS_H
|
||||
|
||||
Reference in New Issue
Block a user