Apache Singa
A General Distributed Deep Learning Library
common.h
1 
19 #ifndef SINGA_CORE_COMMON_H_
20 #define SINGA_CORE_COMMON_H_
21 #include <random>
22 #include <chrono>
23 #include "singa/singa_config.h"
24 #include <atomic>
25 #include <memory>
26 #include "singa/utils/logging.h"
27 
28 #ifdef USE_CUDA
29 #include <cuda_runtime.h>
30 #include <cublas_v2.h>
31 #include <curand.h>
32 #ifdef USE_CUDNN
33 #include <cudnn.h>
34 #endif
35 #endif // USE_CUDA
36 
37 
38 #ifdef USE_OPENCL
39 #include "singa/utils/opencl_utils.h"
40 #endif // USE_OPENCL
41 
42 #ifdef USE_MKLDNN
43 #include <mkldnn.hpp>
44 #endif // USE_MKLDNN
45 
46 using std::atomic;
47 
48 namespace singa {
49 
50 namespace lang {
52 typedef struct _Cpp { } Cpp;
54 typedef struct _Cuda { } Cuda;
56 typedef struct _Opencl { } Opencl;
57 } // namespace lang
58 
60 class Block {
61  public:
62  Block(void* ptr, size_t size, size_t offset = 0)
63  : data_(ptr), size_(size), offset_(offset) {
64  ref_count_ = 1; // std::make_shared<std::atomic<int>>(1);
65  }
66  // Disabled as it is not used currently.
67  // Block(void* ptr, size_t size, size_t offset, std::shared_ptr<atomic<int>>
68  // ref) : data_(ptr), size_(size), offset_(offset), ref_count_(ref) {}
69  void* mutable_data() {
70  initialized_ = true;
71  return static_cast<char*>(data_) + offset_;
72  }
73  const void* data() const {
74  CHECK(initialized_) << "Must initialize data before reading it";
75  return static_cast<char*>(data_) + offset_;
76  }
77  size_t size() const { return size_; }
78  size_t offset() const { return offset_; }
79  int IncRefCount() {
80  return ++ref_count_; // Note do not use ref_count_++;
81  }
82  int DecRefCount() {
83  return --ref_count_;
84  }
85  int ref_count() const { return ref_count_.load(); }
86 
87  bool initialized() const {
88  return initialized_;
89  }
90 
91  private:
92  Block() {}
93  void* data_ = nullptr;
94  size_t size_ = 0;
95  size_t offset_ = 0;
96  bool initialized_ = false;
97  // Disabled as it is not used currently.
98  // std::shared_ptr<std::atomic<int>> ref_count_ = nullptr;
99  std::atomic<int> ref_count_;
100 };
101 
102 typedef struct _Context {
103  std::mt19937 random_generator;
104 #ifdef USE_CUDA
105  cublasHandle_t cublas_handle;
106  cudaStream_t stream;
107  curandGenerator_t curand_generator;
108 #ifdef USE_CUDNN
109  cudnnHandle_t cudnn_handle;
110 #endif
111 #endif // USE_CUDA
112 
113 #ifdef USE_OPENCL
114  // This stores the context ID of the OpenCL context controlled by ViennaCL.
115  long vcl_ctx_id;
116 #endif
117 
118 #ifdef USE_MKLDNN
119  mkldnn::engine *engine;
120 #endif // USE_MKLDNN
121 
122 } Context;
123 
124 } // namespace singa
125 #endif // SINGA_CORE_COMMON_H_
To implemente functions using cuda libraries.
Definition: common.h:54
To implement function using opencl libraries.
Definition: common.h:56
To implemente functions using cpp libraries.
Definition: common.h:52
Definition: common.h:102
Block represent a chunk of memory (on device or host).
Definition: common.h:60
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48