Commit bdfd554c941948f5b62d215516179272997fe665
Merge branch 'trunk' of github.com:CGLG/openbr into trunk
Showing
7 changed files
with
169 additions
and
163 deletions
openbr/plugins/cuda/GpuMatManager.cpp deleted
| 1 | -#include <pthread.h> | |
| 2 | -#include <semaphore.h> | |
| 3 | - | |
| 4 | -#include <opencv2/opencv.hpp> | |
| 5 | - | |
| 6 | -#include "GpuMatManager.hpp" | |
| 7 | - | |
| 8 | -using namespace cv; | |
| 9 | -using namespace cv::gpu; | |
| 10 | - | |
| 11 | -namespace br { namespace cuda { | |
| 12 | - GpuMatManager::GpuMatManager(int num) { | |
| 13 | - _numMats = num; | |
| 14 | - | |
| 15 | - // initialize the GpuMats | |
| 16 | - _mats = (GpuMat**)malloc(num * sizeof(GpuMat*)); | |
| 17 | - _matTaken = (bool**)malloc(num * sizeof(bool*)); | |
| 18 | - for (int i=0; i < num; i++) { | |
| 19 | - _mats[i] = new GpuMat(); | |
| 20 | - _matTaken[i] = new bool; | |
| 21 | - (*_matTaken[i]) = false; | |
| 22 | - } | |
| 23 | - | |
| 24 | - // initialize the locks | |
| 25 | - _matTakenLock = new pthread_mutex_t; | |
| 26 | - pthread_mutex_init(_matTakenLock, NULL); | |
| 27 | - _openCvOperationLock = new pthread_mutex_t; | |
| 28 | - pthread_mutex_init(_openCvOperationLock, NULL); | |
| 29 | - | |
| 30 | - // initialize the semaphore | |
| 31 | - _matSemaphore = new sem_t; | |
| 32 | - sem_init(_matSemaphore, 0, _numMats); | |
| 33 | - } | |
| 34 | - | |
| 35 | - GpuMat* GpuMatManager::reserve() { | |
| 36 | - GpuMat* reservedMat = NULL; | |
| 37 | - | |
| 38 | - // get the reserved GpuMat | |
| 39 | - //sem_wait(_matSemaphore); | |
| 40 | - pthread_mutex_lock(_matTakenLock); | |
| 41 | - for (int i=0; i < _numMats; i++) { | |
| 42 | - if ( !(*_matTaken[i]) ) { | |
| 43 | - reservedMat = _mats[i]; | |
| 44 | - *_matTaken[i] = true; | |
| 45 | - break; | |
| 46 | - } | |
| 47 | - } | |
| 48 | - pthread_mutex_unlock(_matTakenLock); | |
| 49 | - | |
| 50 | - return reservedMat; | |
| 51 | - } | |
| 52 | - | |
| 53 | - void GpuMatManager::upload(GpuMat* reservedMat, Mat& mat) { | |
| 54 | - // check the image Dimensions | |
| 55 | - if (reservedMat->size() != mat.size()) { | |
| 56 | - pthread_mutex_lock(_openCvOperationLock); | |
| 57 | - reservedMat->release(); | |
| 58 | - reservedMat->create(mat.size(), mat.type()); | |
| 59 | - pthread_mutex_unlock(_openCvOperationLock); | |
| 60 | - } | |
| 61 | - | |
| 62 | - // upload the image | |
| 63 | - pthread_mutex_lock(_openCvOperationLock); | |
| 64 | - reservedMat->upload(mat); | |
| 65 | - pthread_mutex_unlock(_openCvOperationLock); | |
| 66 | - pthread_mutex_lock(_openCvOperationLock); | |
| 67 | - reservedMat->upload(mat); | |
| 68 | - pthread_mutex_unlock(_openCvOperationLock); | |
| 69 | - } | |
| 70 | - | |
| 71 | - void GpuMatManager::matchDimensions(GpuMat* srcMat, GpuMat* dstMat) { | |
| 72 | - if (srcMat->size() != dstMat->size()) { | |
| 73 | - pthread_mutex_lock(_openCvOperationLock); | |
| 74 | - dstMat->release(); | |
| 75 | - dstMat->create(srcMat->size(), srcMat->type()); | |
| 76 | - pthread_mutex_unlock(_openCvOperationLock); | |
| 77 | - } | |
| 78 | - } | |
| 79 | - | |
| 80 | - void GpuMatManager::download(GpuMat* reservedMat, Mat& dstMat) { | |
| 81 | - pthread_mutex_lock(_openCvOperationLock); | |
| 82 | - reservedMat->download(dstMat); | |
| 83 | - pthread_mutex_unlock(_openCvOperationLock); | |
| 84 | - } | |
| 85 | - | |
| 86 | - void GpuMatManager::release(GpuMat* reservedMat) { | |
| 87 | - pthread_mutex_lock(_matTakenLock); | |
| 88 | - bool foundMatch = false; | |
| 89 | - for (int i=0; i < _numMats; i++) { | |
| 90 | - if (reservedMat == _mats[i]) { | |
| 91 | - *_matTaken[i] = false; | |
| 92 | - foundMatch = true; | |
| 93 | - } | |
| 94 | - } | |
| 95 | - pthread_mutex_unlock(_matTakenLock); | |
| 96 | - | |
| 97 | - // return unconditionally if we didn't find a match | |
| 98 | - if (!foundMatch) { | |
| 99 | - return; | |
| 100 | - } | |
| 101 | - | |
| 102 | - sem_post(_matSemaphore); | |
| 103 | - } | |
| 104 | - | |
| 105 | - GpuMatManager::~GpuMatManager() { | |
| 106 | - // assume a single thread is destroying the manager | |
| 107 | - // TODO(colin): add the destroy code | |
| 108 | - } | |
| 109 | - | |
| 110 | -}} |
openbr/plugins/cuda/GpuMatManager.hpp deleted
| 1 | -#include <pthread.h> | |
| 2 | -#include <semaphore.h> | |
| 3 | - | |
| 4 | -#include <opencv2/opencv.hpp> | |
| 5 | -#include <opencv2/gpu/gpu.hpp> | |
| 6 | - | |
| 7 | -using namespace cv; | |
| 8 | -using namespace cv::gpu; | |
| 9 | - | |
| 10 | -namespace br { namespace cuda { | |
| 11 | - class GpuMatManager { | |
| 12 | - private: | |
| 13 | - int _numMats; | |
| 14 | - GpuMat** _mats; // holds all the mats | |
| 15 | - bool** _matTaken; // holds whether or not they are taken | |
| 16 | - | |
| 17 | - pthread_mutex_t* _matTakenLock; // lock for matTaken table | |
| 18 | - pthread_mutex_t* _openCvOperationLock; // lock for OpenCV upload/download/realloc operations | |
| 19 | - sem_t* _matSemaphore; | |
| 20 | - | |
| 21 | - public: | |
| 22 | - GpuMatManager(int num); | |
| 23 | - | |
| 24 | - GpuMat* reserve(); | |
| 25 | - void upload(GpuMat* reservedMat, Mat& mat); | |
| 26 | - void matchDimensions(GpuMat* srcMat, GpuMat* dstMat); | |
| 27 | - void download(GpuMat* reservedMat, Mat& dstMat); | |
| 28 | - void release(GpuMat* mat); | |
| 29 | - | |
| 30 | - ~GpuMatManager(); | |
| 31 | - }; | |
| 32 | -}} |
openbr/plugins/cuda/MatManager.cu
0 โ 100644
| 1 | +#include <pthread.h> | |
| 2 | +#include <semaphore.h> | |
| 3 | + | |
| 4 | +#include <opencv2/opencv.hpp> | |
| 5 | + | |
| 6 | +#include "MatManager.hpp" | |
| 7 | + | |
| 8 | +using namespace cv; | |
| 9 | +using namespace cv::gpu; | |
| 10 | + | |
| 11 | +namespace br { namespace cuda { | |
| 12 | + MatManager::MatManager(int num) { | |
| 13 | + _numMats = num; | |
| 14 | + | |
| 15 | + // initialize the an array of Mats | |
| 16 | + _mats = (uint8_t**)malloc(num * sizeof(uint8_t*)); | |
| 17 | + _matTaken = (bool*)malloc(num * sizeof(bool)); | |
| 18 | + _matsDimension = (int*)malloc(num * sizeof(int)); | |
| 19 | + | |
| 20 | + for (int i=0; i < num; i++) { | |
| 21 | + cudaMalloc(&_mats[i], 1 * sizeof(uint8_t)); | |
| 22 | + | |
| 23 | + // initialize matTaken | |
| 24 | + _matTaken[i] = false; | |
| 25 | + | |
| 26 | + // initialize all mat dimensions to be 1 | |
| 27 | + _matsDimension[i] = 1; | |
| 28 | + } | |
| 29 | + | |
| 30 | + // initialize the locks | |
| 31 | + _matTakenLock = new pthread_mutex_t; | |
| 32 | + pthread_mutex_init(_matTakenLock, NULL); | |
| 33 | + _matsDimensionLock = new pthread_mutex_t; | |
| 34 | + pthread_mutex_init(_matsDimensionLock, NULL); | |
| 35 | + | |
| 36 | + // initialize the semaphore | |
| 37 | + _matSemaphore = new sem_t; | |
| 38 | + sem_init(_matSemaphore, 0, _numMats); | |
| 39 | + } | |
| 40 | + | |
| 41 | + MatManager::matindex MatManager::reserve(Mat &mat) { | |
| 42 | + int reservedMatIndex = 0; | |
| 43 | + | |
| 44 | + sem_wait(_matSemaphore); | |
| 45 | + pthread_mutex_lock(_matTakenLock); | |
| 46 | + int i; | |
| 47 | + for (i=0; i < _numMats; i++) { | |
| 48 | + if ( !_matTaken[i] ) { | |
| 49 | + _matTaken[i] = true; | |
| 50 | + reservedMatIndex = i; | |
| 51 | + break; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + if (i == _numMats) { | |
| 55 | + std::cout << "Cannot reserve a mat. Not enough GpuMat resourses\n" << std::endl << std::flush; | |
| 56 | + } | |
| 57 | + | |
| 58 | + pthread_mutex_unlock(_matTakenLock); | |
| 59 | + | |
| 60 | + // reallocate if size does not match | |
| 61 | + pthread_mutex_lock(_matsDimensionLock); | |
| 62 | + if (_matsDimension[reservedMatIndex] != mat.rows * mat.cols) { | |
| 63 | + cudaFree(_mats[reservedMatIndex]); // free the previous memory first | |
| 64 | + cudaMalloc(&_mats[reservedMatIndex], mat.rows * mat.cols * sizeof(uint8_t)); | |
| 65 | + // change the dimension of that matrix | |
| 66 | + _matsDimension[reservedMatIndex] = mat.rows * mat.cols; | |
| 67 | + | |
| 68 | + } | |
| 69 | + pthread_mutex_unlock(_matsDimensionLock); | |
| 70 | + return reservedMatIndex; | |
| 71 | + } | |
| 72 | + | |
| 73 | + void MatManager::upload(MatManager::matindex reservedMatIndex, Mat& mat) { | |
| 74 | + // copy the content of the Mat to GPU | |
| 75 | + uint8_t* reservedMat = _mats[reservedMatIndex]; | |
| 76 | + cudaMemcpy(reservedMat, mat.ptr<uint8_t>(), mat.rows * mat.cols, cudaMemcpyHostToDevice); | |
| 77 | + } | |
| 78 | + | |
| 79 | + void MatManager::download(MatManager::matindex reservedMatIndex, Mat& dstMat) { | |
| 80 | + // copy the mat data back | |
| 81 | + int dimension = dstMat.rows * dstMat.cols; | |
| 82 | + uint8_t* reservedMat = _mats[reservedMatIndex]; | |
| 83 | + cudaMemcpy(dstMat.ptr<uint8_t>(), reservedMat, dimension, cudaMemcpyDeviceToHost); | |
| 84 | + } | |
| 85 | + | |
| 86 | + void MatManager::release(MatManager::matindex reservedMatIndex) { | |
| 87 | + uint8_t* reservedMat = _mats[reservedMatIndex]; | |
| 88 | + pthread_mutex_lock(_matTakenLock); | |
| 89 | + bool foundMatch = false; | |
| 90 | + for (int i=0; i < _numMats; i++) { | |
| 91 | + if (reservedMat == _mats[i]) { | |
| 92 | + _matTaken[i] = false; | |
| 93 | + foundMatch = true; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + pthread_mutex_unlock(_matTakenLock); | |
| 97 | + | |
| 98 | + // return unconditionally if we didn't find a match | |
| 99 | + if (!foundMatch) { | |
| 100 | + std::cout << "Reservedmat is not in the _mats array" << std::endl << std::flush; | |
| 101 | + return; | |
| 102 | + } | |
| 103 | + sem_post(_matSemaphore); | |
| 104 | + } | |
| 105 | + | |
| 106 | + MatManager::~MatManager() { | |
| 107 | + // assume a single thread is destroying the manager | |
| 108 | + // TODO(colin): add the destroy code | |
| 109 | + //std::cout << "Start to destroy.." << std::endl << std::flush; | |
| 110 | + } | |
| 111 | + | |
| 112 | + uint8_t* MatManager::get_mat_pointer_from_index(MatManager::matindex matIndex) { | |
| 113 | + return _mats[matIndex]; | |
| 114 | + } | |
| 115 | + | |
| 116 | +}} | ... | ... |
openbr/plugins/cuda/MatManager.hpp
0 โ 100644
| 1 | +/* | |
| 2 | +NOTES | |
| 3 | +Mat reservations should return a handle instead of a pointer | |
| 4 | +*/ | |
| 5 | + | |
| 6 | +#include <pthread.h> | |
| 7 | +#include <semaphore.h> | |
| 8 | + | |
| 9 | +#include <opencv2/opencv.hpp> | |
| 10 | +#include <opencv2/gpu/gpu.hpp> | |
| 11 | + | |
| 12 | +using namespace cv; | |
| 13 | +using namespace cv::gpu; | |
| 14 | + | |
| 15 | +namespace br { namespace cuda { | |
| 16 | + class MatManager { | |
| 17 | + private: | |
| 18 | + int _numMats; | |
| 19 | + uint8_t** _mats; // holds all the mats | |
| 20 | + bool* _matTaken; // holds whether or not they are taken | |
| 21 | + int* _matsDimension; // holds the dimension of the Mats | |
| 22 | + | |
| 23 | + pthread_mutex_t* _matTakenLock; // lock for matTaken table | |
| 24 | + pthread_mutex_t* _matsDimensionLock; // lock for _matsDimension table and _mats table | |
| 25 | + sem_t* _matSemaphore; | |
| 26 | + | |
| 27 | + public: | |
| 28 | + typedef int matindex; | |
| 29 | + MatManager(int num); | |
| 30 | + | |
| 31 | + int reserve(Mat &mat); | |
| 32 | + void upload(matindex reservedMatIndex, Mat& mat); | |
| 33 | + void download(matindex reservedMatIndex, Mat& dstMat); | |
| 34 | + void release(matindex matIndex); | |
| 35 | + uint8_t* get_mat_pointer_from_index(matindex matIndex); | |
| 36 | + | |
| 37 | + ~MatManager(); | |
| 38 | + }; | |
| 39 | +}} | ... | ... |
openbr/plugins/cuda/cudalbp.cpp
| ... | ... | @@ -33,7 +33,7 @@ |
| 33 | 33 | #include <openbr/plugins/openbr_internal.h> |
| 34 | 34 | |
| 35 | 35 | #include "cudalbp.hpp" |
| 36 | -#include "GpuMatManager.hpp" | |
| 36 | +#include "MatManager.hpp" | |
| 37 | 37 | |
| 38 | 38 | using namespace cv; |
| 39 | 39 | |
| ... | ... | @@ -90,7 +90,7 @@ class CUDALBPTransform : public UntrainableTransform |
| 90 | 90 | uchar null; |
| 91 | 91 | |
| 92 | 92 | |
| 93 | - cuda::GpuMatManager* matManager; | |
| 93 | + cuda::MatManager* matManager; | |
| 94 | 94 | |
| 95 | 95 | public: |
| 96 | 96 | /* Returns the number of 0->1 or 1->0 transitions in i */ |
| ... | ... | @@ -143,7 +143,7 @@ class CUDALBPTransform : public UntrainableTransform |
| 143 | 143 | lut[i] = null; // Set to null id |
| 144 | 144 | |
| 145 | 145 | // init the mat manager for managing 10 mats |
| 146 | - matManager = new cuda::GpuMatManager(10); | |
| 146 | + matManager = new cuda::MatManager(10); | |
| 147 | 147 | |
| 148 | 148 | // copy lut over to the GPU |
| 149 | 149 | br::cuda::cudalbp_init_wrapper(lut, &lutGpuPtr); |
| ... | ... | @@ -154,17 +154,17 @@ class CUDALBPTransform : public UntrainableTransform |
| 154 | 154 | void project(const Template &src, Template &dst) const |
| 155 | 155 | { |
| 156 | 156 | Mat& m = (Mat&)src.m(); |
| 157 | - | |
| 158 | - GpuMat* a; | |
| 159 | - GpuMat* b; | |
| 160 | - a = matManager->reserve(); | |
| 157 | + cuda::MatManager::matindex a; | |
| 158 | + cuda::MatManager::matindex b; | |
| 159 | + a = matManager->reserve(m); | |
| 161 | 160 | matManager->upload(a, m); |
| 162 | 161 | |
| 163 | 162 | // reserve the second mat and check the dimensiosn |
| 164 | - b = matManager->reserve(); | |
| 165 | - matManager->matchDimensions(b, a); | |
| 166 | - | |
| 167 | - br::cuda::cudalbp_wrapper(*a, *b, lutGpuPtr); | |
| 163 | + b = matManager->reserve(m); | |
| 164 | + | |
| 165 | + uint8_t* srcMatPtr = matManager->get_mat_pointer_from_index(a); | |
| 166 | + uint8_t* dstMatPtr = matManager->get_mat_pointer_from_index(b); | |
| 167 | + br::cuda::cudalbp_wrapper(srcMatPtr, dstMatPtr, lutGpuPtr, m.cols, m.rows, m.step1()); | |
| 168 | 168 | |
| 169 | 169 | matManager->download(b, dst); |
| 170 | 170 | ... | ... |
openbr/plugins/cuda/cudalbp.cu
| ... | ... | @@ -36,15 +36,8 @@ namespace br { namespace cuda { |
| 36 | 36 | dstRowPtr[colInd] = val; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - void cudalbp_wrapper(GpuMat& src, GpuMat& dst, uint8_t* lut) | |
| 39 | + void cudalbp_wrapper(uint8_t* srcPtr, uint8_t* dstPtr, uint8_t* lut, int imageWidth, int imageHeight, size_t step) | |
| 40 | 40 | { |
| 41 | - // convert the GpuMats to pointers | |
| 42 | - uint8_t* srcPtr = (uint8_t*)src.data; | |
| 43 | - uint8_t* dstPtr = (uint8_t*)dst.data; | |
| 44 | - | |
| 45 | - int imageWidth = src.cols; | |
| 46 | - int imageHeight = src.rows; | |
| 47 | - | |
| 48 | 41 | // make 8 * 8 = 64 square block |
| 49 | 42 | dim3 threadsPerBlock(8, 8); |
| 50 | 43 | |
| ... | ... | @@ -55,7 +48,7 @@ namespace br { namespace cuda { |
| 55 | 48 | //printf("Dst Image Dimesions:\n\trows: %d\tcols: %d\n", dst.rows, dst.cols); |
| 56 | 49 | //printf("Running CUDALBP\nBlock Dimensions:\n\tx: %d\ty: %d\n", numBlocks.x, numBlocks.y); |
| 57 | 50 | |
| 58 | - cudalbp_kernel<<<numBlocks, threadsPerBlock>>>(srcPtr, dstPtr, src.step, dst.step, imageHeight, imageWidth, lut); | |
| 51 | + cudalbp_kernel<<<numBlocks, threadsPerBlock>>>(srcPtr, dstPtr, step, step, imageHeight, imageWidth, lut); | |
| 59 | 52 | } |
| 60 | 53 | |
| 61 | 54 | void cudalbp_init_wrapper(uint8_t* lut, uint8_t** lutGpuPtrPtr) { | ... | ... |
openbr/plugins/cuda/cudalbp.hpp
| ... | ... | @@ -5,5 +5,5 @@ using namespace cv::gpu; |
| 5 | 5 | |
| 6 | 6 | namespace br { namespace cuda { |
| 7 | 7 | void cudalbp_init_wrapper(uint8_t* lut, uint8_t** lutGpuPtrPtr); |
| 8 | - void cudalbp_wrapper(GpuMat& src, GpuMat& dst, uint8_t* lut); | |
| 8 | + void cudalbp_wrapper(uint8_t* src, uint8_t* dst, uint8_t* lut, int imageWidth, int imageHeight, size_t step); | |
| 9 | 9 | }} | ... | ... |