Commit efe0b774 authored by Alexey Spizhevoy's avatar Alexey Spizhevoy

reverted wrong commit for one of gpu samples

parent ace94d2e
...@@ -32,11 +32,7 @@ int main() ...@@ -32,11 +32,7 @@ int main()
#include <cuda.h> #include <cuda.h>
#include <cuda_runtime.h> #include <cuda_runtime.h>
#include <GL/gl.h>
#include <cudaGL.h>
#include "opencv2/core/internal.hpp" // For TBB wrappers #include "opencv2/core/internal.hpp" // For TBB wrappers
#include "tbb/tbb.h"
#include "tbb/mutex.h"
using namespace std; using namespace std;
using namespace cv; using namespace cv;
...@@ -58,14 +54,14 @@ inline void safeCall_(int code, const char* expr, const char* file, int line) ...@@ -58,14 +54,14 @@ inline void safeCall_(int code, const char* expr, const char* file, int line)
} }
// Each GPU is associated with its own context // Each GPU is associated with its own context
CUcontext contexts[/*2*/1]; CUcontext contexts[2];
void inline contextOn(int id) void inline contextOn(int id)
{ {
safeCall(cuCtxPushCurrent(contexts[id])); safeCall(cuCtxPushCurrent(contexts[id]));
} }
void inline contextOff() void inline contextOff()
{ {
CUcontext prev_context; CUcontext prev_context;
safeCall(cuCtxPopCurrent(&prev_context)); safeCall(cuCtxPopCurrent(&prev_context));
...@@ -80,10 +76,6 @@ GpuMat d_result[2]; ...@@ -80,10 +76,6 @@ GpuMat d_result[2];
// CPU result // CPU result
Mat result; Mat result;
int some[2];
tbb::mutex mutex;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc < 3) if (argc < 3)
...@@ -93,11 +85,11 @@ int main(int argc, char** argv) ...@@ -93,11 +85,11 @@ int main(int argc, char** argv)
} }
int num_devices = getCudaEnabledDeviceCount(); int num_devices = getCudaEnabledDeviceCount();
// if (num_devices < 2) if (num_devices < 2)
// { {
// std::cout << "Two or more GPUs are required\n"; std::cout << "Two or more GPUs are required\n";
// return -1; return -1;
// } }
for (int i = 0; i < num_devices; ++i) for (int i = 0; i < num_devices; ++i)
{ {
...@@ -131,14 +123,13 @@ int main(int argc, char** argv) ...@@ -131,14 +123,13 @@ int main(int argc, char** argv)
// Create context for GPU #0 // Create context for GPU #0
CUdevice device; CUdevice device;
safeCall(cuDeviceGet(&device, 0)); safeCall(cuDeviceGet(&device, 0));
safeCall(cuGLCtxCreate(&contexts[0], 0, device)); safeCall(cuCtxCreate(&contexts[0], 0, device));
//safeCall(cuCtxCreate(&contexts[0], 0, device));
contextOff(); contextOff();
// // Create context for GPU #1 // Create context for GPU #1
// safeCall(cuDeviceGet(&device, 0)); safeCall(cuDeviceGet(&device, 1));
// safeCall(cuCtxCreate(&contexts[1], 0, device)); safeCall(cuCtxCreate(&contexts[1], 0, device));
// contextOff(); contextOff();
// Split source images for processing on GPU #0 // Split source images for processing on GPU #0
contextOn(0); contextOn(0);
...@@ -148,20 +139,15 @@ int main(int argc, char** argv) ...@@ -148,20 +139,15 @@ int main(int argc, char** argv)
contextOff(); contextOff();
// Split source images for processing on the GPU #1 // Split source images for processing on the GPU #1
contextOn(0); contextOn(1);
d_left[1].upload(left.rowRange(left.rows / 2, left.rows)); d_left[1].upload(left.rowRange(left.rows / 2, left.rows));
d_right[1].upload(right.rowRange(right.rows / 2, right.rows)); d_right[1].upload(right.rowRange(right.rows / 2, right.rows));
bm[1] = new StereoBM_GPU(); bm[1] = new StereoBM_GPU();
contextOff(); contextOff();
some[0] = some[1] = 0;
// Execute calculation in two threads using two GPUs // Execute calculation in two threads using two GPUs
vector<int> devices; int devices[] = {0, 1};
for (int i = 0; i < 4; ++i) parallel_do(devices, devices + 2, Worker());
devices.push_back(rand()%2);
tbb::parallel_do(&devices[0], &devices[devices.size() - 1], Worker());
cout << some[0] << " " << some[1] << endl;
// Release the first GPU resources // Release the first GPU resources
contextOn(0); contextOn(0);
...@@ -173,7 +159,7 @@ int main(int argc, char** argv) ...@@ -173,7 +159,7 @@ int main(int argc, char** argv)
contextOff(); contextOff();
// Release the second GPU resources // Release the second GPU resources
contextOn(0); contextOn(1);
imshow("GPU #1 result", Mat(d_result[1])); imshow("GPU #1 result", Mat(d_result[1]));
d_left[1].release(); d_left[1].release();
d_right[1].release(); d_right[1].release();
...@@ -189,9 +175,7 @@ int main(int argc, char** argv) ...@@ -189,9 +175,7 @@ int main(int argc, char** argv)
void Worker::operator()(int device_id) const void Worker::operator()(int device_id) const
{ {
mutex.lock(); contextOn(device_id);
contextOn(0);
bm[device_id]->operator()(d_left[device_id], d_right[device_id], bm[device_id]->operator()(d_left[device_id], d_right[device_id],
d_result[device_id]); d_result[device_id]);
...@@ -200,16 +184,13 @@ void Worker::operator()(int device_id) const ...@@ -200,16 +184,13 @@ void Worker::operator()(int device_id) const
<< "): finished\n"; << "): finished\n";
contextOff(); contextOff();
mutex.unlock();
} }
void destroyContexts() void destroyContexts()
{ {
safeCall(cuCtxDestroy(contexts[0])); safeCall(cuCtxDestroy(contexts[0]));
//safeCall(cuCtxDestroy(contexts[1])); safeCall(cuCtxDestroy(contexts[1]));
} }
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment