\r
#include <cuda.h>\r
#include <cuda_runtime.h>\r
-#include <GL/gl.h>\r
-#include <cudaGL.h>\r
#include "opencv2/core/internal.hpp" // For TBB wrappers\r
-#include "tbb/tbb.h"\r
-#include "tbb/mutex.h"\r
\r
using namespace std;\r
using namespace cv;\r
}\r
\r
// Each GPU is associated with its own context\r
-CUcontext contexts[/*2*/1];\r
+CUcontext contexts[2];\r
\r
-void inline contextOn(int id) \r
+void inline contextOn(int id)\r
{\r
safeCall(cuCtxPushCurrent(contexts[id]));\r
}\r
\r
-void inline contextOff() \r
+void inline contextOff()\r
{\r
CUcontext prev_context;\r
safeCall(cuCtxPopCurrent(&prev_context));\r
// CPU result\r
Mat result;\r
\r
-int some[2];\r
-\r
-tbb::mutex mutex;\r
-\r
int main(int argc, char** argv)\r
{\r
if (argc < 3)\r
}\r
\r
int num_devices = getCudaEnabledDeviceCount();\r
-// if (num_devices < 2)\r
-// {\r
-// std::cout << "Two or more GPUs are required\n";\r
-// return -1;\r
-// }\r
+ if (num_devices < 2)\r
+ {\r
+ std::cout << "Two or more GPUs are required\n";\r
+ return -1;\r
+ }\r
\r
for (int i = 0; i < num_devices; ++i)\r
{\r
// Create context for GPU #0\r
CUdevice device;\r
safeCall(cuDeviceGet(&device, 0));\r
- safeCall(cuGLCtxCreate(&contexts[0], 0, device));\r
- //safeCall(cuCtxCreate(&contexts[0], 0, device));\r
+ safeCall(cuCtxCreate(&contexts[0], 0, device));\r
contextOff();\r
\r
-// // Create context for GPU #1\r
-// safeCall(cuDeviceGet(&device, 0));\r
-// safeCall(cuCtxCreate(&contexts[1], 0, device));\r
-// contextOff();\r
+ // Create context for GPU #1\r
+ safeCall(cuDeviceGet(&device, 1));\r
+ safeCall(cuCtxCreate(&contexts[1], 0, device));\r
+ contextOff();\r
\r
// Split source images for processing on GPU #0\r
contextOn(0);\r
contextOff();\r
\r
// Split source images for processing on the GPU #1\r
- contextOn(0);\r
+ contextOn(1);\r
d_left[1].upload(left.rowRange(left.rows / 2, left.rows));\r
d_right[1].upload(right.rowRange(right.rows / 2, right.rows));\r
bm[1] = new StereoBM_GPU();\r
contextOff();\r
\r
- some[0] = some[1] = 0;\r
// Execute calculation in two threads using two GPUs\r
- vector<int> devices;\r
- for (int i = 0; i < 4; ++i)\r
- devices.push_back(rand()%2);\r
- tbb::parallel_do(&devices[0], &devices[devices.size() - 1], Worker());\r
-\r
- cout << some[0] << " " << some[1] << endl;\r
+ int devices[] = {0, 1};\r
+ parallel_do(devices, devices + 2, Worker());\r
\r
// Release the first GPU resources\r
contextOn(0);\r
contextOff();\r
\r
// Release the second GPU resources\r
- contextOn(0);\r
+ contextOn(1);\r
imshow("GPU #1 result", Mat(d_result[1]));\r
d_left[1].release();\r
d_right[1].release();\r
\r
void Worker::operator()(int device_id) const\r
{\r
- mutex.lock();\r
-\r
- contextOn(0);\r
+ contextOn(device_id);\r
\r
bm[device_id]->operator()(d_left[device_id], d_right[device_id],\r
d_result[device_id]);\r
<< "): finished\n";\r
\r
contextOff();\r
-\r
- mutex.unlock();\r
}\r
\r
\r
void destroyContexts()\r
{\r
safeCall(cuCtxDestroy(contexts[0]));\r
- //safeCall(cuCtxDestroy(contexts[1]));\r
+ safeCall(cuCtxDestroy(contexts[1]));\r
}\r
\r
#endif\r
-\r