diff --git a/openbr/plugins/cuda/GpuMatManager.cpp b/openbr/plugins/cuda/GpuMatManager.cpp deleted file mode 100644 index 187a549..0000000 --- a/openbr/plugins/cuda/GpuMatManager.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include - -#include - -#include "GpuMatManager.hpp" - -using namespace cv; -using namespace cv::gpu; - -namespace br { namespace cuda { - GpuMatManager::GpuMatManager(int num) { - _numMats = num; - - // initialize the GpuMats - _mats = (GpuMat**)malloc(num * sizeof(GpuMat*)); - _matTaken = (bool**)malloc(num * sizeof(bool*)); - for (int i=0; i < num; i++) { - _mats[i] = new GpuMat(); - _matTaken[i] = new bool; - (*_matTaken[i]) = false; - } - - // initialize the locks - _matTakenLock = new pthread_mutex_t; - pthread_mutex_init(_matTakenLock, NULL); - _openCvOperationLock = new pthread_mutex_t; - pthread_mutex_init(_openCvOperationLock, NULL); - - // initialize the semaphore - _matSemaphore = new sem_t; - sem_init(_matSemaphore, 0, _numMats); - } - - GpuMat* GpuMatManager::reserve() { - GpuMat* reservedMat = NULL; - - // get the reserved GpuMat - //sem_wait(_matSemaphore); - pthread_mutex_lock(_matTakenLock); - for (int i=0; i < _numMats; i++) { - if ( !(*_matTaken[i]) ) { - reservedMat = _mats[i]; - *_matTaken[i] = true; - break; - } - } - pthread_mutex_unlock(_matTakenLock); - - return reservedMat; - } - - void GpuMatManager::upload(GpuMat* reservedMat, Mat& mat) { - // check the image Dimensions - if (reservedMat->size() != mat.size()) { - pthread_mutex_lock(_openCvOperationLock); - reservedMat->release(); - reservedMat->create(mat.size(), mat.type()); - pthread_mutex_unlock(_openCvOperationLock); - } - - // upload the image - pthread_mutex_lock(_openCvOperationLock); - reservedMat->upload(mat); - pthread_mutex_unlock(_openCvOperationLock); - pthread_mutex_lock(_openCvOperationLock); - reservedMat->upload(mat); - pthread_mutex_unlock(_openCvOperationLock); - } - - void GpuMatManager::matchDimensions(GpuMat* srcMat, GpuMat* dstMat) { - if (srcMat->size() != dstMat->size()) { - pthread_mutex_lock(_openCvOperationLock); - dstMat->release(); - dstMat->create(srcMat->size(), srcMat->type()); - pthread_mutex_unlock(_openCvOperationLock); - } - } - - void GpuMatManager::download(GpuMat* reservedMat, Mat& dstMat) { - pthread_mutex_lock(_openCvOperationLock); - reservedMat->download(dstMat); - pthread_mutex_unlock(_openCvOperationLock); - } - - void GpuMatManager::release(GpuMat* reservedMat) { - pthread_mutex_lock(_matTakenLock); - bool foundMatch = false; - for (int i=0; i < _numMats; i++) { - if (reservedMat == _mats[i]) { - *_matTaken[i] = false; - foundMatch = true; - } - } - pthread_mutex_unlock(_matTakenLock); - - // return unconditionally if we didn't find a match - if (!foundMatch) { - return; - } - - sem_post(_matSemaphore); - } - - GpuMatManager::~GpuMatManager() { - // assume a single thread is destroying the manager - // TODO(colin): add the destroy code - } - -}} diff --git a/openbr/plugins/cuda/GpuMatManager.hpp b/openbr/plugins/cuda/GpuMatManager.hpp deleted file mode 100644 index 86fb169..0000000 --- a/openbr/plugins/cuda/GpuMatManager.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -#include -#include - -using namespace cv; -using namespace cv::gpu; - -namespace br { namespace cuda { - class GpuMatManager { - private: - int _numMats; - GpuMat** _mats; // holds all the mats - bool** _matTaken; // holds whether or not they are taken - - pthread_mutex_t* _matTakenLock; // lock for matTaken table - pthread_mutex_t* _openCvOperationLock; // lock for OpenCV upload/download/realloc operations - sem_t* _matSemaphore; - - public: - GpuMatManager(int num); - - GpuMat* reserve(); - void upload(GpuMat* reservedMat, Mat& mat); - void matchDimensions(GpuMat* srcMat, GpuMat* dstMat); - void download(GpuMat* reservedMat, Mat& dstMat); - void release(GpuMat* mat); - - ~GpuMatManager(); - }; -}} diff --git a/openbr/plugins/cuda/MatManager.cu b/openbr/plugins/cuda/MatManager.cu new file mode 100644 index 0000000..4823249 --- /dev/null +++ b/openbr/plugins/cuda/MatManager.cu @@ -0,0 +1,116 @@ +#include +#include + +#include + +#include "MatManager.hpp" + +using namespace cv; +using namespace cv::gpu; + +namespace br { namespace cuda { + MatManager::MatManager(int num) { + _numMats = num; + + // initialize the an array of Mats + _mats = (uint8_t**)malloc(num * sizeof(uint8_t*)); + _matTaken = (bool*)malloc(num * sizeof(bool)); + _matsDimension = (int*)malloc(num * sizeof(int)); + + for (int i=0; i < num; i++) { + cudaMalloc(&_mats[i], 1 * sizeof(uint8_t)); + + // initialize matTaken + _matTaken[i] = false; + + // initialize all mat dimensions to be 1 + _matsDimension[i] = 1; + } + + // initialize the locks + _matTakenLock = new pthread_mutex_t; + pthread_mutex_init(_matTakenLock, NULL); + _matsDimensionLock = new pthread_mutex_t; + pthread_mutex_init(_matsDimensionLock, NULL); + + // initialize the semaphore + _matSemaphore = new sem_t; + sem_init(_matSemaphore, 0, _numMats); + } + + MatManager::matindex MatManager::reserve(Mat &mat) { + int reservedMatIndex = 0; + + sem_wait(_matSemaphore); + pthread_mutex_lock(_matTakenLock); + int i; + for (i=0; i < _numMats; i++) { + if ( !_matTaken[i] ) { + _matTaken[i] = true; + reservedMatIndex = i; + break; + } + } + if (i == _numMats) { + std::cout << "Cannot reserve a mat. Not enough GpuMat resourses\n" << std::endl << std::flush; + } + + pthread_mutex_unlock(_matTakenLock); + + // reallocate if size does not match + pthread_mutex_lock(_matsDimensionLock); + if (_matsDimension[reservedMatIndex] != mat.rows * mat.cols) { + cudaFree(_mats[reservedMatIndex]); // free the previous memory first + cudaMalloc(&_mats[reservedMatIndex], mat.rows * mat.cols * sizeof(uint8_t)); + // change the dimension of that matrix + _matsDimension[reservedMatIndex] = mat.rows * mat.cols; + + } + pthread_mutex_unlock(_matsDimensionLock); + return reservedMatIndex; + } + + void MatManager::upload(MatManager::matindex reservedMatIndex, Mat& mat) { + // copy the content of the Mat to GPU + uint8_t* reservedMat = _mats[reservedMatIndex]; + cudaMemcpy(reservedMat, mat.ptr(), mat.rows * mat.cols, cudaMemcpyHostToDevice); + } + + void MatManager::download(MatManager::matindex reservedMatIndex, Mat& dstMat) { + // copy the mat data back + int dimension = dstMat.rows * dstMat.cols; + uint8_t* reservedMat = _mats[reservedMatIndex]; + cudaMemcpy(dstMat.ptr(), reservedMat, dimension, cudaMemcpyDeviceToHost); + } + + void MatManager::release(MatManager::matindex reservedMatIndex) { + uint8_t* reservedMat = _mats[reservedMatIndex]; + pthread_mutex_lock(_matTakenLock); + bool foundMatch = false; + for (int i=0; i < _numMats; i++) { + if (reservedMat == _mats[i]) { + _matTaken[i] = false; + foundMatch = true; + } + } + pthread_mutex_unlock(_matTakenLock); + + // return unconditionally if we didn't find a match + if (!foundMatch) { + std::cout << "Reservedmat is not in the _mats array" << std::endl << std::flush; + return; + } + sem_post(_matSemaphore); + } + + MatManager::~MatManager() { + // assume a single thread is destroying the manager + // TODO(colin): add the destroy code + //std::cout << "Start to destroy.." << std::endl << std::flush; + } + + uint8_t* MatManager::get_mat_pointer_from_index(MatManager::matindex matIndex) { + return _mats[matIndex]; + } + +}} diff --git a/openbr/plugins/cuda/MatManager.hpp b/openbr/plugins/cuda/MatManager.hpp new file mode 100644 index 0000000..e58ad42 --- /dev/null +++ b/openbr/plugins/cuda/MatManager.hpp @@ -0,0 +1,39 @@ +/* +NOTES +Mat reservations should return a handle instead of a pointer +*/ + +#include +#include + +#include +#include + +using namespace cv; +using namespace cv::gpu; + +namespace br { namespace cuda { + class MatManager { + private: + int _numMats; + uint8_t** _mats; // holds all the mats + bool* _matTaken; // holds whether or not they are taken + int* _matsDimension; // holds the dimension of the Mats + + pthread_mutex_t* _matTakenLock; // lock for matTaken table + pthread_mutex_t* _matsDimensionLock; // lock for _matsDimension table and _mats table + sem_t* _matSemaphore; + + public: + typedef int matindex; + MatManager(int num); + + int reserve(Mat &mat); + void upload(matindex reservedMatIndex, Mat& mat); + void download(matindex reservedMatIndex, Mat& dstMat); + void release(matindex matIndex); + uint8_t* get_mat_pointer_from_index(matindex matIndex); + + ~MatManager(); + }; +}} diff --git a/openbr/plugins/cuda/cudalbp.cpp b/openbr/plugins/cuda/cudalbp.cpp index dca1b79..08f0227 100644 --- a/openbr/plugins/cuda/cudalbp.cpp +++ b/openbr/plugins/cuda/cudalbp.cpp @@ -33,7 +33,7 @@ #include #include "cudalbp.hpp" -#include "GpuMatManager.hpp" +#include "MatManager.hpp" using namespace cv; @@ -90,7 +90,7 @@ class CUDALBPTransform : public UntrainableTransform uchar null; - cuda::GpuMatManager* matManager; + cuda::MatManager* matManager; public: /* Returns the number of 0->1 or 1->0 transitions in i */ @@ -143,7 +143,7 @@ class CUDALBPTransform : public UntrainableTransform lut[i] = null; // Set to null id // init the mat manager for managing 10 mats - matManager = new cuda::GpuMatManager(10); + matManager = new cuda::MatManager(10); // copy lut over to the GPU br::cuda::cudalbp_init_wrapper(lut, &lutGpuPtr); @@ -154,17 +154,17 @@ class CUDALBPTransform : public UntrainableTransform void project(const Template &src, Template &dst) const { Mat& m = (Mat&)src.m(); - - GpuMat* a; - GpuMat* b; - a = matManager->reserve(); + cuda::MatManager::matindex a; + cuda::MatManager::matindex b; + a = matManager->reserve(m); matManager->upload(a, m); // reserve the second mat and check the dimensiosn - b = matManager->reserve(); - matManager->matchDimensions(b, a); - - br::cuda::cudalbp_wrapper(*a, *b, lutGpuPtr); + b = matManager->reserve(m); + + uint8_t* srcMatPtr = matManager->get_mat_pointer_from_index(a); + uint8_t* dstMatPtr = matManager->get_mat_pointer_from_index(b); + br::cuda::cudalbp_wrapper(srcMatPtr, dstMatPtr, lutGpuPtr, m.cols, m.rows, m.step1()); matManager->download(b, dst); diff --git a/openbr/plugins/cuda/cudalbp.cu b/openbr/plugins/cuda/cudalbp.cu index 84644f0..23aec27 100644 --- a/openbr/plugins/cuda/cudalbp.cu +++ b/openbr/plugins/cuda/cudalbp.cu @@ -36,15 +36,8 @@ namespace br { namespace cuda { dstRowPtr[colInd] = val; } - void cudalbp_wrapper(GpuMat& src, GpuMat& dst, uint8_t* lut) + void cudalbp_wrapper(uint8_t* srcPtr, uint8_t* dstPtr, uint8_t* lut, int imageWidth, int imageHeight, size_t step) { - // convert the GpuMats to pointers - uint8_t* srcPtr = (uint8_t*)src.data; - uint8_t* dstPtr = (uint8_t*)dst.data; - - int imageWidth = src.cols; - int imageHeight = src.rows; - // make 8 * 8 = 64 square block dim3 threadsPerBlock(8, 8); @@ -55,7 +48,7 @@ namespace br { namespace cuda { //printf("Dst Image Dimesions:\n\trows: %d\tcols: %d\n", dst.rows, dst.cols); //printf("Running CUDALBP\nBlock Dimensions:\n\tx: %d\ty: %d\n", numBlocks.x, numBlocks.y); - cudalbp_kernel<<>>(srcPtr, dstPtr, src.step, dst.step, imageHeight, imageWidth, lut); + cudalbp_kernel<<>>(srcPtr, dstPtr, step, step, imageHeight, imageWidth, lut); } void cudalbp_init_wrapper(uint8_t* lut, uint8_t** lutGpuPtrPtr) { diff --git a/openbr/plugins/cuda/cudalbp.hpp b/openbr/plugins/cuda/cudalbp.hpp index f6569be..7fa270c 100644 --- a/openbr/plugins/cuda/cudalbp.hpp +++ b/openbr/plugins/cuda/cudalbp.hpp @@ -5,5 +5,5 @@ using namespace cv::gpu; namespace br { namespace cuda { void cudalbp_init_wrapper(uint8_t* lut, uint8_t** lutGpuPtrPtr); - void cudalbp_wrapper(GpuMat& src, GpuMat& dst, uint8_t* lut); + void cudalbp_wrapper(uint8_t* src, uint8_t* dst, uint8_t* lut, int imageWidth, int imageHeight, size_t step); }}