From 660f603b40cf0b693a97a9da9702cdba9b0e7a92 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 5 May 2020 18:39:34 +0300
Subject: [PATCH] Set cuda device to second device

---
 lib/libstereo/middlebury/main.cpp             | 6 ++++++
 lib/libstereo/src/algorithms/brefcensussgm.cu | 2 +-
 lib/libstereo/src/algorithms/censussgm.cu     | 2 +-
 lib/libstereo/src/algorithms/hcensussgm.cu    | 2 +-
 lib/libstereo/src/algorithms/meancensussgm.cu | 2 +-
 lib/libstereo/src/algorithms/stablesgm.cu     | 2 +-
 lib/libstereo/src/algorithms/tcensussgm.cu    | 2 +-
 7 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/libstereo/middlebury/main.cpp b/lib/libstereo/middlebury/main.cpp
index 9bbf0edd1..75e68a99c 100644
--- a/lib/libstereo/middlebury/main.cpp
+++ b/lib/libstereo/middlebury/main.cpp
@@ -9,6 +9,8 @@
 #include "middlebury.hpp"
 #include "algorithms.hpp"
 
+#include <cuda_runtime.h>
+
 #include "../../components/common/cpp/include/ftl/config.h"
 
 struct Result {
@@ -141,6 +143,10 @@ void main_default(const std::vector<std::string> &paths,
 
 	std::vector<std::pair<MiddleburyData, std::map<std::string, Result>>> results;
 
+	int devices=0;
+	cudaGetDeviceCount(&devices);
+	cudaSetDevice(devices-1);
+
 	for (const auto &dir : paths) {
 		auto input = load_input(dir);
 		std::map<std::string, Result> result;
diff --git a/lib/libstereo/src/algorithms/brefcensussgm.cu b/lib/libstereo/src/algorithms/brefcensussgm.cu
index 141388f3d..eea64c294 100644
--- a/lib/libstereo/src/algorithms/brefcensussgm.cu
+++ b/lib/libstereo/src/algorithms/brefcensussgm.cu
@@ -21,7 +21,7 @@ StereoBRefCensusSgm::StereoBRefCensusSgm() : impl_(nullptr) {
 
 void StereoBRefCensusSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
diff --git a/lib/libstereo/src/algorithms/censussgm.cu b/lib/libstereo/src/algorithms/censussgm.cu
index fb229bf66..3c15e5275 100644
--- a/lib/libstereo/src/algorithms/censussgm.cu
+++ b/lib/libstereo/src/algorithms/censussgm.cu
@@ -16,7 +16,7 @@ StereoCensusSgm::StereoCensusSgm() : impl_(nullptr) {
 
 void StereoCensusSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
diff --git a/lib/libstereo/src/algorithms/hcensussgm.cu b/lib/libstereo/src/algorithms/hcensussgm.cu
index 420f22f53..2a7c7586c 100644
--- a/lib/libstereo/src/algorithms/hcensussgm.cu
+++ b/lib/libstereo/src/algorithms/hcensussgm.cu
@@ -70,7 +70,7 @@ StereoHierCensusSgm::StereoHierCensusSgm() : impl_(nullptr) {
 
 void StereoHierCensusSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
diff --git a/lib/libstereo/src/algorithms/meancensussgm.cu b/lib/libstereo/src/algorithms/meancensussgm.cu
index e0c1d49d7..7058ba79f 100644
--- a/lib/libstereo/src/algorithms/meancensussgm.cu
+++ b/lib/libstereo/src/algorithms/meancensussgm.cu
@@ -20,7 +20,7 @@ StereoMeanCensusSgm::StereoMeanCensusSgm() : impl_(nullptr) {
 
 void StereoMeanCensusSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
diff --git a/lib/libstereo/src/algorithms/stablesgm.cu b/lib/libstereo/src/algorithms/stablesgm.cu
index 3aeb33a52..efaf20a28 100644
--- a/lib/libstereo/src/algorithms/stablesgm.cu
+++ b/lib/libstereo/src/algorithms/stablesgm.cu
@@ -16,7 +16,7 @@ StereoStableSgm::StereoStableSgm() : impl_(nullptr) {
 
 void StereoStableSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
diff --git a/lib/libstereo/src/algorithms/tcensussgm.cu b/lib/libstereo/src/algorithms/tcensussgm.cu
index 131405107..6a449937a 100644
--- a/lib/libstereo/src/algorithms/tcensussgm.cu
+++ b/lib/libstereo/src/algorithms/tcensussgm.cu
@@ -16,7 +16,7 @@ StereoTCensusSgm::StereoTCensusSgm() : impl_(nullptr) {
 
 void StereoTCensusSgm::compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity) {
 
-	cudaSetDevice(0);
+	//cudaSetDevice(0);
 
 	if (l.rows() != impl_->cost.height() || r.cols() != impl_->cost.width()) {
 		delete impl_; impl_ = nullptr;
-- 
GitLab