Commit eac782df784f1f10a1e864139f22233247ea36e6

Authored by DepthDeluxe
1 parent 24536713

removed MatManager from master because it isn't ready

openbr/core/cuda/MatManager.cu deleted
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/core/cuda/MatManager.hpp deleted
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   -}}