You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2018/05/09 04:17:37 UTC

[incubator-mxnet] branch master updated: Fix mxnet::Context hash for 32 bit architectures, where size_t is 32 bit. (#10746)

This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ae01e4  Fix mxnet::Context hash for 32 bit architectures, where size_t is 32 bit. (#10746)
3ae01e4 is described below

commit 3ae01e466e3363e50d39a4a9d8cd5777c0399958
Author: Pedro Larroy <92...@users.noreply.github.com>
AuthorDate: Wed May 9 06:17:30 2018 +0200

    Fix mxnet::Context hash for 32 bit architectures, where size_t is 32 bit. (#10746)
    
    Improve output of checkForWorkingCuda which was outputting just a number in stdout
---
 include/mxnet/base.h   |  5 ++++-
 tests/cpp/misc/base.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/cpp/test_main.cc | 18 ++++++++++--------
 3 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/include/mxnet/base.h b/include/mxnet/base.h
index 38fd7ed..7cabfe5 100644
--- a/include/mxnet/base.h
+++ b/include/mxnet/base.h
@@ -365,7 +365,10 @@ constexpr size_t kMKLDNNAlign = 64;
 namespace std {
 template<> struct hash<mxnet::Context> {
   size_t operator()(const mxnet::Context& ctx) const {
-    return (static_cast<size_t>(ctx.dev_type) << 32) | ctx.dev_id;
+    size_t res = 0;
+    res = dmlc::HashCombine(res, static_cast<size_t>(ctx.dev_type));
+    res = dmlc::HashCombine(res, static_cast<size_t>(ctx.dev_id));
+    return res;
   }
 };
 }
diff --git a/tests/cpp/misc/base.cc b/tests/cpp/misc/base.cc
new file mode 100644
index 0000000..b560f02
--- /dev/null
+++ b/tests/cpp/misc/base.cc
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+#include <mxnet/base.h>
+
+using namespace mxnet;
+using namespace std;
+
+/*
+ * Test that different Context have different hash values
+ */
+TEST(ContextHashTest, ContextHashUnique) {
+    set<size_t> hashes;
+    size_t collision_count = 0;
+    size_t total = 0;
+    for (size_t dev_type = 0; dev_type < 32; ++dev_type) {
+        for (size_t dev_id = 0; dev_id < 64; ++dev_id) {
+            auto ctx = Context::Create(static_cast<Context::DeviceType>(dev_type), dev_id);
+            size_t res = std::hash<Context>()(ctx);
+            auto insert_res = hashes.insert(res);
+            if (!insert_res.second)
+                ++collision_count;
+            ++total;
+        }
+    }
+    double collision = collision_count / static_cast<double>(total);
+    cout << "mxnet::Context std::hash collision ratio: " << collision << endl;
+    EXPECT_LE(collision, 0.04);
+}
diff --git a/tests/cpp/test_main.cc b/tests/cpp/test_main.cc
index fc46dff..592a036 100644
--- a/tests/cpp/test_main.cc
+++ b/tests/cpp/test_main.cc
@@ -53,19 +53,21 @@ bool csv = false;
 #if MXNET_USE_CUDA
 
 static bool checkForWorkingCuda() {
-  int count = 0;
-  if (cudaSuccess == cudaGetDeviceCount(&count)) {
-    if (count == 0) return -1;
-    for (int device = 0; device < count; ++device) {
+  int device_count = 0;
+  bool workingCuda = false;
+  if (cudaSuccess == cudaGetDeviceCount(&device_count)) {
+    for (int device = 0; device < device_count; ++device) {
       cudaDeviceProp prop;
       if (cudaSuccess == cudaGetDeviceProperties(&prop, device)) {
-        std::printf("%d.%d ", prop.major, prop.minor);
-        return true;
+        std::cout << "Found CUDA Device #: " << device << " properties: " << prop.major
+                  << "." << prop.minor << std::endl;
+        workingCuda = true;
       }
     }
   }
-  std::cerr << "Warning: Could not find working CUDA driver" << std::endl;
-  return false;
+  if (!workingCuda)
+    std::cerr << "Warning: Could not find working CUDA device" << std::endl;
+  return workingCuda;
 }
 #else
 static bool checkForWorkingCuda() {

-- 
To stop receiving notification emails like this one, please contact
zhasheng@apache.org.