add guards to drop GPU code in CPU-only mode
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Mon, 30 Jun 2014 04:11:44 +0000 (21:11 -0700)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 17 Jul 2014 09:57:09 +0000 (11:57 +0200)
Makefile
include/caffe/test/test_caffe_main.hpp
src/caffe/net.cpp
src/caffe/test/test_math_functions.cpp
src/caffe/test/test_random_number_generator.cpp

index ef3aa4b..69f2c30 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -208,7 +208,7 @@ ifeq ($(CPU_ONLY), 1)
        TEST_OBJS := $(TEST_CXX_OBJS)
        TEST_BINS := $(TEST_CXX_BINS)
        ALL_WARNS := $(ALL_CXX_WARNS)
-       TEST_FILTER := --gtest_filter="-*GPU*:*/2.*:*/3.*"
+       TEST_FILTER := --gtest_filter="-*GPU*"
        COMMON_FLAGS += -DCPU_ONLY
 endif
 
index e1a7645..3541c0b 100644 (file)
@@ -44,6 +44,12 @@ struct DoubleCPU {
   static const Caffe::Brew device = Caffe::CPU;
 };
 
+#ifdef CPU_ONLY
+
+typedef ::testing::Types<FloatCPU, DoubleCPU> TestDtypesAndDevices;
+
+#else
+
 struct FloatGPU {
   typedef float Dtype;
   static const Caffe::Brew device = Caffe::GPU;
@@ -57,6 +63,8 @@ struct DoubleGPU {
 typedef ::testing::Types<FloatCPU, DoubleCPU, FloatGPU, DoubleGPU>
     TestDtypesAndDevices;
 
+#endif
+
 }  // namespace caffe
 
 #endif  // CAFFE_TEST_TEST_CAFFE_MAIN_HPP_
index edea726..aba4cc2 100644 (file)
@@ -498,11 +498,13 @@ void Net<Dtype>::Update() {
       owner_diff = params_[param_owners_[i]]->mutable_cpu_diff();
       caffe_add(count, this_diff, owner_diff, owner_diff);
       break;
+#ifndef CPU_ONLY
     case Caffe::GPU:
       this_diff = params_[i]->gpu_diff();
       owner_diff = params_[param_owners_[i]]->mutable_gpu_diff();
       caffe_gpu_add(count, this_diff, owner_diff, owner_diff);
       break;
+#endif
     default:
       LOG(FATAL) << "Unknown caffe mode: " << Caffe::mode();
     }
index 2a70c9c..502b77e 100644 (file)
@@ -80,18 +80,6 @@ TYPED_TEST(MathFunctionsTest, TestHammingDistanceCPU) {
             caffe_cpu_hamming_distance<TypeParam>(n, x, y));
 }
 
-// TODO: Fix caffe_gpu_hamming_distance and re-enable this test.
-TYPED_TEST(MathFunctionsTest, DISABLED_TestHammingDistanceGPU) {
-  int n = this->blob_bottom_->count();
-  const TypeParam* x = this->blob_bottom_->cpu_data();
-  const TypeParam* y = this->blob_top_->cpu_data();
-  int reference_distance = this->ReferenceHammingDistance(n, x, y);
-  x = this->blob_bottom_->gpu_data();
-  y = this->blob_top_->gpu_data();
-  int computed_distance = caffe_gpu_hamming_distance<TypeParam>(n, x, y);
-  EXPECT_EQ(reference_distance, computed_distance);
-}
-
 TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
   int n = this->blob_bottom_->count();
   const TypeParam* x = this->blob_bottom_->cpu_data();
@@ -103,91 +91,116 @@ TYPED_TEST(MathFunctionsTest, TestAsumCPU) {
   EXPECT_LT((cpu_asum - std_asum) / std_asum, 1e-2);
 }
 
-TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
+TYPED_TEST(MathFunctionsTest, TestSignCPU) {
   int n = this->blob_bottom_->count();
   const TypeParam* x = this->blob_bottom_->cpu_data();
-  TypeParam std_asum = 0;
+  caffe_cpu_sign<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
+  const TypeParam* signs = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    std_asum += std::fabs(x[i]);
+    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
   }
-  TypeParam gpu_asum;
-  caffe_gpu_asum<TypeParam>(n, this->blob_bottom_->gpu_data(), &gpu_asum);
-  EXPECT_LT((gpu_asum - std_asum) / std_asum, 1e-2);
 }
 
