Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
3afc37ce
Commit
3afc37ce
authored
Jan 27, 2011
by
Alexey Spizhevoy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added HAVE_CUDA, HAVE_TBB handling into multi_gpu sample
parent
65b9f3bc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
14 deletions
+82
-14
multi_gpu.cpp
samples/gpu/multi_gpu.cpp
+82
-14
No files found.
samples/gpu/multi_gpu.cpp
View file @
3afc37ce
...
...
@@ -3,32 +3,100 @@
#include <iostream>
#include <cvconfig.h>
#include "opencv2/core/core.hpp"
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/highgui/highgui.hpp>
#ifdef HAVE_CUDA
#include <cuda_runtime.h>
#endif
using
namespace
std
;
using
namespace
cv
;
#if !defined(HAVE_CUDA) || !defined(HAVE_TBB)
int
main
()
{
bool
can_run
=
true
;
#if !defined(HAVE_CUDA)
cout
<<
"CUDA support is required (CMake key 'WITH_CUDA' must be true).
\n
"
;
can_run
=
false
;
#endif
#if !defined(HAVE_TBB)
cout
<<
"TBB support is required (CMake key 'WITH_TBB' must be true).
\n
"
;
can_run
=
false
;
#endif
if
(
!
can_run
)
return
0
;
}
#else
#include <cuda.h>
#include <cuda_runtime.h>
#include "opencv2/core/internal.hpp" // For TBB wrappers
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
gpu
;
void
cuSafeCall
(
int
code
);
struct
Worker
{
void
operator
()(
int
device_id
)
const
;
};
void
destroy
();
// Each GPU is associated with its own context
CUcontext
contexts
[
2
];
// Auxiliary variable, stores previusly used context
CUcontext
prev_context
;
int
main
()
{
if
(
getCudaEnabledDeviceCount
()
<
2
)
{
cout
<<
"Two or more GPUs are required
\n
"
;
return
-
1
;
}
// Save the default context
cuSafeCall
(
cuCtxAttach
(
&
contexts
[
0
],
0
));
cuSafeCall
(
cuCtxDetach
(
contexts
[
0
]));
// Create new context for the second GPU
CUdevice
device
;
cuSafeCall
(
cuDeviceGet
(
&
device
,
1
));
cuSafeCall
(
cuCtxCreate
(
&
contexts
[
1
],
0
,
device
));
// Restore the first GPU context
cuSafeCall
(
cuCtxPopCurrent
(
&
prev_context
));
// Run
int
devices
[]
=
{
0
,
1
};
parallel_do
(
devices
,
devices
+
2
,
Worker
());
// Destroy context of the second GPU
destroy
();
return
0
;
}
\ No newline at end of file
}
void
Worker
::
operator
()(
int
device_id
)
const
{
cout
<<
device_id
<<
endl
;
}
void
cuSafeCall
(
int
code
)
{
if
(
code
!=
CUDA_SUCCESS
)
{
cout
<<
"CUDA driver API error: code "
<<
code
<<
", file "
<<
__FILE__
<<
", line "
<<
__LINE__
<<
endl
;
destroy
();
exit
(
-
1
);
}
}
void
destroy
()
{
cuCtxDestroy
(
contexts
[
1
]);
}
#endif
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment