-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcommon.h
More file actions
132 lines (111 loc) · 3.53 KB
/
common.h
File metadata and controls
132 lines (111 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* 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.
*/
#ifndef SINGA_CORE_COMMON_H_
#define SINGA_CORE_COMMON_H_
#include <random>
#include <chrono>
#include "singa/singa_config.h"
#include <atomic>
#include <memory>
#include "singa/utils/logging.h"
#include <string>
#ifdef USE_CUDA
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <curand.h>
#ifdef USE_CUDNN
#include <cudnn.h>
#endif
#endif // USE_CUDA
#ifdef USE_OPENCL
#include "singa/utils/opencl_utils.h"
#endif // USE_OPENCL
using std::atomic;
namespace singa {
namespace lang {
/// To implemente functions using cpp libraries
typedef struct _Cpp { } Cpp;
/// To implemente functions using cuda libraries
typedef struct _Cuda { } Cuda;
/// To implement function using opencl libraries
typedef struct _Opencl { } Opencl;
} // namespace lang
class Device;
struct DeviceOptInfoToAppend;
/// Block represent a chunk of memory (on device or host).
class Block {
public:
Block(void* ptr, size_t size, size_t offset = 0, Device* ptr_device = nullptr)
: data_(ptr), size_(size), offset_(offset), ptr_device_(ptr_device) {
ref_count_ = 1; // std::make_shared<std::atomic<int>>(1);
}
// Disabled as it is not used currently.
// Block(void* ptr, size_t size, size_t offset, std::shared_ptr<atomic<int>>
// ref) : data_(ptr), size_(size), offset_(offset), ref_count_(ref) {}
void* mutable_data() ;
const void* data() const;
void* get_data() ;
void update_data(void* data_new) ;
size_t size() const { return size_; }
size_t offset() const { return offset_; }
int IncRefCount() {
return ++ref_count_; // Note do not use ref_count_++;
}
int DecRefCount() {
return --ref_count_;
}
int ref_count() const { return ref_count_.load(); }
bool initialized() const {
return initialized_;
}
private:
Block() {}
void* data_ = nullptr;
size_t size_ = 0;
size_t offset_ = 0;
Device* ptr_device_;
bool initialized_ = false;
// Disabled as it is not used currently.
// std::shared_ptr<std::atomic<int>> ref_count_ = nullptr;
std::atomic<int> ref_count_;
};
// struct for Append purpose in device class.
struct DeviceOptInfoToAppend{
string operation_type;
string block_ptr;
int size;
long t = (std::chrono::system_clock::now()).time_since_epoch().count();
DeviceOptInfoToAppend(string opt_type, string ptr,int s):operation_type(opt_type),block_ptr(ptr),size(s){}
};
typedef struct _Context {
std::mt19937 random_generator;
#ifdef USE_CUDA
cublasHandle_t cublas_handle;
cudaStream_t stream;
curandGenerator_t curand_generator;
#ifdef USE_CUDNN
cudnnHandle_t cudnn_handle;
#endif
#endif // USE_CUDA
#ifdef USE_OPENCL
// This stores the context ID of the OpenCL context controlled by ViennaCL.
long vcl_ctx_id;
#endif
} Context;
} // namespace singa
#endif // SINGA_CORE_COMMON_H_