singa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wan...@apache.org
Subject [5/6] incubator-singa git commit: SINGA-104 Add Context Class
Date Thu, 26 Nov 2015 11:32:02 GMT
SINGA-104 Add Context Class

Add cuda_utils.h;
Add comments for context.h


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/e3bda08d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/e3bda08d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/e3bda08d

Branch: refs/heads/master
Commit: e3bda08d8428e3a6a23bf4de8c356406a8126cd8
Parents: 3841bc5
Author: Wei Wang <wangwei@comp.nus.edu.sg>
Authored: Thu Nov 26 16:22:54 2015 +0800
Committer: Wei Wang <wangwei@comp.nus.edu.sg>
Committed: Thu Nov 26 16:34:22 2015 +0800

----------------------------------------------------------------------
 include/singa/utils/context.h    | 25 +++++++---
 include/singa/utils/cuda_utils.h | 91 +++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e3bda08d/include/singa/utils/context.h
----------------------------------------------------------------------
diff --git a/include/singa/utils/context.h b/include/singa/utils/context.h
index 5223012..5066633 100644
--- a/include/singa/utils/context.h
+++ b/include/singa/utils/context.h
@@ -40,10 +40,12 @@ namespace singa {
 
 /**
  * Context is used as a global singleton, which stores the mapping from CPU
- * thread id to GPU device id. It manages the handlers for GPU
+ * thread id to GPU device id. If a thread has no GPU, then its associated
+ * device id is -1. It manages (e.g., creating) the handlers for GPU
  * devices. It also manages the GPU and CPU random generators, which are created
  * when accessed. One CPU thread has a CPU random generator. A GPU device
- * has a GPU random generator.
+ * has a GPU random generator, which is accessible after assigning the GPU
+ * device with a CPU thread via SetupDevice.
  */
 class Context {
  public:
@@ -75,7 +77,7 @@ class Context {
 
    }
   /**
-   * Constructor.
+   * Constructor, init handlers and GPU rand generators to nullptr.
    */
   Context() {
     for (int i = 0; i < kMaxNumGPU; i++) {
@@ -86,7 +88,7 @@ class Context {
 
   /**
    * @return the ID of the device attached to a given CPU thread, or -1 if this
-   * thread has not attached GPU device.
+   * thread has not been attached GPU device.
    */
 	int device_id(const std::thread::id& tid) {
     if (device_id_.find(tid) != device_id_.end())
@@ -94,11 +96,10 @@ class Context {
     else
       return -1;
 	}
-
   /**
    * Setup the CPU thread, which may be assigned a GPU device.
+   * If there is no GPU device, then set did to -1.
    * Set the random seed to -1.
-   * A GPU handler will be created for the GPU device.
    * @param[in] thread::id CPU thread ID
    * @param[in] device_id GPU device ID
    */
@@ -109,11 +110,14 @@ class Context {
    * @copy SetupDevice(const int, const int);
    * @param[in] seed random seed
    */
-  void SetupDevice(const std::thread::id& tid, const int did, long long seed) {
+  void SetupDevice(const std::thread::id& tid, const int did, const int seed) {
     device_id_[tid] = did;
     seed_[tid] = seed;
   }
 
+  /**
+   * Activate the GPU device by calling cudaSetDevice.
+   */
   void ActivateDevice(const int device_id) {
     CHECK_GE(device_id, 0);
 #ifdef USE_GPU
@@ -139,6 +143,10 @@ class Context {
     return rand_generator_[tid];
   }
 #ifdef USE_GPU
+  /**
+   * Get the handler of the GPU which is assigned to the given thread.
+   * Calls cublas_handle(const int);
+   */
   cublasHandle_t cublas_handle(const std::thread::id thread_id) {
     return cublas_handle(device_id(thread_id));
   }
@@ -157,6 +165,9 @@ class Context {
     }
     return cublas_handle_[device_id];
   }
+  /**
+   * Get the rand generator of the GPU device assigned to the given thread.
+   */
 	curandGenerator_t curand_generator(const std::thread::id thread_id) {
     return curand_generator(device_id(thread_id));
   }

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e3bda08d/include/singa/utils/cuda_utils.h
----------------------------------------------------------------------
diff --git a/include/singa/utils/cuda_utils.h b/include/singa/utils/cuda_utils.h
new file mode 100644
index 0000000..b27a6bb
--- /dev/null
+++ b/include/singa/utils/cuda_utils.h
@@ -0,0 +1,91 @@
+/************************************************************
+*
+* 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.
+*
+*************************************************************/
+
+/**
+ * The code is adapted from that of Caffe whose license is attached.
+ *
+ * COPYRIGHT
+ * All contributions by the University of California:
+ * Copyright (c) 2014, The Regents of the University of California (Regents)
+ * All rights reserved.
+ * All other contributions:
+ * Copyright (c) 2014, the respective contributors
+ * All rights reserved.
+ * Caffe uses a shared copyright model: each contributor holds copyright over
+ * their contributions to Caffe. The project versioning records all such
+ * contribution and copyright details. If a contributor wants to further mark
+ * their specific copyright on a particular contribution, they should indicate
+ * their copyright solely in the commit message of the change when it is
+ * committed.
+ * LICENSE
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * CONTRIBUTION AGREEMENT
+ * By contributing to the BVLC/caffe repository through pull-request, comment,
+ * or otherwise, the contributor releases their content to the
+ * license and copyright terms herein.
+ *
+ */
+#ifndef SINGA_UTILS_CUDA_UTILS_H_
+#define SINGA_UTILS_CUDA_UTILS_H_
+#include <cublas_v2.h>
+#include <cuda.h>
+#include <cuda_runtime.h>
+#include <curand.h>
+
+// CUDA: various checks for different function calls.
+#define CUDA_CHECK(condition) \
+  /* Code block avoids redefinition of cudaError_t error */ \
+  do { \
+    cudaError_t error = condition; \
+    CHECK_EQ(error, cudaSuccess) << " " << cudaGetErrorString(error); \
+  } while (0)
+
+#define CUBLAS_CHECK(condition) \
+  do { \
+    cublasStatus_t status = condition; \
+    CHECK_EQ(status, CUBLAS_STATUS_SUCCESS) << " " \
+      << caffe::cublasGetErrorString(status); \
+  } while (0)
+
+#define CURAND_CHECK(condition) \
+  do { \
+    curandStatus_t status = condition; \
+    CHECK_EQ(status, CURAND_STATUS_SUCCESS) << " " \
+      << caffe::curandGetErrorString(status); \
+  } while (0)
+
+#endif  // SINGA_UTILS_CUDA_UTILS_H_


Mime
View raw message