This commit is contained in:
gfuse
2021-06-18 14:57:59 +02:00
commit 6e6d191bda
19 changed files with 1797 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
// Copyright (C) 2011 Dmitri Nikulin
// Copyright (C) 2011 Monash University
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "cvd-cl/steps/FastGrayStep.hh"
#include "kernels/fast-gray.hh"
namespace CVD {
namespace CL {
FastGrayStep::FastGrayStep(GrayImageState & i_image, PointListState & i_points, PointListState & o_points, cl_int threshold, cl_int ring) :
WorkerStep (i_image.worker),
i_image (i_image),
i_points (i_points),
o_points (o_points),
threshold (threshold),
ring (ring)
{
char opt[256] = {0,};
snprintf(opt, sizeof(opt) - 1, "-DFAST_THRESH=%d -DFAST_RING=%d -DFAST_COUNT=%d", int(threshold), int(ring), int(o_points.size));
worker.compile(&program, &kernel, OCL_FAST_GRAY, "fast_gray", opt);
}
FastGrayStep::~FastGrayStep() {
// Do nothing.
}
void FastGrayStep::execute() {
// Read number of input points.
size_t const np = i_points.getCount();
// Assign kernel parameters.
kernel.setArg(0, i_image.image);
kernel.setArg(1, i_points.buffer);
kernel.setArg(2, o_points.buffer);
kernel.setArg(3, o_points.count);
kernel.setArg(4, (cl_uint) np);
// Reset number of output points.
o_points.setCount(0);
// Prepare global and local size.
cl::NDRange const global(worker.padGlobal(np));
cl::NDRange const local(worker.defaultLocal);
// Queue kernel with global size set to number of input points.
worker.queue.enqueueNDRangeKernel(kernel, cl::NullRange, global, local);
}
} // namespace CL
} // namespace CVD
+70
View File
@@ -0,0 +1,70 @@
// Copyright (C) 2011 Dmitri Nikulin
// Copyright (C) 2011 Monash University
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "cvd-cl/steps/PreFastGrayStep.hh"
#include "kernels/prefast-gray.hh"
namespace CVD {
namespace CL {
PreFastGrayStep::PreFastGrayStep(GrayImageState & i_image, PointListState & o_points, cl_int threshold) :
WorkerStep (i_image.worker),
i_image (i_image),
o_points (o_points),
threshold (threshold)
{
char opt[256] = {0,};
snprintf(opt, sizeof(opt) - 1, "-DFAST_THRESH=%d -DFAST_COUNT=%d", int(threshold), int(o_points.size));
worker.compile(&program, &kernel, OCL_PRE_FAST_GRAY, "prefast_gray", opt);
}
PreFastGrayStep::~PreFastGrayStep() {
// Do nothing.
}
void PreFastGrayStep::execute() {
// Reset number of output points.
o_points.setCount(0);
// Assign kernel parameters.
kernel.setArg(0, i_image.image);
kernel.setArg(1, o_points.buffer);
kernel.setArg(2, o_points.count);
// Read image dimensions.
size_t const nx = i_image.nx - 16;
size_t const ny = i_image.ny - 16;
// Construct global, local and offset with a safety boundary.
// 16x16 appears to give good performance on most devices.
cl::NDRange const global (nx - 16, ny - 16);
cl::NDRange const local ( 8, 8);
cl::NDRange const offset ( 8, 8);
// Queue kernel.
worker.queue.enqueueNDRangeKernel(kernel, offset, global, local);
}
} // namespace CL
} // namespace CVD
+48
View File
@@ -0,0 +1,48 @@
//
// Created by root on 4/27/21.
//
#include "cl_class.h"
opencl::opencl() {
std::vector<cl::Platform> all_platforms;
cl::Platform::get(&all_platforms);
if (all_platforms.size() == 0){
std::cerr << " No platforms found. Check OpenCL installation!\n";
exit(1);
}
//get default device of the default platform
std::vector<cl::Device> all_devices;
for(auto i:all_platforms){
std::cout << "Find platform: " << i.getInfo<CL_PLATFORM_NAME>() << "\n";
i.getDevices(CL_DEVICE_TYPE_GPU, &all_devices);
}
if (all_devices.size() == 0){
std::cout << " No devices found. Check OpenCL installation!\n";
exit(1);
}
device = all_devices[0];
std::cout << "CL_DEVICE_NAME: " << device.getInfo<CL_DEVICE_NAME>() <<'\n'
<< "CL_DEVICE_OPENCL_C_VERSION: " <<device.getInfo<CL_DEVICE_OPENCL_C_VERSION>()<<'\n'
<< "CL_DEVICE_BUILT_IN_KERNELS: " <<device.getInfo<CL_DEVICE_BUILT_IN_KERNELS>()<<'\n'
<< "CL_DEVICE_COMPILER_AVAILABLE: " <<device.getInfo<CL_DEVICE_COMPILER_AVAILABLE>()<<'\n'
<< "CL_DEVICE_LOCAL_MEM_SIZE: " <<device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>()<<'\n'
<< "CL_DEVICE_GLOBAL_MEM_SIZE: " <<device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>()<<'\n';
context=new cl::Context({ device });
cl::Program::Sources sources;
read_cl fast("../kernel/fast-gray.cl");
sources.push_back({ fast.src_str, fast.size });
program=new cl::Program(*context, sources);
if(program->build({ device },"-DFAST_THRESH=40") !=0)
std::cout << " Error building: " << program->getBuildInfo<CL_PROGRAM_BUILD_STATUS>(device)<<'\n'
<<program->getBuildInfo<CL_PROGRAM_BUILD_LOG>(device) << '\n';
queue=new cl::CommandQueue(*context,device,0,NULL);
}
opencl::~opencl() {
}
void opencl::clear_buf() {
queue->finish();
queue->flush();
_kernels.clear();
}
+135
View File
@@ -0,0 +1,135 @@
// Copyright (C) 2011 Dmitri Nikulin
// Copyright (C) 2011 Monash University
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "common.h"
#include <cvd-cl/steps/PreFastGrayStep.hh>
#include <cvd-cl/steps/FastGrayStep.hh>
static void clfast(cl::Device & device, CVD::Image<CVD::byte> const & image) {
// Re-interpret image pointer.
cl_uchar const * const data = reinterpret_cast<cl_uchar const *>(image.data());
int const nx = image.size().x;
int const ny = image.size().y;
CVD::CL::Worker worker(device);
// Create states.
CVD::CL::GrayImageState cl_image(worker, ny, nx, CL_MEM_READ_ONLY);
CVD::CL::PointListState cl_corners1(worker, FAST_COUNT);
CVD::CL::PointListState cl_corners2(worker, FAST_COUNT);
// Create steps.
CVD::CL::PreFastGrayStep fast1(cl_image, cl_corners1, FAST_THRESH);
CVD::CL::FastGrayStep fast2(cl_image, cl_corners1, cl_corners2, FAST_THRESH, FAST_RING);
// Write image data.
cl_image.set(data);
// Run kernels for time.
long const time1 = time(NULL);
for (int i = 0; i < REPEAT; i++)
fast1.execute();
long const time2 = time(NULL);
for (int i = 0; i < REPEAT; i++)
fast2.execute();
long const time3 = time(NULL);
// Read number of corners.
int const ncorners1 = cl_corners1.getCount();
int const ncorners2 = cl_corners2.getCount();
// Calculate microseconds per kernel.
int const us1 = (((time2 - time1) * 1000000) / REPEAT);
int const us2 = (((time3 - time2) * 1000000) / REPEAT);
// Report timing and number of corners.
std::cerr << "OpenCL" << std::endl;
std::cerr << std::setw(12) << ncorners1 << " corners 1" << std::endl;
std::cerr << std::setw(12) << ncorners2 << " corners 2" << std::endl;
std::cerr << std::setw(12) << us1 << " microseconds 1" << std::endl;
std::cerr << std::setw(12) << us2 << " microseconds 2" << std::endl;
std::cerr << std::endl;
}
void clfast(CVD::Image<CVD::byte> const & image) {
try {
// Prepare list for all OpenCL devices on all platforms.
std::vector<cl::Device> devices;
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::cerr << "Found " << platforms.size() << " OpenCL platforms" << std::endl;
for (size_t ip = 0; ip < platforms.size(); ip++) {
cl::Platform &pf = platforms.at(ip);
std::cerr
<< pf.getInfo<CL_PLATFORM_NAME >() << " ("
<< pf.getInfo<CL_PLATFORM_VENDOR >() << ", "
<< pf.getInfo<CL_PLATFORM_VERSION>() << ")"
<< std::endl;
std::vector<cl::Device> newDevices;
try {
pf.getDevices(CL_DEVICE_TYPE_ALL, &newDevices);
std::cerr << " Found " << newDevices.size() << " OpenCL devices" << std::endl;
} catch (cl::Error & err) {
std::cerr << err.what() << " (code " << err.err() << ")" << std::endl;
}
for (size_t id = 0; id < newDevices.size(); id++) {
cl::Device &dev = newDevices.at(id);
std::cerr << " " << dev.getInfo<CL_DEVICE_NAME>() << std::endl;
// Add to list of all devices.
devices.push_back(dev);
}
}
std::cerr << std::endl << std::endl;
for (size_t id = 0; id < devices.size(); id++) {
cl::Device &dev = devices.at(id);
std::cerr << "Running pipeline for \"" << dev.getInfo<CL_DEVICE_NAME>() << "\"" << std::endl;
try {
clfast(dev, image);
} catch (cl::Error & err) {
std::cerr << err.what() << " (code " << err.err() << ")" << std::endl;
}
}
} catch (cl::Error & err) {
std::cerr << err.what() << " (code " << err.err() << ")" << std::endl;
}
}