Commit a2742d604e0ddf394eb03f215f4c0e8b2c878810

Authored by boolli
1 parent 4d228feb

Hide max pointers from user

openbr/plugins/cuda/MatManager.cu
@@ -41,9 +41,9 @@ namespace br { namespace cuda { @@ -41,9 +41,9 @@ namespace br { namespace cuda {
41 sem_init(_matSemaphore, 0, _numMats); 41 sem_init(_matSemaphore, 0, _numMats);
42 } 42 }
43 43
44 - uint8_t* MatManager::reserve(Mat *mat) { 44 + int MatManager::reserve(Mat *mat) {
45 int reservedMatIndex = 0; 45 int reservedMatIndex = 0;
46 - std::cout << "Reserving" << std::endl << std::flush; 46 + //std::cout << "Reserving" << std::endl << std::flush;
47 47
48 sem_wait(_matSemaphore); 48 sem_wait(_matSemaphore);
49 pthread_mutex_lock(_matTakenLock); 49 pthread_mutex_lock(_matTakenLock);
@@ -52,7 +52,7 @@ namespace br { namespace cuda { @@ -52,7 +52,7 @@ namespace br { namespace cuda {
52 if ( !(*_matTaken[i]) ) { 52 if ( !(*_matTaken[i]) ) {
53 *_matTaken[i] = true; 53 *_matTaken[i] = true;
54 reservedMatIndex = i; 54 reservedMatIndex = i;
55 - std::cout << "Taking " << i << std::endl << std::flush; 55 + //std::cout << "Taking " << i << std::endl << std::flush;
56 break; 56 break;
57 } 57 }
58 } 58 }
@@ -70,7 +70,7 @@ namespace br { namespace cuda { @@ -70,7 +70,7 @@ namespace br { namespace cuda {
70 //printSizeChangingMat(reservedMat); 70 //printSizeChangingMat(reservedMat);
71 //reservedMat->release(); 71 //reservedMat->release();
72 //reservedMat->create(mat->size(), mat->type()); 72 //reservedMat->create(mat->size(), mat->type());
73 - std::cout << "Size mismatch" << std::endl << std::flush; 73 + //std::cout << "Size mismatch" << std::endl << std::flush;
74 // re malloc 74 // re malloc
75 cudaFree(_mats[reservedMatIndex]); // free the previous memory first 75 cudaFree(_mats[reservedMatIndex]); // free the previous memory first
76 cudaMalloc(&_mats[reservedMatIndex], mat->rows * mat->cols * sizeof(uint8_t)); 76 cudaMalloc(&_mats[reservedMatIndex], mat->rows * mat->cols * sizeof(uint8_t));
@@ -79,10 +79,10 @@ namespace br { namespace cuda { @@ -79,10 +79,10 @@ namespace br { namespace cuda {
79 79
80 } 80 }
81 pthread_mutex_unlock(_matsDimensionLock); 81 pthread_mutex_unlock(_matsDimensionLock);
82 - return _mats[reservedMatIndex]; 82 + return reservedMatIndex;
83 } 83 }
84 84
85 - void MatManager::upload(uint8_t* reservedMat, Mat& mat) { 85 + void MatManager::upload(int reservedMatIndex, Mat& mat) {
86 // upload the image 86 // upload the image
87 /* 87 /*
88 pthread_mutex_lock(_matsDimensionLock); 88 pthread_mutex_lock(_matsDimensionLock);
@@ -91,10 +91,11 @@ namespace br { namespace cuda { @@ -91,10 +91,11 @@ namespace br { namespace cuda {
91 */ 91 */
92 92
93 // copy the content of the Mat to GPU 93 // copy the content of the Mat to GPU
  94 + uint8_t* reservedMat = _mats[reservedMatIndex];
94 cudaMemcpy(reservedMat, mat.ptr<uint8_t>(), mat.rows * mat.cols, cudaMemcpyHostToDevice); 95 cudaMemcpy(reservedMat, mat.ptr<uint8_t>(), mat.rows * mat.cols, cudaMemcpyHostToDevice);
95 } 96 }
96 97
97 - void MatManager::download(uint8_t* reservedMat, Mat& dstMat) { 98 + void MatManager::download(int reservedMatIndex, Mat& dstMat) {
98 /* 99 /*
99 pthread_mutex_lock(_matsDimensionLock); 100 pthread_mutex_lock(_matsDimensionLock);
100 reservedMat->download(dstMat); 101 reservedMat->download(dstMat);
@@ -103,10 +104,12 @@ namespace br { namespace cuda { @@ -103,10 +104,12 @@ namespace br { namespace cuda {
103 104
104 // copy the mat data back 105 // copy the mat data back
105 int dimension = dstMat.rows * dstMat.cols; 106 int dimension = dstMat.rows * dstMat.cols;
  107 + uint8_t* reservedMat = _mats[reservedMatIndex];
106 cudaMemcpy(dstMat.ptr<uint8_t>(), reservedMat, dimension, cudaMemcpyDeviceToHost); 108 cudaMemcpy(dstMat.ptr<uint8_t>(), reservedMat, dimension, cudaMemcpyDeviceToHost);
107 } 109 }
108 110
109 - void MatManager::release(uint8_t* reservedMat) { 111 + void MatManager::release(int reservedMatIndex) {
  112 + uint8_t* reservedMat = _mats[reservedMatIndex];
110 pthread_mutex_lock(_matTakenLock); 113 pthread_mutex_lock(_matTakenLock);
111 bool foundMatch = false; 114 bool foundMatch = false;
112 for (int i=0; i < _numMats; i++) { 115 for (int i=0; i < _numMats; i++) {
@@ -141,7 +144,7 @@ namespace br { namespace cuda { @@ -141,7 +144,7 @@ namespace br { namespace cuda {
141 MatManager::~MatManager() { 144 MatManager::~MatManager() {
142 // assume a single thread is destroying the manager 145 // assume a single thread is destroying the manager
143 // TODO(colin): add the destroy code 146 // TODO(colin): add the destroy code
144 - std::cout << "Start to destroy.." << std::endl << std::flush; 147 + //std::cout << "Start to destroy.." << std::endl << std::flush;
145 } 148 }
146 149
147 /* 150 /*
@@ -182,5 +185,8 @@ namespace br { namespace cuda { @@ -182,5 +185,8 @@ namespace br { namespace cuda {
182 std::cout << "can't release mat at address: " << gpuMat << std::endl << std::flush; 185 std::cout << "can't release mat at address: " << gpuMat << std::endl << std::flush;
183 } 186 }
184 */ 187 */
  188 + uint8_t* MatManager::get_mat_pointer_from_index(int matIndex) {
  189 + return _mats[matIndex];
  190 + }
185 191
186 }} 192 }}
openbr/plugins/cuda/MatManager.hpp
@@ -27,10 +27,11 @@ namespace br { namespace cuda { @@ -27,10 +27,11 @@ namespace br { namespace cuda {
27 public: 27 public:
28 MatManager(int num); 28 MatManager(int num);
29 29
30 - uint8_t* reserve(Mat *mat);  
31 - void upload(uint8_t* reservedMat, Mat& mat);  
32 - void download(uint8_t* reservedMat, Mat& dstMat);  
33 - void release(uint8_t* mat); 30 + int reserve(Mat *mat);
  31 + void upload(int reservedMatIndex, Mat& mat);
  32 + void download(int reservedMatIndex, Mat& dstMat);
  33 + void release(int matIndex);
  34 + uint8_t* get_mat_pointer_from_index(int matIndex);
34 35
35 ~MatManager(); 36 ~MatManager();
36 //void printMats(); 37 //void printMats();
openbr/plugins/cuda/cudalbp.cpp
@@ -154,8 +154,8 @@ class CUDALBPTransform : public UntrainableTransform @@ -154,8 +154,8 @@ class CUDALBPTransform : public UntrainableTransform
154 void project(const Template &src, Template &dst) const 154 void project(const Template &src, Template &dst) const
155 { 155 {
156 Mat& m = (Mat&)src.m(); 156 Mat& m = (Mat&)src.m();
157 - uint8_t* a;  
158 - uint8_t* b; 157 + int a;
  158 + int b;
159 a = matManager->reserve(&m); 159 a = matManager->reserve(&m);
160 // std::cout << "m: " << m.size() << ", " << m.type() << std::endl << std::flush; 160 // std::cout << "m: " << m.size() << ", " << m.type() << std::endl << std::flush;
161 // std::cout << "a: " << a->size() << ", " << a->type() << std::endl << std::flush; 161 // std::cout << "a: " << a->size() << ", " << a->type() << std::endl << std::flush;
@@ -166,7 +166,9 @@ class CUDALBPTransform : public UntrainableTransform @@ -166,7 +166,9 @@ class CUDALBPTransform : public UntrainableTransform
166 //matManager->matchDimensions(b, a); 166 //matManager->matchDimensions(b, a);
167 167
168 //std::cout << "Coming to here" << std::endl << std::flush; 168 //std::cout << "Coming to here" << std::endl << std::flush;
169 - br::cuda::cudalbp_wrapper(a, b, lutGpuPtr, m.cols, m.rows, m.step1()); 169 + uint8_t* srcMatPtr = matManager->get_mat_pointer_from_index(a);
  170 + uint8_t* dstMatPtr = matManager->get_mat_pointer_from_index(b);
  171 + br::cuda::cudalbp_wrapper(srcMatPtr, dstMatPtr, lutGpuPtr, m.cols, m.rows, m.step1());
170 //std::cout << "Coming out of here" << std::endl << std::flush; 172 //std::cout << "Coming out of here" << std::endl << std::flush;
171 173
172 //std::cout << "Start to download" << std::endl << std::flush; 174 //std::cout << "Start to download" << std::endl << std::flush;
@@ -176,7 +178,7 @@ class CUDALBPTransform : public UntrainableTransform @@ -176,7 +178,7 @@ class CUDALBPTransform : public UntrainableTransform
176 // release both the mats 178 // release both the mats
177 matManager->release(a); 179 matManager->release(a);
178 matManager->release(b); 180 matManager->release(b);
179 - std::cout << "finish release" << std::endl << std::flush; 181 + //std::cout << "finish release" << std::endl << std::flush;
180 } 182 }
181 }; 183 };
182 184