-TYPED_TEST(MathFunctionsTest, TestSignCPU) {
+TYPED_TEST(MathFunctionsTest, TestSgnbitCPU) {
   int n = this->blob_bottom_->count();
   const TypeParam* x = this->blob_bottom_->cpu_data();
-  caffe_cpu_sign<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
-  const TypeParam* signs = this->blob_bottom_->cpu_diff();
+  caffe_cpu_sgnbit<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
+  const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestSignGPU) {
+TYPED_TEST(MathFunctionsTest, TestFabsCPU) {
   int n = this->blob_bottom_->count();
-  caffe_gpu_sign<TypeParam>(n, this->blob_bottom_->gpu_data(),
-                            this->blob_bottom_->mutable_gpu_diff());
-  const TypeParam* signs = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
+  caffe_cpu_fabs<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
+  const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
+    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestSgnbitCPU) {
+TYPED_TEST(MathFunctionsTest, TestScaleCPU) {
   int n = this->blob_bottom_->count();
+  TypeParam alpha = this->blob_bottom_->cpu_diff()[caffe_rng_rand() %
+                                                   this->blob_bottom_->count()];
+  caffe_cpu_scale<TypeParam>(n, alpha, this->blob_bottom_->cpu_data(),
+                             this->blob_bottom_->mutable_cpu_diff());
+  const TypeParam* scaled = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
-  caffe_cpu_sgnbit<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
-  const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+    EXPECT_EQ(scaled[i], x[i] * alpha);
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestSgnbitGPU) {
+TYPED_TEST(MathFunctionsTest, TestCopyCPU) {
+  const int n = this->blob_bottom_->count();
+  const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
+  TypeParam* top_data = this->blob_top_->mutable_cpu_data();
+  Caffe::set_mode(Caffe::CPU);
+  caffe_copy(n, bottom_data, top_data);
+  for (int i = 0; i < n; ++i) {
+    EXPECT_EQ(bottom_data[i], top_data[i]);
+  }
+}
+
+#ifndef CPU_ONLY
+
+// TODO: Fix caffe_gpu_hamming_distance and re-enable this test.
+TYPED_TEST(MathFunctionsTest, DISABLED_TestHammingDistanceGPU) {
+  int n = this->blob_bottom_->count();
+  const TypeParam* x = this->blob_bottom_->cpu_data();
+  const TypeParam* y = this->blob_top_->cpu_data();
+  int reference_distance = this->ReferenceHammingDistance(n, x, y);
+  x = this->blob_bottom_->gpu_data();
+  y = this->blob_top_->gpu_data();
+  int computed_distance = caffe_gpu_hamming_distance<TypeParam>(n, x, y);
+  EXPECT_EQ(reference_distance, computed_distance);
+}
+
+TYPED_TEST(MathFunctionsTest, TestAsumGPU) {
   int n = this->blob_bottom_->count();
-  caffe_gpu_sgnbit<TypeParam>(n, this->blob_bottom_->gpu_data(),
-                            this->blob_bottom_->mutable_gpu_diff());
-  const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
+  TypeParam std_asum = 0;
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
+    std_asum += std::fabs(x[i]);
   }
+  TypeParam gpu_asum;
+  caffe_gpu_asum<TypeParam>(n, this->blob_bottom_->gpu_data(), &gpu_asum);
+  EXPECT_LT((gpu_asum - std_asum) / std_asum, 1e-2);
 }
 
-TYPED_TEST(MathFunctionsTest, TestFabsCPU) {
+TYPED_TEST(MathFunctionsTest, TestSignGPU) {
   int n = this->blob_bottom_->count();
+  caffe_gpu_sign<TypeParam>(n, this->blob_bottom_->gpu_data(),
+                            this->blob_bottom_->mutable_gpu_diff());
+  const TypeParam* signs = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
-  caffe_cpu_fabs<TypeParam>(n, x, this->blob_bottom_->mutable_cpu_diff());
-  const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+    EXPECT_EQ(signs[i], x[i] > 0 ? 1 : (x[i] < 0 ? -1 : 0));
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestFabsGPU) {
+TYPED_TEST(MathFunctionsTest, TestSgnbitGPU) {
   int n = this->blob_bottom_->count();
-  caffe_gpu_fabs<TypeParam>(n, this->blob_bottom_->gpu_data(),
+  caffe_gpu_sgnbit<TypeParam>(n, this->blob_bottom_->gpu_data(),
                             this->blob_bottom_->mutable_gpu_diff());
-  const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
+  const TypeParam* signbits = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
+    EXPECT_EQ(signbits[i], x[i] < 0 ? 1 : 0);
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestScaleCPU) {
+TYPED_TEST(MathFunctionsTest, TestFabsGPU) {
   int n = this->blob_bottom_->count();
-  TypeParam alpha = this->blob_bottom_->cpu_diff()[caffe_rng_rand() %
-                                                   this->blob_bottom_->count()];
-  caffe_cpu_scale<TypeParam>(n, alpha, this->blob_bottom_->cpu_data(),
-                             this->blob_bottom_->mutable_cpu_diff());
-  const TypeParam* scaled = this->blob_bottom_->cpu_diff();
+  caffe_gpu_fabs<TypeParam>(n, this->blob_bottom_->gpu_data(),
+                            this->blob_bottom_->mutable_gpu_diff());
+  const TypeParam* abs_val = this->blob_bottom_->cpu_diff();
   const TypeParam* x = this->blob_bottom_->cpu_data();
   for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(scaled[i], x[i] * alpha);
+    EXPECT_EQ(abs_val[i], x[i] > 0 ? x[i] : -x[i]);
   }
 }
 
@@ -204,17 +217,6 @@ TYPED_TEST(MathFunctionsTest, TestScaleGPU) {
   }
 }
 
-TYPED_TEST(MathFunctionsTest, TestCopyCPU) {
-  const int n = this->blob_bottom_->count();
-  const TypeParam* bottom_data = this->blob_bottom_->cpu_data();
-  TypeParam* top_data = this->blob_top_->mutable_cpu_data();
-  Caffe::set_mode(Caffe::CPU);
-  caffe_copy(n, bottom_data, top_data);
-  for (int i = 0; i < n; ++i) {
-    EXPECT_EQ(bottom_data[i], top_data[i]);
-  }
-}
-
 TYPED_TEST(MathFunctionsTest, TestCopyGPU) {
   const int n = this->blob_bottom_->count();
   const TypeParam* bottom_data = this->blob_bottom_->gpu_data();
@@ -228,4 +230,7 @@ TYPED_TEST(MathFunctionsTest, TestCopyGPU) {
   }
 }
 
+#endif
+
+
 }  // namespace caffe
index 3ab4680..3cd77da 100644 (file)
@@ -65,11 +65,6 @@ class RandomNumberGeneratorTest : public ::testing::Test {
     caffe_rng_gaussian(sample_size_, mu, sigma, rng_data);
   }
 
-  void RngGaussianFillGPU(const Dtype mu, const Dtype sigma, void* gpu_data) {
-    Dtype* rng_data = static_cast<Dtype*>(gpu_data);
-    caffe_gpu_rng_gaussian(sample_size_, mu, sigma, rng_data);
-  }
-
   void RngGaussianChecks(const Dtype mu, const Dtype sigma,
                          const void* cpu_data, const Dtype sparse_p = 0) {
     const Dtype* rng_data = static_cast<const Dtype*>(cpu_data);
@@ -114,19 +109,6 @@ class RandomNumberGeneratorTest : public ::testing::Test {
     caffe_rng_uniform(sample_size_, lower, upper, rng_data);
   }
 
-  void RngUniformFillGPU(const Dtype lower, const Dtype upper, void* gpu_data) {
-    CHECK_GE(upper, lower);
-    Dtype* rng_data = static_cast<Dtype*>(gpu_data);
-    caffe_gpu_rng_uniform(sample_size_, lower, upper, rng_data);
-  }
-
-  // Fills with uniform integers in [0, UINT_MAX] using 2 argument form of
-  // caffe_gpu_rng_uniform.
-  void RngUniformIntFillGPU(void* gpu_data) {
-    unsigned int* rng_data = static_cast<unsigned int*>(gpu_data);
-    caffe_gpu_rng_uniform(sample_size_, rng_data);
-  }
-
   void RngUniformChecks(const Dtype lower, const Dtype upper,
                         const void* cpu_data, const Dtype sparse_p = 0) {
     const Dtype* rng_data = static_cast<const Dtype*>(cpu_data);
@@ -188,6 +170,28 @@ class RandomNumberGeneratorTest : public ::testing::Test {
     EXPECT_NEAR(sample_mean, true_mean, bound);
   }
 
+#ifndef CPU_ONLY
+
+  void RngGaussianFillGPU(const Dtype mu, const Dtype sigma, void* gpu_data) {
+    Dtype* rng_data = static_cast<Dtype*>(gpu_data);
+    caffe_gpu_rng_gaussian(sample_size_, mu, sigma, rng_data);
+  }
+
+  void RngUniformFillGPU(const Dtype lower, const Dtype upper, void* gpu_data) {
+    CHECK_GE(upper, lower);
+    Dtype* rng_data = static_cast<Dtype*>(gpu_data);
+    caffe_gpu_rng_uniform(sample_size_, lower, upper, rng_data);
+  }
+
+  // Fills with uniform integers in [0, UINT_MAX] using 2 argument form of
+  // caffe_gpu_rng_uniform.
+  void RngUniformIntFillGPU(void* gpu_data) {
+    unsigned int* rng_data = static_cast<unsigned int*>(gpu_data);
+    caffe_gpu_rng_uniform(sample_size_, rng_data);
+  }
+
+#endif
+
   int num_above_mean;
   int num_below_mean;
 
@@ -393,6 +397,7 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngBernoulliTimesBernoulli) {
   EXPECT_NEAR(true_mean, sample_p, bound);
 }
 
+#ifndef CPU_ONLY
 
 TYPED_TEST(RandomNumberGeneratorTest, TestRngGaussianGPU) {
   const TypeParam mu = 0;
@@ -512,5 +517,6 @@ TYPED_TEST(RandomNumberGeneratorTest, TestRngUniformTimesUniformGPU) {
   this->RngUniformChecks(lower_prod, upper_prod, uniform_data_1);
 }
 
+#endif
 
 }  // namespace caffe