Skip to content
Browse files

wrote the vector addition, linking problems with opencl lib

  • Loading branch information...
1 parent 705cf0e commit 5f62acb9bb5a37535d5f703a3db4651eeb77fd24 @yglee committed
Showing with 88 additions and 698 deletions.
  1. +12 −3 testcode/CMakeLists.txt
  2. +4 −546 testcode/openCLWrapper.cpp
  3. +4 −90 testcode/openCLWrapper.h
  4. +68 −59 testcode/test_main.cc
View
15 testcode/CMakeLists.txt
@@ -2,10 +2,19 @@ include_directories(${EIGEN3_INCLUDE_DIR})
include_directories(${OPENCL_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
+add_library(OPENCLWRAPPER
+ openCLWrapper.cpp
+)
+
install(FILES
- vector_add_cpu.h
- DESTINATION include/fslam/test # core
+ openCLWrapper.h
+ DESTINATION include/fslam/test
)
-set(FSLAM_USED_LIBS ${OPENCL_LIBRARIES})
+install(TARGETS
+ OPENCLWRAPPER
+ DESTINATION lib
+)
+
+set(FSLAM_USED_LIBS ${OPENCL_LIBRARIES} OPENCLWRAPPER)
add_fslam_tool(fslam_gpu test_main.cc)
View
550 testcode/openCLWrapper.cpp
@@ -1,8 +1,5 @@
#include "openCLWrapper.h"
-#include "openGLUtils.h"
-
-#include "common/tf/stopwatch.h"
#include <iostream>
#include <fstream>
@@ -69,7 +66,6 @@ ErrorMsg openCL_error_msgs[] = {
{CL_INVALID_EVENT_WAIT_LIST, "CL_INVALID_EVENT_WAIT_LIST"},
{CL_INVALID_EVENT, "CL_INVALID_EVENT"},
{CL_INVALID_OPERATION, "CL_INVALID_OPERATION"},
-{CL_INVALID_GL_OBJECT, "CL_INVALID_GL_OBJECT"},
{CL_INVALID_BUFFER_SIZE, "CL_INVALID_BUFFER_SIZE"},
{CL_INVALID_MIP_LEVEL, "CL_INVALID_MIP_LEVEL"},
{CL_INVALID_GLOBAL_WORK_SIZE, "CL_INVALID_GLOBAL_WORK_SIZE"},
@@ -100,49 +96,12 @@ OpenCLBuffer::OpenCLBuffer(OpenCLContext &context,
size_t size,
cl_mem_flags flags,
bool create_ogl_buffer)
-: _context(context), _size(size), _ogl_buffer(0)
+: _context(context), _size(size)
{
cl_int error = 0;
- /*
- // this doesn't seem to be true
- size_t mmas = _context.MaxMemAllocSize();
- if (size > mmas)
- {
- cerr << "Error allocating OpenCL buffer of size " << size
- << ", size exceeds max. mem alloc size of " << mmas << endl;
-
- //return;
- }
- */
-
- if (create_ogl_buffer) {
- // clear any previous errors
- glGetError();
-
- // allocate a pixel buffer of the right size
- glGenBuffers(1, &_ogl_buffer);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, _ogl_buffer);
- glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, 0, GL_STREAM_DRAW_ARB);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
-
- GLenum gl_error = glGetError();
- if (gl_error != GL_NO_ERROR) {
- cerr << "Error: OpenGL error during buffer creation: "
- << gl_error << endl;
-
- glDeleteBuffers(1, &_ogl_buffer);
- _ogl_buffer = 0;
-
- return;
- }
-
- _buffer = clCreateFromGLBuffer(_context._context, flags,
- _ogl_buffer, &error);
- }
- else {
- _buffer = clCreateBuffer(_context._context, flags, size, NULL, &error);
- }
+
+ _buffer = clCreateBuffer(_context._context, flags, size, NULL, &error);
if (error != CL_SUCCESS) {
cerr << "Error allocating OpenCL buffer of size " << size
@@ -150,10 +109,6 @@ OpenCLBuffer::OpenCLBuffer(OpenCLContext &context,
_buffer = NULL;
_size = 0;
- if (_ogl_buffer) {
- glDeleteBuffers(1, &_ogl_buffer);
- _ogl_buffer = 0;
- }
}
}
@@ -161,8 +116,6 @@ OpenCLBuffer::~OpenCLBuffer()
{
if (_buffer)
clReleaseMemObject(_buffer);
- if (_ogl_buffer)
- glDeleteBuffers(1, &_ogl_buffer);
}
bool OpenCLBuffer::UploadData(const void *data, size_t size, size_t offset,
@@ -174,15 +127,10 @@ bool OpenCLBuffer::UploadData(const void *data, size_t size, size_t offset,
return false;
OpenCLMemory *ptr = this;
- if (_ogl_buffer)
- _context.AcquireGLObjects(&ptr, 1);
error = clEnqueueWriteBuffer(_context._queue, _buffer, blocking,
offset, size, data, 0, 0, 0);
- if (_ogl_buffer)
- _context.ReleaseGLObjects(&ptr, 1);
-
if (error != CL_SUCCESS) {
cerr << "Error writing OpenCL buffer: "
<< GetOpenCLErrorString(error) << endl;
@@ -201,15 +149,10 @@ bool OpenCLBuffer::DownloadData(void *data, size_t size, size_t offset,
return false;
OpenCLMemory *ptr = this;
- if (_ogl_buffer)
- _context.AcquireGLObjects(&ptr, 1);
error = clEnqueueReadBuffer(_context._queue, _buffer, blocking,
offset, size, data, 0, 0, 0);
- if (_ogl_buffer)
- _context.ReleaseGLObjects(&ptr, 1);
-
if (error != CL_SUCCESS) {
cerr << "Error reading OpenCL buffer: "
<< GetOpenCLErrorString(error) << endl;
@@ -221,368 +164,6 @@ bool OpenCLBuffer::DownloadData(void *data, size_t size, size_t offset,
////////////////////////////////////////////////////////////////////////////////
-OpenCLTexture::OpenCLTexture(OpenCLContext &context,
- size_t size[3], int channels, int type_size,
- cl_mem_flags flags)
-: _context(context), _channels(channels), _type_size(type_size),
- _ogl_tex(0), _io_buf(0)
-{
- _size[0] = size[0];
- _size[1] = size[1];
- _size[2] = size[2];
-
- glGenTextures(1, &_ogl_tex);
-
- bool is_3d = (_size[2] != 0);
- cl_int error = 0;
-
- // determine the right texture format
- int internal_format;
- int transfer;
- if (channels == 1) {
- if (type_size == 1)
- internal_format = GL_R;
- else if (type_size == 2)
- internal_format = GL_R16F;
- else
- internal_format = GL_R32F;
-
- transfer = GL_R;
- }
- else if (channels == 2) {
- if (type_size == 1)
- internal_format = GL_RG;
- else if (type_size == 2)
- internal_format = GL_RG16F;
- else
- internal_format = GL_RG32F;
-
- transfer = GL_RG;
- }
- else if (channels == 2) {
- if (type_size == 1)
- internal_format = GL_RGB;
- else if (type_size == 2)
- internal_format = GL_RGB16F;
- else
- internal_format = GL_RGB32F;
-
- transfer = GL_RGB;
- }
- else {
- if (type_size == 1)
- internal_format = GL_RGBA;
- else if (type_size == 2)
- internal_format = GL_RGBA16F;
- else
- internal_format = GL_RGBA32F;
-
- transfer = GL_RGBA;
- }
-
- // clear any existing errors
- glGetError();
-
- if (!is_3d) {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, _ogl_tex);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-
- glTexImage2D(GL_TEXTURE_2D, 0, internal_format,
- _size[0], _size[1], 0, transfer, GL_FLOAT, 0);
-
- glDisable(GL_TEXTURE_2D);
-
- _buffer = clCreateFromGLTexture2D(_context._context, flags,
- GL_TEXTURE_2D, 0 /*mip level*/,
- _ogl_tex, &error);
- }
- else {
- glEnable(GL_TEXTURE_3D);
- glBindTexture(GL_TEXTURE_3D, _ogl_tex);
- glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-
- glTexImage3D(GL_TEXTURE_3D, 0, internal_format,
- _size[0], _size[1], _size[2], 0, transfer, GL_FLOAT, 0);
-
- glDisable(GL_TEXTURE_3D);
-
- _buffer = clCreateFromGLTexture3D(_context._context, flags,
- GL_TEXTURE_3D, 0 /*mip level*/,
- _ogl_tex, &error);
- }
-
- GLenum gl_error = glGetError();
- if (gl_error != GL_NO_ERROR) {
- cerr << "Error: OpenGL error during texture creation: "
- << gl_error << endl;
-
- glDeleteTextures(1, &_ogl_tex);
-
- if (_buffer)
- clReleaseMemObject(_buffer);
-
- _buffer = NULL;
- _size[0] = 0;
- _size[1] = 0;
- _size[2] = 0;
-
- return;
- }
-
- if (error != CL_SUCCESS) {
- cerr << "Error allocating OpenCL texture of size "
- << "(" << _size[0] << ", " << _size[1] << ", " << _size[2] << ")"
- << ", error : " << GetOpenCLErrorString(error) << endl;
-
- _buffer = NULL;
- _size[0] = 0;
- _size[1] = 0;
- _size[2] = 0;
- }
-
- glGenBuffers(1, &_io_buf);
-}
-
-OpenCLTexture::OpenCLTexture(OpenCLContext &context,
- int opengl_tex, bool is_3d,
- cl_mem_flags flags)
-: _context(context), _channels(0), _type_size(0),
- _ogl_tex(opengl_tex), _io_buf(0)
-{
- cl_int error = 0;
-
-
-
- if (!is_3d) {
- _buffer = clCreateFromGLTexture2D(_context._context, flags,
- GL_TEXTURE_2D, 0 /*mip level*/,
- opengl_tex, &error);
- }
- else {
- _buffer = clCreateFromGLTexture3D(_context._context, flags,
- GL_TEXTURE_3D, 0 /*mip level*/,
- opengl_tex, &error);
- }
-
- if (error != CL_SUCCESS) {
- cerr << "Error getting OpenCL texture from OpenGL" <<
- ", error : " << GetOpenCLErrorString(error) << endl;
- _buffer = NULL;
- return;
- }
-
- if (!is_3d) {
- int tmp;
- glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &tmp);
- _size[0] = tmp;
- glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &tmp);
- _size[1] = tmp;
- _size[2] = 0;
- }
- else {
- int tmp;
- glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &tmp);
- _size[0] = tmp;
- glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, &tmp);
- _size[1] = tmp;
- glGetTexLevelParameteriv(GL_TEXTURE_3D, 0, GL_TEXTURE_DEPTH, &tmp);
- _size[2] = tmp;
- }
-
- glGenBuffers(1, &_io_buf);
-}
-
-OpenCLTexture::~OpenCLTexture()
-{
- if (_buffer)
- clReleaseMemObject(_buffer);
-
- // only delete the OpenGL textrue if we created it
- if (_type_size != 0)
- glDeleteTextures(1, &_ogl_tex);
-
- if (_io_buf != 0)
- glDeleteBuffers(1, &_io_buf);
-}
-
-bool OpenCLTexture::TransferDataToImage(OpenCLBuffer *data, size_t data_offset,
- size_t size[3], size_t offset[3],
- bool blocking)
-{
- if (offset[0] + size[0] > _size[0] ||
- offset[1] + size[1] > _size[1] ||
- offset[2] + size[2] > _size[2]) {
- cerr << "Warning: TransferDataToImage out of bounds" << endl;
- return false;
- }
-
- if (size[0] == 0 ||
- size[1] == 0 ||
- size[2] == 0) {
- cerr << "Warning: TransferDataToImage has zero size" << endl;
- return false;
- }
-
- OpenCLMemory *ptr = this;
- _context.AcquireGLObjects(&ptr, 1);
-
- cl_int error = 0;
- error = clEnqueueCopyBufferToImage(_context._queue, data->_buffer, _buffer,
- data_offset, offset, size,
- 0, 0, 0);
-
- _context.ReleaseGLObjects(&ptr, 1);
-
- if (error != CL_SUCCESS) {
- cerr << "Error uploading to OpenCL texture: "
- << GetOpenCLErrorString(error) << endl;
- return false;
- }
-
- if (blocking)
- _context.Finish();
-
- return true;
-}
-
-bool OpenCLTexture::TransferDataFromImage(OpenCLBuffer *data,
- size_t data_offset,
- size_t size[3], size_t offset[3],
- bool blocking)
-{
- if (offset[0] + size[0] > _size[0] ||
- offset[1] + size[1] > _size[1] ||
- offset[2] + size[2] > _size[2]) {
- cerr << "Warning: TransferDataFromImage out of bounds" << endl;
- return false;
- }
-
- if (size[0] == 0 ||
- size[1] == 0 ||
- size[2] == 0) {
- cerr << "Warning: TransferDataFromImage has zero size" << endl;
- return false;
- }
-
- OpenCLMemory *ptr = this;
- _context.AcquireGLObjects(&ptr, 1);
-
- cl_int error = 0;
- error = clEnqueueCopyImageToBuffer(_context._queue, _buffer, data->_buffer,
- offset, size, data_offset,
- 0, 0, 0);
-
- _context.ReleaseGLObjects(&ptr, 1);
-
- if (error != CL_SUCCESS) {
- cerr << "Error uploading to OpenCL texture: "
- << GetOpenCLErrorString(error) << endl;
- return false;
- }
-
- if (blocking)
- _context.Finish();
-
- return true;
-}
-
-bool OpenCLTexture::UploadData(const void *data, size_t size[3],
- size_t offset[3], int channels, int type_size)
-{
-
- if (offset[0] + size[0] > _size[0] ||
- offset[1] + size[1] > _size[1] ||
- offset[2] + size[2] > _size[2]) {
- cerr << "Warning: UploadData out of bounds" << endl;
- cerr << "Texture size: "
- << _size[0] << " x " << _size[1] << " x " << _size[2] << endl;
- cerr << "Offset: "
- << offset[0] << " x " << offset[1] << " x " << offset[2] << endl;
- cerr << "Upload size: "
- << size[0] << " x " << size[1] << " x " << size[2] << endl;
- return false;
- }
-
- if (size[0] == 0 ||
- size[1] == 0 ||
- size[2] == 0) {
- cerr << "Warning: UploadData has zero size" << endl;
- return false;
- }
-
- int format;
- int transfer;
- if (type_size == 1)
- format = GL_UNSIGNED_BYTE;
- else if (type_size == 2)
- format = GL_HALF_FLOAT_ARB;
- else
- format = GL_FLOAT;
-
- if (channels == 1)
- transfer = GL_R;
- else if (channels == 2)
- transfer = GL_RG;
- else if (channels == 2)
- transfer = GL_RGB;
- else
- transfer = GL_RGBA;
-
- size_t bytes = size[0] * size[1] * size[2] * channels * type_size;
-
- // clear any existing errors
- glGetError();
-
- if (_size[2] == 0) {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, _ogl_tex);
-
- glTexSubImage2D(GL_TEXTURE_2D, 0, offset[0], offset[1],
- size[0], size[1], transfer, format, data);
-
- glDisable(GL_TEXTURE_2D);
- } else {
- glEnable(GL_TEXTURE_3D);
- glBindTexture(GL_TEXTURE_3D, _ogl_tex);
-
- // using the pbuffer is way faster than doing it straight up
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, _io_buf);
- glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, bytes, NULL, GL_STREAM_DRAW);
-
- void *io_mem = glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
- assert(io_mem);
- memcpy(io_mem, data, bytes);
- glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
-
- glTexSubImage3D(GL_TEXTURE_3D, 0, offset[0], offset[1], offset[2],
- size[0], size[1], size[2], transfer, format, 0);
-
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
-
- glDisable(GL_TEXTURE_3D);
- }
-
- GLenum error = glGetError();
- if (error != GL_NO_ERROR) {
- cerr << "Error: OpenGL error during texture upload: " << error << endl;
- return false;
- }
-
- return true;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
OpenCLKernel::~OpenCLKernel()
{
if (_kernel)
@@ -623,22 +204,6 @@ bool OpenCLKernel::SetBuf(int argNo, OpenCLBuffer *buf)
return false;
}
-bool OpenCLKernel::SetTexture(int argNo, OpenCLTexture *tex)
-{
- if (!_kernel)
- return false;
-
- cl_int error = clSetKernelArg(_kernel, argNo,
- sizeof(cl_mem), &tex->_buffer);
- if (error == CL_SUCCESS)
- return true;
-
- std::cerr << "Error setting kernel argument: "
- << GetOpenCLErrorString(error) << std::endl;
-
- return false;
-}
-
bool OpenCLKernel::RunKernel(int dim, const size_t *workSize,
const size_t *localWorkSize)
{
@@ -766,26 +331,7 @@ OpenCLContext::OpenCLContext(bool openGLSupport)
0
};
- // make sure we have an OpenGL context before we try to allocate a OpenCL
- // context that supports it
- if (_supportsOpenGL &&
- (glXGetCurrentContext() == NULL || glXGetCurrentDisplay() == NULL)) {
- cerr << "Error getting OpenCL context (no OpenGL context)" << endl;
-
- _device = 0;
- _platform = 0;
-
- return;
- }
-
- cl_context_properties gl_props[] = {
- CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
- CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),
- CL_CONTEXT_PLATFORM, (cl_context_properties)(_platform),
- 0
- };
-
- _context = clCreateContext((_supportsOpenGL ? gl_props : props), 1,
+ _context = clCreateContext(props, 1,
&_device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
cerr << "Error creating context: "
@@ -923,55 +469,6 @@ OpenCLBuffer *OpenCLContext::AllocMemBuffer(size_t size, cl_mem_flags flags)
return NULL;
}
-OpenCLBuffer *OpenCLContext::AllocGLMemBuffer(size_t size, cl_mem_flags flags)
-{
- if (!_context)
- return NULL;
-
- OpenCLBuffer *buf = new OpenCLBuffer(*this, size, flags, true);
-
- if (buf->IsValid())
- return buf;
-
- delete buf;
-
- return NULL;
-}
-
-OpenCLTexture *OpenCLContext::AllocTexture(size_t size[3], int channels,
- int type_size, cl_mem_flags flags)
-{
- if (!_context)
- return NULL;
-
- OpenCLTexture *tex = new OpenCLTexture(*this, size, channels,
- type_size, flags);
-
- if (tex->IsValid())
- return tex;
-
- delete tex;
-
- return NULL;
-}
-
-OpenCLTexture *OpenCLContext::GetTextureFromGL(unsigned int ogl_tex,
- bool is_3d,
- cl_mem_flags flags)
-{
- if (!_context)
- return NULL;
-
- OpenCLTexture *tex = new OpenCLTexture(*this, ogl_tex, is_3d, flags);
-
- if (tex->IsValid())
- return tex;
-
- delete tex;
-
- return NULL;
-}
-
void OpenCLContext::Finish()
{
if (!_context)
@@ -986,42 +483,3 @@ void OpenCLContext::Finish()
}
}
-bool OpenCLContext::AcquireGLObjects(OpenCLMemory **objs, int num_objs)
-{
- vector<cl_mem> memobj;
-
- memobj.resize(num_objs);
- for (int i = 0; i < num_objs; ++i)
- memobj[i] = objs[i]->_buffer;
-
- cl_int error = clEnqueueAcquireGLObjects(_queue, num_objs, &memobj[0],
- 0, NULL, NULL);
- if (error != CL_SUCCESS) {
- cerr << "Error AcquireGLObjects: "
- << GetOpenCLErrorString(error) << endl;
- return false;
- }
-
- return true;
-}
-
-void OpenCLContext::ReleaseGLObjects(OpenCLMemory **objs, int num_objs)
-{
- vector<cl_mem> memobj;
-
- memobj.resize(num_objs);
- for (int i = 0; i < num_objs; ++i)
- memobj[i] = objs[i]->_buffer;
-
- cl_int error = clEnqueueReleaseGLObjects(_queue, num_objs, &memobj[0],
- 0, NULL, NULL);
- if (error != CL_SUCCESS) {
- cerr << "Error ReleaseGLObjects: "
- << GetOpenCLErrorString(error) << endl;
- return;
- }
-
- return;
-}
-
-
View
94 testcode/openCLWrapper.h
@@ -3,17 +3,14 @@
#ifndef OPENCL_WRAPPER_H
#define OPENCL_WRAPPER_H
-//#include <CL/cl.h>
-#include <OpenCL/OpenCL.h>
-//#include <CL/cl_gl.h>
-
+#include <OpenCL/cl.h>
+#include <OpenCL/cl_gl.h>
#include <string>
#include <iostream>
// forward decl
class OpenCLContext;
class OpenCLProgram;
-class OpenCLTexture;
// defines
#define TEXTURE_TYPE_UBYTE 1
@@ -49,7 +46,6 @@ class OpenCLBuffer : public OpenCLMemory
{
friend class OpenCLContext;
friend class OpenCLKernel;
- friend class OpenCLTexture;
public:
~OpenCLBuffer();
@@ -62,10 +58,6 @@ class OpenCLBuffer : public OpenCLMemory
{
return _size;
}
- unsigned int GetGLBuffer() const
- {
- return _ogl_buffer;
- }
// upload of data to the memory buffer
// it checks that the transfer does not exceed the bounds of the buffer
@@ -87,69 +79,10 @@ class OpenCLBuffer : public OpenCLMemory
OpenCLContext &_context;
size_t _size;
- unsigned int _ogl_buffer;
+ //unsigned int _ogl_buffer;
};
////////////////////////////////////////////////////////////////////////////////
-// OpenCL texture abstraction
-
-class OpenCLTexture : public OpenCLMemory
-{
- friend class OpenCLContext;
- friend class OpenCLKernel;
-
-public:
- ~OpenCLTexture();
-
- bool IsValid() const
- {
- return _buffer != NULL;
- }
- const size_t *GetSize() const
- {
- return _size;
- }
- unsigned int GetOpenGLTexture() const
- {
- return _ogl_tex;
- }
-
- // upload of data from one OpenCLBuffer to a texture
- // it checks that the transfer does not exceed the bounds of the image
- // the data needs to be in the internal format of the texture
- // if blocking is false the source buffer must be kept around until the
- // transfer completes
- bool TransferDataToImage(OpenCLBuffer *data, size_t data_offset,
- size_t size[3], size_t offset[3],
- bool blocking = true);
-
- // download data from a texture to an OpenCLBuffer
- // it checks that the transfer does not exceed the bounds of he buffer
- // if blocking is false the destination buffer must be kept around until
- // the transfer completes
- bool TransferDataFromImage(OpenCLBuffer *data, size_t data_offset,
- size_t size[3], size_t offset[3],
- bool blocking = true);
-
- bool UploadData(const void *data, size_t size[3], size_t offset[3],
- int channels, int type_size);
-
-private:
- OpenCLTexture(OpenCLContext &context, size_t size[3],
- int channels, int type_size, cl_mem_flags flags);
- OpenCLTexture(OpenCLContext &context, int opengl_tex, bool is_3d,
- cl_mem_flags flags);
-
- OpenCLContext &_context;
- size_t _size[3];
- int _channels;
- int _type_size;
- unsigned int _ogl_tex;
- unsigned int _io_buf;
-};
-
-
-////////////////////////////////////////////////////////////////////////////////
// OpenCL kernel abstraction
class OpenCLKernel
@@ -172,7 +105,6 @@ class OpenCLKernel
bool SetArray(int argNo, T *arg, int numElem);
bool SetBuf(int argNo, OpenCLBuffer *buf);
- bool SetTexture(int argNo, OpenCLTexture *tex);
// call the kernel
bool RunKernel(int dim, const size_t *workSize, const size_t *localWorkSize = NULL);
@@ -271,12 +203,6 @@ class OpenCLContext
return _context != NULL;
}
- //
- bool HasOpenGLSupport() const
- {
- return _supportsOpenGL;
- }
-
// Queries to capabilities of the OpenCL device
bool HasImagingSupport() const;
size_t MaxWorkGroupSize() const;
@@ -289,22 +215,10 @@ class OpenCLContext
// Create mem buffer
OpenCLBuffer *AllocMemBuffer(size_t size, cl_mem_flags flags);
- OpenCLBuffer *AllocGLMemBuffer(size_t size, cl_mem_flags flags);
-
- // create a texture
- // type_size = 1, 2, 4 -> byte, half , float
- OpenCLTexture *AllocTexture(size_t size[3], int channels,
- int type_size, cl_mem_flags);
- OpenCLTexture *GetTextureFromGL(unsigned int ogl_tex,
- bool is_3d, cl_mem_flags);
// Flow control
void Finish();
-
- // OpenGL interop
- bool AcquireGLObjects(OpenCLMemory **objs, int num_objs);
- void ReleaseGLObjects(OpenCLMemory **objs, int num_objs);
-
+
private:
cl_platform_id _platform;
cl_device_id _device;
View
127 testcode/test_main.cc
@@ -2,87 +2,96 @@
#include "openCLWrapper.h"
#include <assert.h>
+#include <iostream>
+#include <math.h>
#define PROGRAM_FILE "vector_add_gpu.cl"
using namespace std;
+
+const int NUM_SRC_PARAMS = 2;
+const int NUM_RET_PARAMS = 1;
+const int VEC_SIZE = 1234567;
+
+size_t shrRoundUp( const size_t group_size, const size_t global_size )
+{
+ size_t r = global_size % group_size;
+ if( r == 0 )
+ return global_size;
+ return global_size + group_size - r;
+}
+
int main()
{
+ OpenCLContext context;
+ OpenCLKernel *math_kernel;
- cl_int error = 0; // Used to handle error codes
- cl_platform_id platform;
- cl_context context;
- cl_command_queue queue;
- cl_device_id device;
+ //initialize context (sets up platform, device, context, command queue)
+ context = OpenCLContext(0);
+ //get kernel paths (hard coded for now)
+ string kernelpaths = "/Users/ylee8/FastSLAM/testcode";
- // Platform
- error = clGetPlatformIDs(1, &platform, 0);
-
- if (error != CL_SUCCESS) {
- cout << "Error getting platform id: " <<error << endl;
- exit(error);
- }
- // Device
- error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
- if (error != CL_SUCCESS) {
- cout << "Error getting device ids: " << error << endl;
- exit(error);
+ //create program (CreateProgramFromFile also builds the prog)
+ OpenCLProgram *prog = context.CreateProgramFromFile(kernelpaths + PROGRAM_FILE);
+ if(!prog) {
+ cerr<<"Error: couldn't load OpenCL program!"<<endl;
+ return 0;
}
- // Context
- context = clCreateContext(0, 1, &device, NULL, NULL, &error);
- if (error != CL_SUCCESS) {
- cout << "Error creating context: " << error << endl;
- exit(error);
- }
- // Command-queue
- queue = clCreateCommandQueue(context, device, 0, &error);
- if (error != CL_SUCCESS) {
- cout << "Error creating command queue: " << error << endl;
- exit(error);
- }
-
- //allocate memory
- const int size =12345667;
- float *src_a_h = new float[size];
- float *src_b_h = new float[size];
- float *res_h = new float[size];
- //initialize both vectors
- for (int i=0; i< size; i++) {
- src_a_h[i] = src_b_h[i] = (float) i;
+ //extract the kernel
+ math_kernel = prog->CreateKernel("vecAdd_k");
+ if (!math_kernel) {
+ cerr<<"Error: couldn't load OpenCL Kernel!"<<endl;
}
- const int mem_size = sizeof(float)*size;
- //allocates a buffer of size mem_size and copies mem_size bytes from src_a_h
- cl_mem src_a_d = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, mem_size, src_a_h, &error);
- cl_mem src_b_d = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, mem_size, src_b_h, &error);
- cl_mem res_d = clCreateBuffer(context, CL_MEM_WRITE_ONLY, mem_size, NULL, &error);
+ //////////////////////////
+ // allocate mem for source
+ //////////////////////////
- //creates the program
- size_t src_size=0;
+ //allocates a buffer of size mem_size and copies mem_size bytes from src1_dataf and src2_dataf
+ OpenCLBuffer *src_params;
+ const int mem_size = VEC_SIZE * sizeof(float);
+ src_params = context.AllocMemBuffer(mem_size,CL_MEM_READ_ONLY);
- //get kernel path
- string kernelpaths = "/Users/ylee8/FastSLAM/testcode"; //BuildRegGetPackageLocation( "FastSLAM" ) + "/testcode/"
+ // Initialize both vectors
+ float* src1_dataf = new float[VEC_SIZE];
+ float* src2_dataf = new float[VEC_SIZE];
+ for (int i = 0; i < VEC_SIZE; i++) {
+ src1_dataf[i] = src2_dataf[i] = (float) i;
+ }
+ src_params->UploadData(src1_dataf, VEC_SIZE * sizeof(float));
+ src_params->UploadData(src2_dataf, VEC_SIZE * sizeof(float));
+
+ //////////////////////////
+ // allocate mem for result
+ //////////////////////////
- //get cl_program
- cl_program program = context.CreateProgramFromFile(kernelpaths + PROGRAM_FILE);
+ OpenCLBuffer *res_params;
+ res_params = context.AllocMemBuffer(mem_size,CL_MEM_WRITE_ONLY);
+
+ /////////////////////
+ // launching kernel
+ /////////////////////
- //build the program
- error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
- assert(error == CL_SUCCESS);
+ //set up the kernel args (calls clKernelArgs)
+ math_kernel->SetBuf(0,src_params); //SetBuf(which argument, buffer)
+ math_kernel->SetBuf(1,res_params);
- //shows the log
- char* build_log;
- size_t log_size;
- //First call to know the proper size
+ //context finish (blocks until everything in CL queue is processed
+ context.Finish();
+ //run the kernel
+ const size_t global_size_offset = NULL; //
+ const size_t local_work_size = 512; //total number of work items
+ const size_t global_work_size = shrRoundUp(local_work_size,VEC_SIZE); //num of work items per work group
+ int dim = 1;
-}
-
-string BuildRegGetPackageLocation(string packagename) {
+ math_kernel->RunKernel(dim, &global_work_size, &local_work_size);
+ context.Finish();
+ return 1;
}

0 comments on commit 5f62acb

Please sign in to comment.
Something went wrong with that request. Please try again.